4.17作业

定义一个矩形类Rectangle:(知识点:对象的创建和使用)

1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。

2 2个属性:长length、宽width

3 创建一个Rectangle对象,并输出相关信息

 

 1 package chap;
 2 public class Rectangle {
 3        int length;
 4        int width;
 5     
 6         public void getArea() {
 7             System.out.println(length*width);
 8         }
 9         public void getPer() {
10             System.out.println((length+width)*2);
11         }
12         public void showAll() {
13             System.out.println("长"+lenght);
14             System.out.println("宽"+width);
15             System.out.println("面积:");
16         getArea();
17         System.out.println("周长");
18         getPer();
19         }
20 }  
21 
22 package chap;
23 
24 import java.util.Scanner;
25 
26 public class test1 {
27 
28     public static void main(String[] args) {
29         // TODO Auto-generated method stub
30      Rectangle R= new Rectangle();
31      Scanner input =new Scanner(System. in);
32      System.out.println("请输入长");
33      R.lenght=input.nextInt();
34      System.out.println("请输入宽");
35      R.width=input.nextInt();
36      R.showAll();
37      
38     }
39 
40 }

 

posted @ 2020-04-20 10:29  好学生!  阅读(148)  评论(0编辑  收藏  举报