Learning objectives Implementing a Class Writing a Tester Pr
Solution
import java.util.Scanner;
public class TriangleDemo {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print(\"Enter the length of the three sides of your triangle: \");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
System.out.println(\"The lengths of the sides of your triangle are: \" + \'\ \' + a + \'\ \'+ b + \'\ \' + c);
System.out.println(\"The are of your triangle according to Heron\'s Formula is: \" + getArea(a,b,c));
}
public static double getArea(double a, double b, double c) {
double s = (a + b + c)/2;
double x = ((s) * (s-a) * (s-B)/> * (s-c));
double area = Math.sqrt(x);
return area;
}
}
import java.util.Scanner;
import java.text.*;
public class TriangleTestDemo
{
int a, b, c;
MyTriangle()
{
this.a=0;
this.b=0;
this.c=0;
}
TriangleTestDemo (int a, int b, int c)
{
this.a=a;
this.b=b;
this.c=c;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
boolean valid = true;
double side1, side2, side3, area, s;
System.out.print(\"Please enter side1: \");
side1 = input.nextDouble();
System.out.print(\"Please enter side2: \");
side2 = input.nextDouble();
System.out.print(\"Please enter side3: \");
side3 = input.nextDouble();
valid = isValid(side1, side2, side3);
TriangleTestDemo obj = new TriangleTestDemo();
if(valid)
{
double x= obj.area(side1, side2, side3);
System.out.print(\"The area of the triangle is: \" + x);
}
else
System.out.println(\"The input is not valid\");
}
public static boolean isValid(double side1, double side2, double side3)
{
return(((side1 + side2) >side3) && ((side2 + side3) > side1) && ((side3 + side1) >side2));
}
public static double area(double side1, double side2, double side3)
{
double s = (side1 + side2 + side3)/2;
double area = Math.sqrt(s * (s - side1) * (s - side3)*(s-side2));
return area;
}
}
| import java.util.Scanner; |


