Question

In: Computer Science

Write a class with 2 methods. The first method determines if any two sides added together...

Write a class with 2 methods. The first method determines if any two sides added together are greater than the remaining side. The second method calculates the triangle’s area. // Method 1: If sum of any two sides is greater than the remaining side, return true public static boolean isValid(double sid1, double side2, double side3) // Method 2: Returns the triangle area public static double area(double side1, double side2, double side3) Develop a test program that takes in the three sides of a triangle and uses the isValid( )method to determine if the input is valid according to the above criteria. If the inputs are valid display the area. Otherwise display an error message and continue to ask for inputs until a valid set is received. The formula for computing the area of a triangle is as follows: ? = (????1 + ????2 + ????3)/2 ???? = √?(? − ????1)(? − ????2)(? − ????3) Hint: use a while loop to check the validity of the user’s input

I need the answer for Java Eclipse IDE. Thank you :))

Solutions

Expert Solution

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

Triangle.java
---
import java.util.Scanner;

public class Triangle {
   // Method 1: If sum of any two sides is greater than the remaining side, return true
   public static boolean isValid(double side1, double side2, double side3) {
       if(side1 + side2 > side3 && side2+ side3 > side1 && side1+side3 > side2)
           return true;
       else
           return false;
   }
   // Method 2: Returns the triangle area
   public static double area(double side1, double side2, double side3) {
       double s = (side1 + side2 + side3)/2;
       double v = s * (s-side1) * (s-side2) * (s-side3);
       return Math.sqrt(v);
   }
  
   public static void main(String[] args) {
       double side1, side2, side3;
       boolean valid;
       Scanner keybd = new Scanner(System.in);
       do {
           System.out.println("Enter 3 sides of a triangle: ");
           side1 = keybd.nextDouble();
           side2 = keybd.nextDouble();
           side3 = keybd.nextDouble();
          
              
           valid = isValid(side1, side2, side3);
           if(!valid)
               System.out.println("The sides do not form a triangle");
       }while(!valid);
      
       System.out.println("The area of the triangle is " + area(side1, side2, side3));
   }
}


output
---
Enter 3 sides of a triangle:
1 1 2
The sides do not form a triangle
Enter 3 sides of a triangle:
1 2 3
The sides do not form a triangle
Enter 3 sides of a triangle:
3 4 5
The area of the triangle is 6.0


Related Solutions

Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with random integers between a given min and max value. It must accept for parameters, min and max values for the creation of random integers, and rows and columns for the number of rows and columns of the array. The method should return an array of rows by columns size, filled with random integers between min and max values. The second method is called printRA,...
Design an application with a single class and two static methods: method main and method isLeap....
Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not. public static boolean isLeap ( int year ) The year is leap year if it is divisible by 4, but not divisible by 100 except if it is divisible by 400. Examples 2007 is not a leap year 2008 is leap year 1700...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the node that contains the val. .
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the node that contains the val.
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2 Codes in C please.s
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines...
Q6: (Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2
Part A: Create a project with a Program class and write the following two methods (headers...
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric...
Write a method that takes the sides of a dice and produces a random number between...
Write a method that takes the sides of a dice and produces a random number between 1 and the value of the dice sides. use the following header: public static int roll(int diceSides) Add a main method to the class that tests the method by prompting for a int for dice sides and a color for the die, invoking the method and printing the returned number and the color of the die. SAMPLE RUN #1: java DiceRoll Enter a number...
Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not...
Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not loops. You may use JDK String methods like substring() and length(), but do not use the JDK methods to avoid coding the algorithms assigned. For example, don't use String.reverse(). public boolean recursiveContains(char c, String s) returns true if the String contains the char, otherwise returns false. Here is the code for this method, which you should use as an example to see how to...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for a dynamic array with enough room for both integer arrays, and then copying the elements of both arrays to the new array as follows:             first, copy the elements of the first array up to a given position,             then, copy all the elements of the second array,             then, copy the remaining elements in the first array. The function should have parameters for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT