Design and implement two Java programs for programming exercise.

Design and implement two Java programs for programming exercise.

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. Notice that method isvalid() is used to validate the input before
attempting to compute the area. See listings10 and 11(page 224) on how to write 2 programs (main program
and test program). Design the test program 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 escape. Save both programs in the same folder.

 import java.util.Scanner;

 public class MyTriangle{
public static void main(String[] args){
Scanner s = new Scanner(;
System.out.print("\nEnter three sides of a triangle: ");
double x = s.nextDouble();
double y = s.nextDouble();
double z = s.nextDouble();
if (isValid(x, y, z) == true){System.out.print("\nTriangle is Valid.\nArea: "+area(x, y, z));}
else {System.out.print("\nInvalid 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;}}

public static double area(double side1, double side2, double side3){
double s, s1, s2, s3, area;
s = (side1 + side2 + side3)/2;
s1 = (s - side1); s2 = (s - side2); s3 = (s - side3);
area = Math.sqrt(s * s1 * s2 * s3);
return area;}


Popular posts from this blog

How to change this to <%Html.ActionLink%> in my mvc application ?