jmu-Java-03面向对象基础-03-形状

1. 定义长方形类与圆形类Circle

长方形类-类名:Rectangleprivate属性:int width,length
圆形类-类名:Circleprivate属性:int radius

编写构造函数:
带参构造函数:Rectangle(width, length),Circle(radius)

编写方法:
public int getPerimeter(),求周长。
public int getArea(),求面积。
toString方法,使用Eclipse自动生成。

注意:

  1. 计算圆形的面积与周长,使用Math.PI
  2. 求周长和面积时,应先计算出其值(带小数位),然后强制转换为int再返回。

2. main方法

  • 输入2行长与宽,创建两个Rectangle对象放入相应的数组。
  • 输入2行半径,创建两个Circle对象放入相应的数组。
  • 输出1:上面2个数组中的所有对象的周长加总。
  • 输出2:上面2个数组中的所有对象的面积加总。
  • 最后需使用Arrays.deepToString分别输出上面建立的Rectangle数组与Circle数组

思考: 如果初次做该题会发现代码冗余严重。使用继承、多态思想可以大幅简化上述代码。

输入样例:

1 2
3 4
7
1

输出样例:

69
170
[Rectangle [width=1, length=2], Rectangle [width=3, length=4]]
[Circle [radius=7], Circle [radius=1]]


答案

import java.util.*;

class Circle{
    private int radius;

    @Override
    public String toString() {
        return "Circle [" +
                "radius=" + radius +
                ']';
    }

    Circle(int _radius){
        radius = _radius;
    }
    public int getPerimeter(){
        return (int)(2 * Math.PI * radius);
    }
    public int getArea(){
        return (int)(Math.PI * radius * radius);
    }
}

class Rectangle{
    private int width,length;

    @Override
    public String toString() {
        return "Rectangle [" +
                "width=" + width +
                ", length=" + length +
                ']';
    }

    Rectangle(int _width, int _length){
        width = _width;
        length = _length;
    }
    public int getPerimeter(){
        return 2 * (width + length);
    }
    public int getArea(){
        return width * length;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Rectangle[] rectangles = new Rectangle[2];
        Circle[] circles = new Circle[2];
        int allPerimeter = 0, allArea = 0;
        for (int i = 0; i < 2; i++) {
            int w = scanner.nextInt();
            int l = scanner.nextInt();
            rectangles[i] = new Rectangle(w,l);
            allPerimeter += rectangles[i].getPerimeter();
            allArea += rectangles[i].getArea();
        }
        for (int i = 0; i < 2; i++) {
            int r = scanner.nextInt();
            circles[i] = new Circle(r);
            allPerimeter += circles[i].getPerimeter();
            allArea += circles[i].getArea();
        }
        System.out.println(allPerimeter);
        System.out.println(allArea);
        System.out.println(Arrays.deepToString(rectangles));
        System.out.println(Arrays.deepToString(circles));
    }
}
iangle),并在每个类中添加特定的成员函数和成员变量,以实现对该类图形的基本操作。同时,为了保证类的一致性和可扩展性,还可以在 Plane 类中定义虚函数,使得派生类可以根据自身的特点来实现该函数。例如: ```c++ class Plane { public: virtual double area() const = 0; // 纯虚函数,用于计算图形面积 virtual double perimeter() const = 0; // 纯虚函数,用于计算图形周长 virtual void draw() const = 0; // 纯虚函数,用于绘制图形 }; class Rectangle : public Plane { public: Rectangle(double w, double h) : width(w), height(h) {} double area() const override { return width * height; } double perimeter() const override { return 2 * (width + height); } void draw() const override { /* 绘制长方形 */ } private: double width; double height; }; class Circle : public Plane { public: Circle(double r) : radius(r) {} double area() const override { return 3.14159 * radius * radius; } double perimeter() const override { return 2 * 3.14159 * radius; } void draw() const override { /* 绘制圆形 */ } private: double radius; }; class Triangle : public Plane { public: Triangle(double a, double b, double c) : sideA(a), sideB(b), sideC(c) {} double area() const override { double p = (sideA + sideB + sideC) / 2; return sqrt(p * (p - sideA) * (p - sideB) * (p - sideC)); } double perimeter() const override { return sideA + sideB + sideC; } void draw() const override { /* 绘制三角形 */ } private: double sideA, sideB, sideC; }; ``` 这样,我们就可以通过多态的方式,使用基类指针或引用来处理不同的图形对象,而无需关注具体对象的类型。例如: ```c++ Plane* shape = new Rectangle(2.0, 3.0); std::cout << "Rectangle area: " << shape->area() << std::endl; // 输出 6.0 shape = new Circle(4.0); std::cout << "Circle perimeter: " << shape->perimeter() << std::endl; // 输出 25.1327 ``` 通过这种方式,我们可以将不同的图形对象统一地处理,提高了代码的可复用性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值