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...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
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...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
JAVA 4.35 (Sides of a Triangle) Write an application that reads three nonzero values entered by...
JAVA 4.35 (Sides of a Triangle) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle. Here's the code: import java.util.Scanner; class TriangleYN {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);               double a;        double b;        double c;               System.out.print("Enter three sizes, separated by spaces(decimals values are acceptable):");...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a numeric object (Integer, Double or any class that extends java.lang.number) Add a method standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation as type double. Use a for each loop where appropriate. Hard code a couple of test arrays into your Demo file. You must use at least 2 different types such as Double and Integer. Your call will...
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....
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT