Framist's Little House

◇ 自顶而下 - 面向未来 ◇

0%

【Java】上机作业报告 2

上机报告 2

java 第二次上机作业:1、作业内容:习题 3-2,3-3,3-9,4-2。其中 4-2 不需要提交程序,仅在上机报告中描述为什么会有这样的运行结果。2、报告格式:将报告和程序一起打压缩包发送到████████████邮箱,压缩包必须命名为“上机作业 2-学号 - 姓名”。注:如果压缩包命名格式错误,会漏统计!


3-2

按题目要求建立类如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class NewRectangle {
double width;
double height;

public NewRectangle() {
width = 0.0;
height = 0.0;
}

public NewRectangle(double w, double h) {
width = w;
height = h;
}

public double getArea() {
return width * height;
}

public double getPerimeter() {
return 2 * (width + height);
}
}

3-3

(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Point {
double x;
double y;

public Point() {
x = 0.0;
y = 0.0;
}

public Point(double x, double y) {
this.x = x;
this.y = y;
}

public double distance(Point p) {
return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2));
}

}

(2)(3)(4)

这里认为点在矩形的边上也属于内部

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
class NewRectangle {
double width;
double height;
Point coordinatesLD;

public NewRectangle() {
width = 0.0;
height = 0.0;
coordinatesLD = new Point();
}

public NewRectangle(double w, double h) {
width = w;
height = h;
coordinatesLD = new Point();
}

public NewRectangle(double w, double h, double x, double y) {
width = w;
height = h;
coordinatesLD = new Point(x, y);
}

public double getArea() {
return width * height;
}

public double getPerimeter() {
return 2 * (width + height);
}

boolean bPointIn(Point p) {
if (coordinatesLD.x <= p.x && coordinatesLD.y <= p.y && coordinatesLD.x + width >= p.x && coordinatesLD.y + height >= p.y) {
return true;
} else {
return false;
}
}


}

(5)

可以实现:

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
boolean bRectangleIn(NewRectangle R) {
Point p = R.coordinatesLD;
if (! bPointIn(p)) {
return false;
}
p = new Point(R.coordinatesLD.x + width, R.coordinatesLD.y + height);
if (! bPointIn(p)) {
return false;
}
return true;
}

boolean bRectangleOverlap(NewRectangle R) {
Point p = R.coordinatesLD;
if (bPointIn(p)) {
return true;
}
p = new Point(R.coordinatesLD.x + width, R.coordinatesLD.y + height);
if (bPointIn(p)) {
return true;
}
p = new Point(R.coordinatesLD.x, R.coordinatesLD.y + height);
if (bPointIn(p)) {
return true;
}
p = new Point(R.coordinatesLD.x + width , R.coordinatesLD.y);
if (bPointIn(p)) {
return true;
}
if (R.bRectangleOverlap(this)) {
return true;
}
return false;
}

综上

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

class NewRectangle {
double width;
double height;
Point coordinatesLD;

public NewRectangle() {
width = 0.0;
height = 0.0;
coordinatesLD = new Point();
}

public NewRectangle(double w, double h) {
width = w;
height = h;
coordinatesLD = new Point();
}

public NewRectangle(double w, double h, double x, double y) {
width = w;
height = h;
coordinatesLD = new Point(x, y);
}

public double getArea() {
return width * height;
}

public double getPerimeter() {
return 2 * (width + height);
}

boolean bPointIn(Point p) {
if (coordinatesLD.x <= p.x && coordinatesLD.y <= p.y && coordinatesLD.x + width >= p.x && coordinatesLD.y + height >= p.y) {
return true;
} else {
return false;
}
}

boolean bRectangleIn(NewRectangle R) {
Point p = R.coordinatesLD;
if (! bPointIn(p)) {
return false;
}
p = new Point(R.coordinatesLD.x + width, R.coordinatesLD.y + height);
if (! bPointIn(p)) {
return false;
}
return true;
}

boolean bRectangleOverlap(NewRectangle R) {
Point p = R.coordinatesLD;
if (bPointIn(p)) {
return true;
}
p = new Point(R.coordinatesLD.x + width, R.coordinatesLD.y + height);
if (bPointIn(p)) {
return true;
}
p = new Point(R.coordinatesLD.x, R.coordinatesLD.y + height);
if (bPointIn(p)) {
return true;
}
p = new Point(R.coordinatesLD.x + width , R.coordinatesLD.y);
if (bPointIn(p)) {
return true;
}
if (R.bRectangleOverlap(this)) {
return true;
}
return false;
}
}

class Point {
double x;
double y;

public Point() {
x = 0.0;
y = 0.0;
}

public Point(double x, double y) {
this.x = x;
this.y = y;
}

public double distance(Point p) {
return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2));
}

}


3-9

程序如下:

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
class Cycle {
void ride() {
System.out.println("Oh, I have " + this.wheel() + " wheels.");
if (this instanceof UniCycle) {
UniCycle a = (UniCycle) this;
a.balance();
}
if (this instanceof BiCycle) {
BiCycle a = (BiCycle) this;
a.balance();
}
}

int wheel() {
return 0;
}
}

class UniCycle extends Cycle {
int wheel() {
return 1;
}

void balance() {
System.out.println("Oh, I must be balance it!");
}
}

class BiCycle extends Cycle {
int wheel() {
return 2;
}

void balance() {
System.out.println("Oh, I must be balance it!");
}
}

class TriCycle extends Cycle {
int wheel() {
return 3;
}
}

如果以此运行:

1
2
3
4
5
6
7
8
public class homework_3_9 {

public static void main(String[] args) {
Cycle a = new BiCycle();
a.ride();
}

}

将产生如下输出:

1
2
Oh, I have 2 wheels.
Oh, I must be balance it!

instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符。

instanceof 是 Java 的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。

4-2

修改之后的输出为:

1
2
3
4
5
6
7
8
9
10
11
12
Create new C() in main
A(4)
A(5)
A(3)
C()
f1(2)
Create new C() in main
A(3)
C()
f1(2)
Exception in thread "main" java.lang.NullPointerException
at StaticInitialization.main(StaticInitialization.java:9)

比较修改之前的输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
A(1)
A(2)
B()
f1(1)
A(4)
A(5)
A(3)
C()
f1(2)
Create new C() in main
A(3)
C()
f1(2)
Create new C() in main
A(3)
C()
f1(2)
f2(1)
f3(1)

去掉new B()new C()

所以以下内容输出缺失:

原始输出 修改后的输出 解释
A(1)
A(2)
B()
f1(1)
A(4)
A(5)
A(3)
C()
f1(2)
去掉new B()new C(),运行前缺失对 B、C 类装载,所以此部分无输出
Create new C() in main
A(3)
C()
f1(2)
Create new C() in main
A(4)
A(5)
A(3)
C()
f1(2)
因为 C 此时是第一次装载,故初始化静态成员,输出A(4)A(5)
Create new C() in main
A(3)
C()
f1(2)
Create new C() in main
A(3)
C()
f1(2)
此部分相同
f2(1)
f3(1)
Exception in thread “main” java.lang.NullPointerException
at StaticInitialization.main(StaticInitialization.java:9)
bbcc没有引用的对象导致出错