第七周作业

作业
定义一个矩形类Rectangle:(知识点:对象的创建和使用)
1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2 有2个属性:长length、宽width
3 创建一个Rectangle对象,并输出相关信息

package com.a02;

public class Rectangle {
    int length;//
    int width;//

    public void showAll() {
        System.out.println("长是" + length + ",宽是" + width);
        System.out.println("可得面积是:");
        getArea();
        System.out.println("周长是:");
        getPer();

    }

    // 求面积
    public void getArea() {
        System.out.println(length * width);
    }

    // 求周长
    public void getPer() {
        System.out.println((length + width) * 2);
    }

}
package com.a02;

import java.util.*;

public class Rectangele2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Rectangle r = new Rectangle();

        Scanner input = new Scanner(System.in);
        System.out.println("输入长为:");
        r.length = input.nextInt();
        System.out.println("输入宽为:");
        r.width = input.nextInt();

        r.showAll();

    }
}

原文地址:https://www.cnblogs.com/hyonf/p/12740688.html