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...
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...
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...
1. Suppose that the integer variables n and m have been initialized. Check all the correct...
1. Suppose that the integer variables n and m have been initialized. Check all the correct statements about the following code fragment. String s2 = (n>=m) ? "one" : ( (m<=2*n) ? "two": "three" ); Question 5 options: If m is strictly greater than 2n then the value of s2 is "two" If n is equal to m then the value of s2 is "two" If m is strictly greater than 2n then the value of s2 is "three" If...
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) 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...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...