4.30 第九周上机练习

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

 1 package text;
 2 
 3 public class Point {
 4     int x;
 5     int y;
 6 
 7     Point() {
 8         System.out.println(x);
 9         System.out.println(y);
10     }
11 
12     Point(int x, int y) {
13         this.x = x;
14         this.y = y;
15         System.out.println(x);
16         System.out.println(y);
17     }
18 
19     public void movePoint(int p1, int p2) {
20         p1 = x + 10;
21         p2 = y + 10;
22         System.out.println("p1的坐标为:" + p1 + "\n" + "p2的坐标为:" + p2);
23     }
24 }
25 测试
26 package text;
27 
28 public class Point2 {
29 
30     public static void main(String[] args) {
31         // TODO Auto-generated method stub
32         Point p = new Point();
33         p.x = 15;
34         p.y = 20;
35         p.movePoint(p.x, p.y);
36     }
37 
38 }

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

 1 package text;
 2 
 3 public class Rectangle {
 4     private int length;
 5     private int width;
 6 
 7     // 构造方法
 8     Rectangle(int width, int length) {
 9         this.width = width;
10         this.length = length;
11     }
12 
13     // 控制台显示长,宽,高,面积,周长
14     public void showAll() {
15         System.out.println("长:" + length);
16         System.out.println("宽:" + width);
17         System.out.println("面积:" + getArea());
18         System.out.println("周长:" + getPer());
19     }
20 
21     // 求面积
22     public int getArea() {
23         return length * width;
24     }
25 
26     // 求周长
27     public int getPer() {
28         return (length + width) * 2;
29     }
30 }
31 
32 
33 
34 测试
35 package text;
36 
37 public class Rectangle1 {
38 
39     public static void main(String[] args) {
40         // TODO Auto-generated method stub
41         Rectangle r = new Rectangle(5, 7);
42         r.showAll();
43     }
44 
45 }

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

 1 package text;
 2 
 3 public class Book {
 4     char color;
 5     int model;
 6 
 7     Book() {
 8 
 9     }
10 
11     Book(char color, int model) {
12         this.color = color;
13         this.model = model;
14     }
15 
16     void show() {
17         System.out.println("颜色:" + color + "  型号:" + model);
18     }
19 }
20 测试
21 package text;
22 
23 public class Demo2 {
24 
25     public static void main(String[] args) {
26         // TODO Auto-generated method stub
27         Book b = new Book('银', 2020);
28         b.show();
29     }
30 }

 

6、定义两个类,描述如下: [必做题]
• 6.1定义一个人类Person:
• 6.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
• 6.1.2有三个属性:名字、身高、年龄
• 6.1.3通过构造方法,分别给三个属性赋值
• 6.2定义一个Constructor类:
• 6.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
• 6.2.2分别调用对象的sayHello()方法。

 1 package text;
 2 
 3 public class Person {
 4     String name;
 5     double height;
 6     int age;
 7 
 8     public void sayHello() {
 9         System.out.println("hello,my name is" + name);
10     }
11 
12     public void goDown(String name, int age, double height) {
13         this.name = name;
14         this.age = age;
15         this.height = height;
16     }
17 
18 }
19 测试
20 package text;
21 
22 public class Constructor {
23 
24     public static void main(String[] args) {
25         // TODO Auto-generated method stub
26         Person p1 = new Person();
27         p1.goDown("zhangsan", 33, 1.74);
28         p1.sayHello();
29         Person p2 = new Person();
30         p2.goDown("lishi", 44, 1.74);
31         p2.sayHello();
32     }
33 }

猜你喜欢

转载自www.cnblogs.com/gwz-1314/p/12808127.html