Question

Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...

Create a class named MyTriangle that contains the following three methods:

  • public static boolean isValid(double sidea, double sideb, double sidec)
  • public static double area(double sidea, double sideb, double sidec)
  • public static String triangletType(double a, double b, double c)

The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent in any particular order.

The area method returns the area of the triangle. Given the lengths of the three sides of the triangle, the area of the triangle can be computed using Heron's formula (do the research).

The triangleType method returns one of the following strings: "Equilateral", "Isosceles", "Scalene", "Invalid Triangle". You can determine the triangle's type if a, b, and c represent the sides in ascending order, you must first determine if the triangle is valid. If it is valid, the triangle is equilateral if a == c. Isosceles if a == b or b == c. Scalene otherwise.

Define a main method within your class that inputs from the user three sides for a triangle in any order. The main method should then display the triangle's type. If the triangle is valid, you must also output the area of the triangle. Zip your complete netbeans project and submit it by the due date.

Please make easy code! java code!

Homework Answers

Answer #1
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Working of the program

  • isValid() method accept three sides
  • The sides may not be in ascending order
  • It must be in order inorder to check the triangle is valid or not
  • To make them order(ie a,b,c in ascending order) the values of a,b and c are interchanged if necessary
  • After executing first two if statement, a will gets smallest value
  • After executing the third if statement, b will get second largest and c will get largest value
  • Then the sum of two smaller side is compared with larger side
  • if sum is greater,then triangle is valid and return true
  • Otherwise triangle is invalid and return false
  • area() method calculate area using Heron's formula where s=(a+b+c)/2
  • triangleType() method first arrange sides in ascending order
  • Then it checks the triangle is invalid or not
  • if not,check it is equlateral by comparing a and c
  • if not,check it is Isoceles by comparing a and b or b and c are equal
  • if none of the condition is true,then it is scalene
  • In main() method,sides of triangle are accepted from user
  • Triangle type is printed by calling triangleType() method
  • Area is printed if triangle is valid

Source code

//Importing Scanner class to accept user input
import java.util.Scanner;
public class MyTriangle {
    //isValid() method to check triangle is valid or not
    public static boolean isValid(double sidea,double sideb,double sidec)
    {
      double a,b,c,temp;
      //assigning a,b,c with sidea,sideb and sidec
      a=sidea;
      b=sideb;
      c=sidec;
      //if a contain larger value than b,interchange both and make a contain smaller value
      if(a>b)
      {
          temp=a;
          a=b;
          b=temp;
      }
      //if a contain larger value than c,interchange both and make a contain smaller value
      if(a>c)
      {
          temp=a;
          a=c;
          c=temp;
      }
      //if b contain larger value than c,interchange both and make b contain smaller value
      if(b>c)
      {
          temp=b;
          b=c;
          c=temp;
      }
      //Now a,b,c hold values in ascending order
      //check sum of two smaller side is greater than third side
      if((a+b)>c)
          //if yes, valid triangle and return true
          return true;
      else
          //if no, invalid triangle return false
          return false;
    }
    //method to calculate area using Heron's formula
    public static double area(double a,double b,double c)
    {
        double s,area;
        //finding s
        s=(a+b+c)/2;
        //finding area
        area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
        return area;
    }
    public static String triangleType(double a,double b,double c)
    {
        double temp;
        //if a contain larger value than b,interchange both and make a contain smaller value
        if(a>b)
        {
            temp=a;
            a=b;
            b=temp;
        }
        //if a contain larger value than c,interchange both and make a contain smaller value
        if(a>c)
        {
            temp=a;
            a=c;
            c=temp;
        }
        //if b contain larger value than c,interchange both and make b contain smaller value
        if(b>c)
        {
            temp=b;
            b=c;
            c=temp;
        }
        //Now a,b,c hold values in ascending order
        //check sum of two smaller side is less than  or equals third side
        if((a+b)<=c)
            //if yes triangle is invalid
            return "Triangle is invalid";
        //otherwise check if a and c are equal(imply b also equals to both)
        else if (a==c)
            //equilateral
            return "Equilateral";
        //otherwise check if a and b are equal or b and c are equal
        else if (a==b || b==c)
            //isosceles
            return "Isosceles";
        //if none of the condition is true
        else
            //Scalene
            return "Scalene";

    }

    public static void main(String[] args) {
        //variables to hold sides of triangle
        double a,b,c;
        //Creating a scanner object to read user input
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter three sides of triangle: ");
        //reading sides
        a=sc.nextDouble();
        b=sc.nextDouble();
        c=sc.nextDouble();
        //printing triangle's type
        System.out.println("Triangle type: "+triangleType(a,b,c));
        //printing area if triangle is valid
        if(isValid(a,b,c))
            System.out.println("Area of the triangle is: "+area(a,b,c));
    }
}

Screen shot of the code

Screen shot of the output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
java Consider the following class definition: public class Circle { private double radius; public Circle (double...
java Consider the following class definition: public class Circle { private double radius; public Circle (double r) { radius = r ; } public double getArea(){ return Math.PI * radius * radius; } public double getRadius(){ return radius; } } a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.” b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return...
public class QuestionX {     private static QuestionX quest = new QuestionX();      private QuestionX(){     ...
public class QuestionX {     private static QuestionX quest = new QuestionX();      private QuestionX(){      }      public static QuestionX getQuestion() {             return quest;      }      public void doSomething(){            System.out.println("In doSomething");      }           //other methods for this class to do things } How is this class used by a client? Provide code showing how you would use this class in the main method of a program and what constraints are placed on class usage...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static <T extends Comparable<T>> int findMin(T[] arr): Iterate through the array to find the index of the smallest element in the array. If there are two elements with the smallest value, the method should return the index of the first one. The method should run in O(n). ·public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively....
2.13 LAB: Driving cost - methods Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and...
2.13 LAB: Driving cost - methods Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975. Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by the variable  account20, which of the following will call the  getNumberOfAccounts method? a. account20.getNumberOfAccounts(); b. SavingsAccount.account20.getNumberOfAccounts(); c. SavingsAccount.getNumberOfAccounts(); d. getNumberOfAccounts(); e. a & c f. a & b 2.In the following class, which variables can the method printStats use? (Mark all that apply.) public class Item { public static int amount = 0; private int quantity; private String name; public Item(String inName, int inQty) { name...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[]...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[] gpaarr ) to calculate the average of gpaArray; 2) add codes in the for loop to calculate sum 3) print the average . public class Test { public static void main (String args[]) { double[ ] gpaArray = { 2.5, 4.0, 3.8, 3.2, 2.9, 4.0 } ;   //add your codes here (you can put your codes in the Answer area with/without copying the original...
Practice manipulating an object by calling its methods. Complete the class Homework1 class using String methods....
Practice manipulating an object by calling its methods. Complete the class Homework1 class using String methods. Remember that all the String methods are accessors. They do not change the original String. If you want to apply multiple methods to a String, you will need to save the return value in a variable. Complete the class by doing the following: + Print the word in lowercase + Replace "e" with "3" (Use the unmodified variable word) + Print the changed word...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT