Design a class named Triangle that extends GeometricObject:import java.util.Scanner;abstract class GeometricObject {private String color = "white";private boolean filled;private java.util.Date dateCreated;/** Construct a default geometric object */protected GeometricObject() {}/** Construct a geometric object with color and filled value */protected GeometricObject(String color, boolean filled) {dateCreated = new java.util.Date();this.color = color;this.filled = filled;}/** Return color */public String getColor() {return color;}/** Set a new color */public void setColor(String color) {this.color = color;}/** Return filled. Since filled is boolean ,* the get method is named isFilled */public boolean isFilled() {return filled;}/** Set a new filled */public void setFilled(boolean filled) {this.filled = filled;}/** Get dateCreated */public java.util.Date getDateCreated() {return dateCreated;}@Overridepublic String toString() {return "created on " + dateCreated + "\ncolor: " + color +" and filled: " + filled;}/** Abstract method getArea */public abstract double getArea();/** Abstract method getPerimeter */public abstract double getPerimeter();}The Triangle class contains:Three double data fields named side1, side2, and side3A default constructor that creates a triangle with three sides of length 1.0A constructor that creates a triangle with specified values for side1, side2, and side3Accessor methods for all three data fieldsA method called getArea() that returns the area of a triangleA method named getPerimeter() that returns the perimeter of the triangleA method named toString() that returns the string description of the triangle in the following format: "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;Test your Triangle class in a Drive program (in the same file) that prompts the user to enter the three sides of the triangle, the color, and whether or not the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties. Then, it should display the area, perimeter, color, and filled value .

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());

 }

}