Chapter 11 The first question (Triangle class) (Triangle class)

Chapter 11 The first question (Triangle class) (Triangle class)

  • 11.1 (Triangle class) Design a class named Triangle to inherit the GeometricObject class. This category includes:

    • Three double type data fields named side1, side2 and side3 represent the three sides of this triangle, and their default value is 1.0.
    • A no-argument construction method creates a default triangle.
    • A construction method for creating triangles with specified side1, side2, and side3 values.
    • Accessor methods for all three data fields.
    • 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 a string description of the triangle.

    Refer to programming exercise 2.19 for the formula for calculating the area of ​​a triangle. The implementation of toString() method is as follows:
    return "Create on "+ super.getDateCreated() + "\ncolor:" + super.getColor() + "\nArea: "+ this.getArea() + "\nPerimeter: "+ this.getPerimeter();
    Draw the UML diagrams of the Triangle class and GeometricObject class, and implement these classes. Write a test program that prompts the user to enter the three sides of the triangle, the color, and a Boolean value to indicate whether the triangle is filled. The program needs to create a triangle with specified sides based on the input, and set the color Laker filled attribute. The program needs to display area, perimeter, color, and true or false values ​​indicating whether to fill.
    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。

  • Reference Code:

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

  • The results show that:
Create on Tue Oct 27 08:46:41 CST 2020
color: white
Area: 0.4330127018922193
Perimeter: 3.0

Process finished with exit code 0

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109303354