第十一章第一题(Triangle类)(Triangle class)

第十一章第一题(Triangle类)(Triangle class)

  • 11.1(Triangle类)设计一个名为Triangle的类来继承GeometricObject类。该类包括:

    • 三个名为side1、side2和side3的double类型数据域表示这个三角形的三条边,他们的默认值是1.0。
    • 一个无参构造方法,创建一个默认的三角形。
    • 一个创建指定side1、side2和side3值的三角形的构造方法。
    • 所有三个数据域的访问器方法。
    • 一个名为getArea()的方法返回三角形的面积。
    • 一个名为getPerimeter()的方法返回三角形的周长。
    • 一个名为toString()的方法返回三角形的字符串描述。

    计算三角形面积的公式参照编程习题2.19。toString()方法的实现如下所示:
    return "Create on " + super.getDateCreated() + "\ncolor: " + super.getColor() + "\nArea: " + this.getArea() + "\nPerimeter: " + this.getPerimeter();
    画出Triangle类和GeometricObject类的UML图,并实现这些类。编写一个测试程序,提示用户输入三角形的三条边、颜色以及一个Boolean值表明该三角形是否填充。程序需要根据输入创建一个具有指定边的三角形,并设置color和filled属性。程序需要显示面积、周长、颜色以及表明是否填充的真或假值。
    11.1(Triangle class)Design a class named triangle to inherit the geometricobject class. This category includes:

    • Three double data fields named side1, side2, and side3 represent the three sides of the triangle, and their default value is 1.0.
    • A nonparametric construction method that creates a default triangle.
    • A construction method for creating triangles with specified side1, side2, and side3 values.
    • Accessor methods for all three data domains.
    • A method called getarea() returns the area of the triangle.
    • A method called getperimeter () returns the perimeter of the triangle.
    • A method called tostring() returns the string description of the triangle.

    The formula for calculating the area of triangle is referred to programming exercise 2.19. The tostring() method is implemented as follows:
    return "Create on " + super.getDateCreated () + "\ncolor: " + super.getColor () + "\nArea: " + this.getArea () + "\nPerimeter: " + this.getPerimeter ();
    Draw the UML diagram of triangle class and geometricobject class, and implement these classes. Write a test program, prompt the user to input three sides of the triangle, color and a Boolean value to indicate whether the triangle is filled or not. The program needs to create a triangle with the specified edge according to the input, and set the color filled property. The program needs to display area, perimeter, color, and true or false values indicating whether to fill。

  • 参考代码:

package chapter11;

public class Code_01 {
    public static void main(String[] args) {
        Triangle triangle = new Triangle();
        System.out.println(triangle.toString());
    }
}
class SimpleGeometricObject {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    /** Construct a default geometric object */
    public SimpleGeometricObject() {
        dateCreated = new java.util.Date();
    }

    /** Construct a geometric object with the specified color
     *  and filled value */
    public SimpleGeometricObject(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,
     its 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;
    }

    /** Return a string representation of this object */
    public String toString() {
        return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
    }
}

class Triangle extends SimpleGeometricObject{
    private double side1 = 1.0;
    private double side2 = 1.0;
    private double side3 = 1.0;

    public Triangle(){
        super();
        side1 = side2 = side3 = 1.0;
    }
    public Triangle(double side1,double side2,double side3){
        super();
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
    public double getSide1(){
        return side1;
    }
    public double getSide2(){
        return side2;
    }
    public double getSide3(){
        return side3;
    }
    public double getArea(){
        double p = (side1 + side2 + side3) / 2;
        return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
    }
    public double getPerimeter(){
        return side1 + side2 + side3;
    }

    @Override
    public String toString(){
        return "color: " + super.getColor() + "\nArea: " + this.getArea() + "\nPerimeter: " + this.getPerimeter() + "\nisFilled: " + this.isFilled();

    }
}

  • 结果显示:
color: white
Area: 0.4330127018922193
Perimeter: 3.0
isFilled: false

Process finished with exit code 0


在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值