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...
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...
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...
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...
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...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add the given Student to the StudentList. The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList. In Assign5Test.java do the following: Create a new StudentList containing 3 students. Print the info of all the Students in the StudentList using...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT