第12周作业

第一题

Point类

 

 1 package works;
 2 
 3 public class Point {
 4     
 5     public int x;
 6     public int y;
 7     
 8     public Point() {
 9         
10     }
11     public Point(int x,int y) {
12         
13         this.x=x;
14         this.y=y;
15         
16     }
17     
18     public void move (int dx,int dy) {
19     
20         System.out.println("("+(x+=dx)+","+(y+=dy)+")");
21     }
22 
23 }

 

 

 

运行

 1 package works;
 2 
 3 public class test {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Point p1=new Point(1,2);
 8         p1.move(2, 3);
 9     }
10 
11 }

 

 

第二题

Rectangle类

 

 1 package works;
 2 
 3 public class Rectangle {
 4     
 5     public int length;
 6     public int width;
 7     
 8     public Rectangle() {
 9         
10     }
11     
12     public Rectangle(int a,int b) {
13         this.length=a;
14         this.width=b;
15     }
16     
17     public int getArea(int a,int b){
18         return a*b;
19     }
20     public int getPer(int a,int b) {
21         return (a+b)*2;
22     }
23     public void showAll() {
24         
25         System.out.println("面积"+getArea(length,width));
26         System.out.println("周长"+getPer(length,width));
27         
28     }
29 
30 }

 

运行

 

 1 package works;
 2 
 3 public class test {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Rectangle s1=new Rectangle(2,2);
 8         s1.showAll();
 9     }
10 
11 }

 

 

 

第三题

 

notebook类

 

 

 1 package works;
 2 
 3 public class notebook {
 4     
 5     public char color;
 6     public int cpu;
 7     
 8     public notebook() {
 9         
10     }
11     
12     public notebook(char a,int b) {
13         this.color=a;
14         this.cpu=b;
15     }
16     
17     public void show() {
18         System.out.println(color+"\n"+cpu);
19     }
20 
21 }

 

 

 

运行

 

package works;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        notebook huawei=new notebook('r',121);
        huawei.show();
    }

}

 

 

 

 

posted @ 2021-05-25 20:07  计算机1903孙铭泽  阅读(43)  评论(0编辑  收藏  举报