Please write this Java program as simple as possible and with comments throughou
ID: 3725013 • Letter: P
Question
Please write this Java program as simple as possible and with comments throughout to explain what is happening. Thank you!
Program 3 (20 points):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.
Explanation / Answer
CODE
// scanner class to take input from user.
import java.util.Scanner;
// DecimalFormat class to precise the area to 2 digits for better calculation.
import java.text.DecimalFormat;
//my Triangle program
class myTriangle{
public static boolean isValid(double side1, double side2, double side3){
// preparing the elements of the formula to check that the input is valid or not.
double a=side1+side2;
double b=side2+side3;
double c=side3+side1;
// this if-else-if set check if sum of any of the two sides of the triangle is greater then third side.
// return true if valid otherwise false.
if(a>side3){
return true;
}
else if(b>side1){
return true;
}
else if(c>side2){
return true;
}
else{
return false;
}
}
// method to compute the area of the Triangle.
public static double area(double side1, double side2, double side3){
// preparing the elements of the formula by dividing it into small expressions.
double s=side1+side2+side3;
double s1=s-side1;
double s2=s-side2;
double s3=s-side3;
// formula inside the square root.
double product=s*s1*s2*s3;
// final result by taking square root of the product
double area=Math.sqrt(product);
// finally return area.
return area;
}
}
// second program testMyTriangle
public class testMyTriangle{
// main method.
public static void main(String[] arguments){
// creating object of Scanner class to take input from user.
Scanner in=new Scanner(System.in);
// creating object of myTriangle class.
myTriangle obj=new myTriangle();
// object of DecimalFormat with constructor calling to precise value to 2 digits.
DecimalFormat df2 = new DecimalFormat(".##");
// declaration and initilization of the cvariable choice which runs the loop and exit
// when its value becomes 2.
int choice=1;
while(choice!=2){
// taking inputs side1, side2, side3 from the user.
System.out.print(" Enter Side 1 of Triandle: ");
double side1=in.nextDouble();
System.out.print(" Enter Side 2 of Triandle: ");
double side2=in.nextDouble();
System.out.print(" Enter Side 3 of Triandle: ");
double side3=in.nextDouble();
// calling isValid() methd and checking if the input is valid
boolean check=obj.isValid(side1, side2, side3);
if(check==true){
// if input is valid calculate the area of the triangle.
double result=obj.area(side1, side2, side3);
System.out.println(" Area of Triangle is: "+df2.format(result)+" ");
}
else{
// prints invalid if isValid() returns false.
System.out.println(" Input is Invalid");
}
System.out.println("SELECT ONE:");
System.out.println("1.)Continue 2.)Exit");
choice=in.nextInt();
}
}
}