Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. package Questions;
  2. import java.util.Scanner;
  3. public class Q2 {
  4. //Return true if the sum of any two sides is greater than the third side.
  5. public static boolean isValid(double side1, double side2, double side3){
  6. if(side1+side2>side3) return true;
  7. if(side1+side3>side2) return true;
  8. if(side2+side3>side1) return true;
  9. return false;
  10. }
  11. //Return the area of the triangle.
  12. public static double area(double side1, double side2, double side3){
  13. double s = (side1+side2+side3)/2;
  14. return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
  15. }
  16. public static void main(String[] args) {
  17. Scanner input = new Scanner(System.in);
  18. System.out.println("Enter three sides in double: ");
  19. boolean valid = false;
  20. double s1,s2,s3;
  21. do{
  22. s1 = input.nextDouble();
  23. s2 = input.nextDouble();
  24. s3 = input.nextDouble();
  25. valid = isValid(s1,s2,s3);
  26. if(valid)break;
  27. System.out.println("Invalid input. Try again.");
  28. }while (!valid);
  29. System.out.println(String.format("Area: %f",area(s1,s2,s3)));
  30.  
  31.  
  32.  
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement