Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement two Java programs for programming exercise 6.19, page 238.

ID: 3598567 • Letter: D

Question

Design and implement two Java programs for programming exercise 6.19, page 238. The first program (called MyTriangle) is to implement the specified methods. The second program (called TestMyTriangle) is to test the first program methods. Program TestMyTriangle is used to compute the area of a triangle if the input is valid. Use Heron's formula (provided in textbook exercise 2.19) to compute the area of the triangle (do not use any other formula). Notice that method isvalid() is used to validate the input before attempting to compute the area. See listings 6.10 and 6.11 (page 224) on how to write 2 programs (main program and test program). Design the test program main method (all input and output is handled by the main method) such that it allows the user to re-run the program with different inputs ((i.e., use a loop structure). Document your code and organize the outputs properly using appropriate formatting techniques. Submit both programs to the same Dropbox folder.

6.19 (The MyTriangle class) Create a class named MyTriangle that contains the following two methods: Return true if the sum of any two sides is greater than the third side. public static boolean isValid( double sidel, double side2, double side3)

Explanation / Answer

import java.util.*;

import java.lang.*;

class MyTriangle

{

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;

}

public static double area(double side1,double side2,double side3)

{

if(isValid(side1,side2,side3) == true)

{

double s = (side1+side2+side3)/2;

return (Math.sqrt(s*(s-side1)*(s-side2)*(s-side3))); //Heron's formula to find area of triangle

}

else

{

System.out.println("Input is invalid");

return 0;

}

}

}


class TestMyTriangle

{

public static void main (String[] args)

{
double side1,side2,side3;
String option;

Scanner sc = new Scanner(System.in);

System.out.println(" Enter the three sides of triangle : ");

side1 = sc.nextDouble();
side2 = sc.nextDouble();
side3 = sc.nextDouble();


do
{

if(MyTriangle.isValid(side1,side2,side3))

System.out.println(" The area of triangle = "+ MyTriangle.area(side1,side2,side3));

else

System.out.println("Invalid triangle");

System.out.println(" Do you want to continue[yes/no] ?");
option = sc.next();

if(option.equals("no"))
break;
else
{

System.out.println(" Enter the three sides of triangle : ");

side1 = sc.nextDouble();
side2 = sc.nextDouble();
side3 = sc.nextDouble();

}

}while(!option.equals("no"));

}
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote