欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-02-20 23:25:40
...

第十一章第一题(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 "Create on " + super.getDateCreated() + "\ncolor: " + super.getColor() + "\nArea: " + this.getArea() + "\nPerimeter: " + this.getPerimeter();
    }
}

  • 结果显示:
Create on Tue Oct 27 08:46:41 CST 2020
color: white
Area: 0.4330127018922193
Perimeter: 3.0

Process finished with exit code 0

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