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.

Homework Answers

Answer #1
import java.util.Scanner;

public class MyTriangle {

        public static void main(String[] args) {

                Scanner scanner = new Scanner(System.in);

                double sideA, sideB, sideC, areaOfTriangle = 0.0;
                boolean trianlgeValid = false;

                // Asking for sides of triangle as INPUT from user
                sideA = scanner.nextDouble();
                sideB = scanner.nextDouble();
                sideC = scanner.nextDouble();

                // checking if the triangle is VALID or INVALID
                trianlgeValid = isValid(sideA, sideB, sideC);

                if (trianlgeValid == true) {

                        System.out.println("Valid triangle");

                        // If it is valid then calculate the area and type
                        areaOfTriangle = area(sideA, sideB, sideC);

                        System.out.println("Area : " + areaOfTriangle);

                        // Here we are directly printing the returned value from the method
                        System.out.println("Type : " + triangletType(sideA, sideB, sideC));

                } else {
                        // If it is not valid then print this
                        System.out.println("InValid Triangle");
                }

        }

        public static boolean isValid(double sidea, double sideb, double sidec) {

                // Initializing an array for storing the sides of triangle to sort them
                // ascending
                // wise
                double arr[] = new double[3];

                boolean triangleValid = false;

                // Copying the values of sides to array
                arr[0] = sidea;
                arr[1] = sideb;
                arr[2] = sidec;

                // Sorting the sides using SELECTION SORT
                for (int i = 1; i < 3; i++) {
                        double temp = arr[i];
                        int j = i - 1;

                        while (j >= 0 && temp < arr[j]) {
                                arr[j + 1] = arr[j];
                                j = j - 1;
                        }
                        arr[j + 1] = temp;
                }

                // Then again copy the values of array to sides
                sidea = arr[0];
                sideb = arr[1];
                sidec = arr[2];

                // Calculating if the triangle is valid or not
                if ((sidea + sideb) > sidec) {

                        // If it is then
                        triangleValid = true;
                } else {
                        // If it is not then
                        triangleValid = false;
                }

                return triangleValid;
        }

        public static double area(double sidea, double sideb, double sidec) {
                double areaOfTriangle = 0.0, s = 0.0;

                // Here we are calculating the s value according to the HERON'S FORMULA
                s = (sidea + sideb + sidec) / 2.0;

                // And the finding the area using same HERON'S FORMULA
                // We are using (Math.sqrt) to find square root the calculated value
                
                areaOfTriangle = Math.sqrt((s * (s - sidea) * (s - sideb) * (s - sidec)));

                return areaOfTriangle;
        }

        public static String triangletType(double a, double b, double c) {
                String type = "";
                double arr[] = new double[3];

                // Using same technique as used above to sort the sides for calculating their
                // types
                arr[0] = a;
                arr[1] = b;
                arr[2] = c;

                for (int i = 1; i < 3; i++) {
                        double temp = arr[i];
                        int j = i - 1;

                        while (j >= 0 && temp < arr[j]) {
                                arr[j + 1] = arr[j];
                                j = j - 1;
                        }
                        arr[j + 1] = temp;
                }

                a = arr[0];
                b = arr[1];
                c = arr[2];

                // calculating the type
                if (a == c) {
                        type = "Equilateral";

                } else if (a == b || b == c) {
                        type = "Isosceles";
                } else {
                        type = "Scalene";
                }

                // returning the calculated type
                return type;
        }

}
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...
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) {           ...
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...
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...
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...
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):");...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
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....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT