Java第十二周作业

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

 1 package test;
 2 
 3 public class Point {
 4     int x;
 5     int y;
 6     public Point() {
 7         
 8     }
 9     public Point(int x, int y) {
10         this.x = x;
11         this.y = y;
12     }
13     public void movePoint(int dx, int dy){
14         x += dx;
15         y += dy;
16     }
17     public void show(){
18         System.out.print("X = " + x);
19         System.out.println("\tY = " + y);
20     }
21     
22 }
 1 package test;
 2 
 3 public class P1 {
 4     public static void main(String[] args) {
 5         Point p1 = new Point();
 6         Point p2 = new Point(3,2);
 7         p1.x = 2;
 8         p1.y = 3;
 9         p1.show();
10         p2.show();
11         System.out.println("-----移动后-----");
12         p1.movePoint(5,6);
13         p2.movePoint(9,2);
14         p1.show();
15         p2.show();
16     }
17     
18     
19 }

2、定义一个矩形类Rectangle: (知识点: 对象的创建和使用)[必做题]

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

2.2 有2个属性:长length、 宽width

2.3 通过构造方法Rectangle(int width, int length),分别给两个属性赋值

2.4 创建-个Rectangle对象, 并输出相关信息

 

 

 1 package test;
 2 
 3 public class Rectangle{
 4     
 5 int length;
 6 int width;
 7     
 8     public Rectangle(int length,int width){
 9         this.length=length;
10         this.width=width;    
11     }
12     public int getArea(){
13         return length*width;
14     }
15     public int getPer(){
16         return (length+width)*2;
17     }
18     public void show(){
19         System.out.println(getArea());
20         System.out.println(getPer());
21     }
22 }
1 package test;
2 
3 public class P1 {
4     public static void main(String[] args) {
5         Rectangle P=new Rectangle(1,5);
6         P.show();
7     }
8 }

 

3、定义一-个笔记本类,该类有颜色(char) 和cpu型号(int) 两个属性。[必做题]

3.1无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;

3.2 输出笔记本信息的方法

3.3 然后编写一-个测试类,测试笔记本类的各个方法。

 1 package test;
 2 
 3 public class com {
 4     int cpu;
 5     char color;
 6     
 7     public com(){
 8         
 9     }
10 
11       public com(char color, int cpu) {
12             this.color = color;
13             this.cpu = cpu;
14       }
15       public void show(){
16           System.out.println(color);
17           System.out.println(cpu);
18       }
19 }
 1 package test;
 2 
 3 public class duixing {
 4 
 5     public static void main(String[] args) {
 6     com p=new com('a',4);
 7     p.show();
 8     }
 9     
10 }

 

posted @ 2021-05-23 20:01  刘德广  阅读(43)  评论(0编辑  收藏  举报