Design a class named Triangle that extends GeometricObject . Theclass contains:
■ Three double data fieldsnamed side1 , side2 , and side3 with default
values 1.0 to denote three sides of thetriangle.
■ A no-arg constructor thatcreates a default triangle.
■ A constructor that creates atriangle with the specified side1 ,side2 , and
side3 .
■ The accessor methods for allthree data fields.
■ A method named getArea() that returns the area of thistriangle.
■ A method named getPerimeter() that returns the perimeter ofthis triangle.
■ A method named toString() that returns a string descriptionfor the triangle.
For the formula to compute the area of atriangle, see Programming Exercise 2.19.
The toString() method is implemented asfollows:
return "Triangle: side1 = " +side1 + " side2 = " + side2 +
" side3 = " + side3;
Write a test program that prompts the userto enter three sides of the triangle, a color, and a Boolean value to indicatewhether the triangle is filled. The program should create a Triangle object with these sides and set thecolor and filled properties using theinput. The program should display the area, perimeter, color, and true or falseto indicate whether it is filled or not.
public class GeometricObject {
private String color = " white ";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject() {
dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(String filled1) {
if (filled1.equals(false)) {
this.filled = false;
}else {
this.filled = true;
}
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "Created on " + dateCreated + "\n color: " + color + " and filled ";
}
}
public class Triangle extends GeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public Triangle() {
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double setSide1() {
return side1;
}
public double setSide2() {
return side2;
}
public double setSide3() {
return side3;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public void setSide3(double side3) {
this.side3 = side2;
}
public double getArea() {
return (side1 + side2 + side3) / 2;
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String toString() {
return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2
+ " Side 3 = " + side3;
}
}
import java.util.Scanner;
public class TestTriangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter three sides of the Triangle");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
System.out.println("Enter the color of the Triangle");
String color = input.next();
System.out.println(" Is the Triangle filled? Reply with 'true' or 'false' ");
String filled = input.next();
Triangle t1 = new Triangle(); //创建默认三角形
System.out.println("默认三角形描述: " + t1.toString());
System.out.println("默认三角形面积 " + t1.getArea());
System.out.println("默认三角形周长 " + t1.getPerimeter());
System.out.println("默认三角形填充颜色 " + t1.getColor());
System.out.println("默认三角形是否填充" + t1.isFilled() );
System.out.println("默认三角形是创建时间" + t1.getDateCreated());
Triangle t2 = new Triangle(side1, side2, side3); // 创建有参数的三角形
System.out.println("============================");
t2.setColor(color);
t2.setFilled(filled);
System.out.println("有参三角形描述: " + t2.toString());
System.out.println("有参三角形面积 " + t2.getArea());
System.out.println("有参三角形周长 " + t2.getPerimeter());
System.out.println("有参三角形填充颜色 " + t2.getColor());
System.out.println("有参三角形是否填充" + t2.isFilled() );
System.out.println("有参三角形是创建时间" + t2.getDateCreated());
}
}