Respuesta :
Answer:
Hi there Collegebound! The implementation of the Triangle class and the Drive program is below. Copy the code below into the file Triangle.java and then compile it with the command: "javac Triangle.java". To run the program, type the command: "java Drive". You should see the following result if you test with the same inputs:
$java Drive
Enter side 1 of the triangle:
1
Enter side 2 of the triangle:
1
Enter side 3 of the triangle:
1
Enter color of the triangle:
orange
Enter if triangle is filled:
true
Area of Triangle is: 0.4330127018922193
Perimeter of Triangle is: 3.0
Color of Triangle is: orange
Triangle is filled: true
Explanation:
import java.lang.Math;
import java.util.Scanner;
public class Triangle extends GeometricObject {
double side1, side2, side3;
protected Triangle() {}
protected Triangle(double s1, double s2, double s3) {
this.side1 = s1;
this.side2 = s2;
this.side3 = s3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getArea() {
/* use Heron's formula */
double s = (this.side1 + this.side2 + this.side3) / 2;
double area = Math.sqrt(s*((s-this.side1)*(s-this.side2)*(s-this.side3)));
return area;
}
public double getPerimeter() {
return side1+side2+side3;
}
@Override
public String toString() {
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
}
}
class Drive {
public static void main(String args[]) {
double side1, side2, side3;
System.out.println("Enter side 1 of the triangle: ");
Scanner scan = new Scanner(System.in);
side1 = scan.nextDouble();
System.out.println("Enter side 2 of the triangle: ");
scan = new Scanner(System.in);
side2 = scan.nextDouble();
System.out.println("Enter side 3 of the triangle: ");
scan = new Scanner(System.in);
side3 = scan.nextDouble();
System.out.println("Enter color of the triangle: ");
scan = new Scanner(System.in);
String color = scan.next();
System.out.println("Enter if triangle is filled: ");
scan = new Scanner(System.in);
Boolean isFilled = scan.nextBoolean();
Triangle triangle = new Triangle(side1, side2, side3);
triangle.setColor(color);
triangle.setFilled(isFilled);
System.out.println("Area of Triangle is: " + triangle.getArea());
System.out.println("Perimeter of Triangle is: " + triangle.getPerimeter());
System.out.println("Color of Triangle is: " + triangle.getColor());
System.out.println("Triangle is filled: " + triangle.isFilled());
}
}