4.18 第七周作业

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

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

原文地址:https://www.cnblogs.com/gwz-1314/p/12737719.html