第七周作业

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

package chap1;
public class Rectangle {
       int l;
       int w;
    
        public void getArea() {
            System.out.println(l*w);
        }
        public void getPer() {
            System.out.println((l+w)*2);
        }
        public void showAll() {
            System.out.println("长"+l);
            System.out.println("宽"+w);
            System.out.println("面积:");
        getArea();
        System.out.println("周长");
        getPer();
        }
}        

  

package chap1;

import java.util.Scanner;

public class test1 {

    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.l=input.nextInt();
     System.out.println("请输入宽");
     R.w=input.nextInt();
     R.showAll();
     
    }

}

  

posted @ 2020-04-22 16:50  只是马文龙  阅读(57)  评论(0编辑  收藏  举报