第八次作业
1定义一个点类Point,包含2个成员变量x、y分 别表示x和y坐标,2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实 现点的位置移动,创建两个Point对象p1、p2,分 别调用movePoint方法后,打印p1和p2的坐标。[ 必作题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class Point { int x; int y; public Point ( int x0, int y0) { super (); this .x=x0; this .y=y0; } public void movepoint( int dx, int dy) { this .x+=dx; this .y+=dy; } public static void main(String[] args) { Point p1= new Point ( 2 , 7 ); p1.movepoint( 2 , 2 ); System.out.println( "p1x为" +p1.x+ "p1y为" +p1.y); Point p2= new Point ( 1 , 5 ); p2.movepoint( 1 , 1 ); System.out.println( "p2x为" +p2.x+ "p2y为" +p2.y); } } |
2.定义一个矩形类Rectangle:(知识点:对象的 创建和使用)[必做题] • 2.1 定义三个方法:getArea()求面积、getPer()求 周长,showAll()分别在控制台输出长、宽、面积 、周长。 • 2.2 有2个属性:长length、宽width • 2.3 通过构造方法Rectangle(int width, int length), 分别给两个属性赋值 • 2.4 创建一个Rectangle对象,并输出相关信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public class Rectangle { int length; int width; public Rectangle( int length, int width) { super (); this .length = length; this .width = width; } public int getAree( int length, int width) { return length * width; } public int getPer( int length, int width) { return (length + width) * 2 ; } public void showAll() { System.out.println( "长" + length + "宽" + width + "周长" + (length + width) * 2 + "面积" + (length * width)); } public static void main(String[] args) { Rectangle l = new Rectangle( 10 , 2 ); l.showAll(); } } |
3. 定义一个笔记本类,该类有颜色(char)和cpu 型号(int)两个属性。 [必做题] • 3.1 无参和有参的两个构造方法;有参构造方法可 以在创建对象的同时为每个属性赋值; • 3.2 输出笔记本信息的方法 • 3.3 然后编写一个测试类,测试笔记本类的各个 方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class Bjb { char color; int cpu; public Bjb() { super (); } public Bjb( char color, int cpu) { super (); this .color = color; this .cpu = cpu; } public void show() { System.out.println( "笔记本颜色是:" + color + "cpu型号是:" + cpu); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
; public class Text { public static void main(String[] args) { // TODO Auto-generated method stub Bjb b1 = new Bjb( '蓝' , 16 ); Bjb b2 = new Bjb( '黑' , 32 ); b1.show(); b2.show(); } } |
5、定义两个类,描述如下: [必做题] • 5.1定义一个人类Person: • 5.1.1定义一个方法sayHello(),可以向对方发出问 候语“hello,my name is XXX” • 5.1.2有三个属性:名字、身高、体重 • 5.2定义一个PersonCreate类: • 5.2.1创建两个对象,分别是zhangsan,33岁,1.73 ;lishi,44,1.74 • 5.2.2分别调用对象的sayHello()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package xingqiwuDemo04; public class Person { private String name; private int age; private double heiht; public void sayHello() { System.out.println( "Hello my name is" +name); } public Person(String name, int age, double heiht) { super (); this .name = name; this .age = age; this .heiht = heiht; } } package xingqiwuDemo04; public class PersonCreate { public static void main(String[] args) { // TODO Auto-generated method stub Person p1= new Person( "zhangsan" , 33 , 1.73 ); Person p2= new Person( "lishi" , 44 , 1.74 ); p1.sayHello(); p2.sayHello(); } } |
、设计2个类,要求如下:(知识点:类的继承 方法的覆
盖) [必做题]
- 2.1 定义一个汽车类Vehicle,
- 2.1.1 属性包括:汽车品牌brand(String类型)、颜色clr
(String类型)和速度speed(duble类型)。
- 2.1.2 至少提供一个有参的构造方法(要求品牌和颜色可以
初始化为任意值,但速度的初始值必须为0)。
- 2.1.3 为属性提供访问器方法。注意:汽车品牌一旦初始化
之后不能修改。
- 2.1.4 定义一个一般方法run(),用打印语句描述汽车奔跑的
功能
- 2.1.5 在main方法中创建一个品牌为―benz‖、颜色为―black‖
的汽车。
- 2.2 定义一个Vehicle类的子类轿车类Car,要求如下:
- 2.2.1 轿车有自己的属性载人数lader(int 类型)。
- 2.2.2 提供该类初始化属性的构造方法。
- 2.2.3 重新定义run(),用打印语句描述轿车奔跑的功能。
- 2.2.4 在main方法中创建一个品牌为―Hnda‖、颜色为―red‖
,载人数为2人的轿车。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package test; public class Vehicle { private String brand; private String color; private double speed; public Vehicle(String brand, String color, double speed) { super (); this .brand = brand; this .color = color; this .speed = speed = 0.0 ; } public String getBrand() { return brand; } public String getColor() { return color; } public void setColor(String color) { this .color = color; } public double getSpeed() { return speed; } public void setSpeed( double speed) { this .speed = speed; } public void run() { System.out.println( "品牌是" + brand + "颜色是" + color + "的汽车,正在以" + speed + "的速度奔跑" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package test; public class Car extends Vehicle{ int loader; public int getLoader() { return loader; } public void setLoader( int loader) { this .loader = loader; } public Car(String brand, String color, double speed, int loader) { super (brand, color, speed); this .loader = loader; } public void run() { System.out.println( "品牌是" + super .getBrand() + ",颜色是" + super .getColor() + ",载人数为" + loader + "的汽车,正在以" + super .getSpeed() + "速度奔跑" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package test; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Vehicle v = new Vehicle( "benz" , "black" , 0 ); v.setSpeed( 120 ); v.run(); Car c = new Car( "Honda" , "red" , 0 , 2 ); c.setSpeed( 130 ); c.run(); } } |