# 7-2 jmu-Java-03面向对象基础-03-形状 (13 分)

1. 定义长方形类与圆形类Circle
长方形类-类名：Rectangle，private属性：int width,length
圆形类-类名：Circle，private属性:int radius

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

2. main方法

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.Arrays;
import java.util.Scanner;

class Rectangle {
private int width;
private int length;

public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}

public int getPerimeter() {
return (length + width) * 2;
}

public int getArea() {
return length * width;
}

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

}

class Circle{
private int radius;

public Circle(int radius){
this.radius = radius;
}

public int getPerimeter() {
return (int)(Math.PI * 2 * radius);
}

public int getArea() {
return (int)(Math.PI * radius * radius);
}

public String toString() {
return "Circle[" + "radius=" + radius + ']';
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Rectangle[] rectangle = new Rectangle[2];
rectangle[0] = new Rectangle(input.nextInt(), input.nextInt());
rectangle[1] = new Rectangle(input.nextInt(), input.nextInt());
Circle[] circle = new Circle[2];
circle[0] = new Circle(input.nextInt());
circle[1] = new Circle(input.nextInt());
System.out.println(rectangle[0].getPerimeter() + rectangle[1].getPerimeter() + circle[0].getPerimeter() + circle[1].getPerimeter());
System.out.println(rectangle[0].getArea() + rectangle[1].getArea() + circle[0].getArea() + circle[1].getArea());
System.out.println(Arrays.deepToString(rectangle));
System.out.println(Arrays.deepToString(circle));
}
}


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客