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: 3727766 • 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 Assignment Submission folder.

Format is for J grasp

*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

MyTriangle.java

public class MyTriangle {

// Declaring instance variables

private double side1;

private double side2;

private double side3;

// Zero argumented constructor

public MyTriangle() {

super();

}

/*

* This method is used to calculate the area of the triangle using Herons

* formula

*/

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

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

double area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));

return area;

}

/*

* This method is used to check whether the three sides are valid sides of

* the triangle

*/

public static boolean isValid(double side1, double side2, double side3) {

if (side1 + side2 > side3 && side1 + side3 > side2

&& side2 + side3 > side1)

return true;

else

return false;

}

//This method will displays the Type of the Triangle

public static String triangletType(double a, double b, double c) {

// Declaring variable

double temp;

/*

* Checking whether first number is greater than second number or not if

* yes, Swapping will happen

*/

if (a > b) {

temp = a;

a = b;

b = temp;

}

/*

* Checking whether second number is greater than third number or not if

* yes, Swapping will happen

*/

if (b > c) {

temp = b;

b = c;

c = temp;

}

/*

* Checking whether first number is greater than second number or not if

* yes, Swapping will happen

*/

if (a > b) {

temp = a;

a = b;

b = temp;

}

if (isValid(a, b, c)) {

if (a == c)

return "Equilateral";

else if (a == b || b == c)

return "Isosceles";

else

return "Scalene";

} else

return "Invalid Triangle";

}

}

____________________

MyTriangleTest.java

import java.util.Scanner;

public class MyTriangleTest {

public static void main(String[] args) {

// declaring variables

double side1, side2, side3;

// Scanner object is used to get the inputs entered by the user

Scanner sc = new Scanner(System.in);

while(true)

{

// Getting the 3 sides of the triangle

System.out.print("Enter Length of side 1:");

side1 = sc.nextDouble();

System.out.print("Enter Length of side 2:");

side2 = sc.nextDouble();

System.out.print("Enter Length of side 3:");

side3 = sc.nextDouble();

// Checking whether the three sides are valid or not

boolean bool = MyTriangle.isValid(side1, side2, side3);

/*

* if valid find area of triangle If not,display Error message

*/

if (bool)

{

System.out.printf("Area of Triangle :%.2f ", MyTriangle.area(side1, side2, side3));

System.out.println("Triangle Type is :"+MyTriangle.triangletType(side1, side2, side3));

}

else

System.out.println("** Invalid Triangle Sides. **");

//Getting the character from the user 'Y' or 'y' or 'N' or 'n'

System.out.print("Do you want to continue(Y/N) ::");

char ch = sc.next(".").charAt(0);

if(ch=='Y'||ch=='y')

continue;

else

{

System.out.println(":: Program Exit ::");

break;

}

}

}

}

___________________

Output:

Enter Length of side 1:3
Enter Length of side 2:3
Enter Length of side 3:4
Area of Triangle :4.47
Triangle Type is :Isosceles
Do you want to continue(Y/N) ::Y
Enter Length of side 1:2
Enter Length of side 2:7
Enter Length of side 3:9
** Invalid Triangle Sides. **
Do you want to continue(Y/N) ::Y
Enter Length of side 1:3
Enter Length of side 2:4
Enter Length of side 3:5
Area of Triangle :6.00
Triangle Type is :Scalene
Do you want to continue(Y/N) ::N
:: Program Exit ::

_______________Thank You

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