**13.1(Triangle类)设计一个继承了抽象类GeometriObject的新的Triangle类。绘制Triangle类和GeometriObject类的UML图并实现Triangle类。编写一个测试程序,提示用户输入三角形的三条边、颜色以及一个表明该三角形是否填充的布尔值。程序应该使用这些边来创建一个Triangle对象,并根据用户的输入来设置颜色和是否填充的属性。程序应该显示面积、周长、颜色以及表明是否被填充的真或假的值。 **13.1(Triangle class)Design a new triangle class that inherits the abstract class geometriobject. Draw the UML diagram of triangle class and geometriobject class and implement triangle class. Write a test program, prompt the user to input three sides of the triangle, color and a Boolean value indicating whether the triangle is filled or not. The program should use these edges to create a triangle object and set color and fill properties based on user input. The program should display area, perimeter, color, and true or false values that indicate whether they are filled.
参考代码:
package chapter13;
import java.util.Scanner;
public class Code_01 {
public staticvoidmain(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入三角形的三条边:");double side1=input.nextDouble();double side2=input.nextDouble();double side3=input.nextDouble();
System.out.print("请输入三角形的颜色:");
String color = input.next();
System.out.print("该三角形是否填充(true/false):");
boolean filled = input.nextBoolean();
GeometricObject object=new Triangle(side1,side2,side3,color,filled);
System.out.println("该三角形的面积为:"+object.getArea());
System.out.println("该三角形的周长为:"+object.getPerimeter());
System.out.println(object.toString());}}
class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle(){}
public Triangle(double side1,double side2,double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;}
public Triangle(double side1,double side2,double side3, String color, boolean filled){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;setColor(color);setFilled(filled);}
public doublegetSide1(){return side1;}
public doublegetSide2(){return side2;}
public doublegetSide3(){return side3;}
public voidsetSide1(double side1){
this.side1 = side1;}
public voidsetSide2(double side2){
this.side2 = side2;}
public voidsetSide3(double side3){
this.side3 = side3;}
public doublegetArea(){double s =(side1 + side2 + side3)/2;double area = Math.pow(s *(s - side1)*(s - side2)*(s - side3),0.5);return area;}
public doublegetPerimeter(){return side1 + side2 + side3;}}
abstract class GeometricObject {
private String color ="white";
private boolean filled;
private java.util.Date dateCreated;/** Construct a default geometric object */
protected GeometricObject(){
dateCreated = new java.util.Date();}/** 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 voidsetColor(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 voidsetFilled(boolean filled){
this.filled = filled;}/** Get dateCreated */
public java.util.Date getDateCreated(){return dateCreated;}
@Override
public String toString(){return"created on "+ dateCreated +"\ncolor: "+ color +" and filled: "+ filled;}/** Abstract method getArea */
public abstract doublegetArea();/** Abstract method getPerimeter */
public abstract doublegetPerimeter();}
结果显示:
请输入三角形的三条边:345
请输入三角形的颜色:red
该三角形是否填充(true/false):true
该三角形的面积为:6.0
该三角形的周长为:12.0
created on Sat Nov 0716:15:37 CST 2020
color: red and filled: true
Process finished with exit code 0