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,...
Write a JAVA program called DBConvertor that has two methods. The first one converts ANY user...
Write a JAVA program called DBConvertor that has two methods. The first one converts ANY user input binary into its equivalent decimal number. The second method converts ANY user input decimal number into its equivalent binary.​ binaryToDecimal, decimalToBinary are the names for those methods.​ binaryToDecimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimalToBinary method.​
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
Please write in Java and have two methods: the main method and the reverse word Write...
Please write in Java and have two methods: the main method and the reverse word Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: The quick brown fox jumps over the lazy dog you should get the following output: dog lazy the over jumps fox brown quick The Then create a main method to prompt the user to enter a line of words...
An equilateral triangle is a triangle whose sides are equal. You are to write a class...
An equilateral triangle is a triangle whose sides are equal. You are to write a class called Triangle, using filenames triangle.h and triangle.cpp, that will allow the creation and handling of equilateral triangles, whose sides are integers in the range 1-39. Details: 1. The single constructor for the Triangle class should have 3 parameters: an integer size (required), which is the length of a side; a border character (optional, with a default of '#'); and a fill character (optional, with...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT