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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int area = 0;
        int Perimeter = 0;
        Rectangle[] r = new Rectangle[2];
        for (int i = 0; i < 2; i++) {
            int length = sc.nextInt();
            int width = sc.nextInt();
            r[i] = new Rectangle(length,width);
            //计算面积
            area+=r[i].getArea();
            //计算周长
            Perimeter+=r[i].getPerimeter();
        }
        Circle[] c = new Circle[2];
        for (int i = 0; i < 2; i++) {
            int radius = sc.nextInt();
            c[i] = new Circle(radius);
            //计算面积
            area+=c[i].getArea();
            //计算周长
            Perimeter+=c[i].getPerimeter();

    }
    System.out.println(Perimeter);
    System.out.println(area);
    System.out.println(Arrays.deepToString(r));
    System.out.println(Arrays.deepToString(c));

}

}

class Rectangle{
    private int width;
    private int length;

public Rectangle() {
}

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

public int getWidth() {
    return width;
}

public void setWidth(int width) {
    this.width = width;
}

public int getLength() {
    return length;
}

public void setLength(int length) {
    this.length = length;
}

//长方形的求周长方法
public int getPerimeter(){
    return (this.length+this.width)*2;
}
//长方形的求面积方法
public int getArea(){
    return this.length*this.width;
}

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

}
class Circle{
    private int radius;

public Circle() {
}

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

public int getRadius() {
    return radius;
}

public void setRadius(int radius) {
    this.radius = radius;
}
//圆形的求面积方法
public int getArea(){
    return (int)(this.radius*this.radius*Math.PI);
}

//圆形的求周长方法
public int getPerimeter(){
    return (int)(this.radius*2*Math.PI);
}

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

}

解析:

难度其实一般般,我都能写出来,就已经可以证明题目的简单了,主要就是考验对类的理解,

可以用多态,继承的方法来做,比如建一个graph的父类,在里面写两求面积和周长的抽象方法,让那两类继承就行了,不过我比较懒,觉得这样写比较麻烦。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值