Design parent class and subclass to model geometric figures of circles and rectangles, JavaSE

1. Introduction

The Circle class inherits all the accessible data fields and methods of the GeometricObject class. In addition, it has a new data field radius, and get and set methods related to radius. It also includes getArea(), getPerimeter() and getDiameter() methods to return the area, perimeter, and diameter of the circle.
The Rectangle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has width and height data fields, and their related get and set methods. It also includes the getArea() and getPerimeter() methods to return the area and perimeter of the rectangle.

2. Code

Super Class GeometricObject

package com.zhuo.base;

import java.util.Date;

public class GeometricObject {
    
    
    private String color = "while";
    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 void setFilled(boolean filled) {
    
    
        this.filled = filled;
    }
    public boolean isfilled() {
    
    
        return filled;
    }

    public Date getDatecreated() {
    
    
        return datecreated;
    }

    @Override
    public String toString() {
    
    
        return "created on " + datecreated + "\ncolor: " + color + " and filled " + filled;
    }
}

Subclass Circle

package com.zhuo.base;

public class Circle extends GeometricObject{
    
    
    private int radius;
    public Circle() {
    
    

    }
    public Circle(int radius) {
    
    
        this.radius = radius;
    }
    public Circle(int radius, String color, boolean filled) {
    
    
        this.radius = radius;
        setColor(color);
        setFilled(filled);
    }

    public int getRadius() {
    
    
        return radius;
    }

    public void setRadius(int radius) {
    
    
        this.radius = radius;
    }
    public double getArea() {
    
    
        return radius * radius * Math.PI;
    }
    public double getDiameter() {
    
    
        return 2 * radius;
    }
    public double getPerimeter() {
    
    
        return 2 * radius * Math.PI;
    }
    public void printCircle() {
    
    
        System.out.println("The circle is created " + getDatecreated() + " and the radius is " + radius);
    }
}

Subclass Rectangle

package com.zhuo.base;

public class Rectangle extends GeometricObject {
    
    
    private int widht;
    private int height;
    public Rectangle() {
    
    

    }
    public Rectangle(int widht, int height) {
    
    
        this.widht = widht;
        this.height = height;
    }
    public Rectangle(int widht, int height, String color, Boolean filled) {
    
    
        this.widht = widht;
        this.height = height;
        setColor(color);
        setFilled(filled);
    }

    public int getWidht() {
    
    
        return widht;
    }

    public void setWidht(int widht) {
    
    
        this.widht = widht;
    }

    public int getHeight() {
    
    
        return height;
    }

    public void setHeight(int height) {
    
    
        this.height = height;
    }
    public double getArea() {
    
    
        return  widht * height;
    }
    public double getPerimeter() {
    
    
        return 2 * (widht + height);
    }
}

Test class TestCircleRectangle

package com.zhuo.base;

public class TestCircleGeometric {
    
    
    public static void main(String[] args) {
    
    
        Circle circle = new Circle(1);
        System.out.println("A circle " + circle.toString());
        System.out.println("The color is " + circle.getColor());
        System.out.println("The radius is " + circle.getRadius());
        System.out.println("The area is " + circle.getArea());
        System.out.println("The perimeter is " + circle.getPerimeter());
        Rectangle rectangle = new Rectangle(2, 4);
        System.out.println("B rectangle " +rectangle.toString());
        System.out.println("The color is " + rectangle.getColor());
        System.out.println("The area is " + rectangle.getArea());
        System.out.println("The perimeteris " + rectangle.getPerimeter() );
    }
}

Three. Running results

A circle created on Thu Feb 11 22:12:27 CST 2021
color: while and filled false
The color is while
The radius is 1
The area is 3.141592653589793
The perimeter is 6.283185307179586
B rectangle created on Thu Feb 11 22:12:27 CST 2021
color: while and filled false
The color is while
The area is 8.0
The perimeteris 12.0

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/weixin_42768634/article/details/113792198