Java第12次作业

1、定义一个点类Point, 包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point( intx0,y0),以及一个movePoint (int dx,intdy)方法实现点的位置移动,创建两个Point对象p1、p2, 分别调用movePoint方法后,打印pl和p2的坐标。[必作题]

 1 public class point {
 2     int x;
 3     int y;
 4  
 5     public point(int x0, int y0) {
 6         super();
 7         this.x = x0;
 8         this.y = y0;
 9     }
10  
11     public point() {
12         super();
13     }
14  
15     public String movePoint(int dx, int dy) {
16         x = x + dx;
17         y = y + dy;
18            return("x是"+x+",y是"+y)
19     }
20  
21     public static void main(String[] args) {
22         point p1 = new point(4, 5);
23         point p2 = new point(6, 4);
24         System.out.println(p1.movePoint(4, 5));
25         System.out.println(p2.movePoint(6, 4));
26     }
27 }

2.、定义一个矩形类Rectangle: (知识点: 对象的创建和使用)[必做题]
2.1 定义三个方法: getArea(求面积、getPer0求周长,showAll0分 别在控制台输出长、宽、面积周长。
2.2 有2个属性:长length、 宽width
2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值
2.4 创建-个Rectangle对象, 并输出相关信息

 1 package 第十二周作业;
 2 
 3 public class Rectangle {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         Rectangle a=new Rectangle(5,4);
11         a.showAll();
12 
13     }
14 
15       int length;
16       int width;
17       public int getArea(int length,int width){
18           return length*width;
19       }
20       public int getPer(int length,int width){
21           return (length+width)*2;
22       }
23       public void showAll(){
24          System.out.println("矩形的长是"+length+"矩形的宽是"+width+"周长是"+(length+width)*2+"面积是"+length*width); 
25           
26       }
27     public Rectangle(int length, int width) {
28         super();
29         this.length = length;
30         this.width = width;
31     }
32 }

3、定义一-个笔记本类,该类有颜色(char) 和cpu型号(int) 两个属性。[必做题]
3.1无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
3.2 输出笔记本信息的方法
3.3 然后编写一-个测试类,测试笔记本类的各个方法。

 1 package java11;
 2  
 3 public class bj {
 4     char coulour;
 5     int cpu;
 6     public void show(){
 7         System.out.println("笔记本颜色是"+coulour+"色,"+"型号是"+cpu);
 8     }
 9     public bj(char coulour,int cpu){
10         super();
11         this.coulour=coulour;
12         this.cpu=cpu;
13     }
14     public  bj(){
15         super();
16     }
17      
18      
19      
20      
21     public static void main(String[] args) {
22         // TODO Auto-generated method stub
23          bj c=new bj();
24             c.coulour='黑';
25             c.cpu=1;
26             c.show();
27             bj c1=new bj('白',2);
28             c1.show();
29  
30     }
31  
32 }

 

posted @ 2021-07-14 13:31  唐一南  阅读(52)  评论(0)    收藏  举报