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...
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...
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.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...
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....
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...
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...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...