-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathPenguin.java
More file actions
75 lines (66 loc) · 1.75 KB
/
Penguin.java
File metadata and controls
75 lines (66 loc) · 1.75 KB
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
public class Penguin extends Bird implements Swim {
private boolean swimming;
private int depths;
public Penguin(String name) {
super(name);
this.swimming = false;
this.depths = 0;
}
@Override
public String sing() {
return "Quack!";
}
public int getDepths() {
return depths;
}
public boolean isSwimming() {
return swimming;
}
/**
* dives, must be on ground
*/
@Override
public void dive() {
if (!this.swimming && this.depths == 0) {
this.swimming = true;
System.out.printf("%s dives into the water.%n", this.getName());
}
}
/**
* swim downward
* @param meters depths increase
* @return depth
*/
@Override
public int swimDown(int meters) {
if (this.swimming) {
this.depths = Math.min(this.depths + meters, 325);
System.out.printf("%s swims downward, depths : %d%n", this.getName(), this.depths);
}
return this.depths;
}
/**
* swin upward
* @param meters depths decrease
* @return depth
*/
@Override
public int swimUp(int meters) {
if (this.swimming) {
this.depths = Math.max(this.depths - meters, 0);
System.out.printf("%s swims upward, depths : %d%n", this.getName(), this.depths);
}
return this.depths;
}
/**
* get out of water, must be on surface
*/
@Override
public void getOut() {
if (this.swimming && this.depths == 0) {
System.out.printf("%s gets out of the water.%n", this.getName());
} else {
System.out.printf("%s is too deep, it can't get out.%n", this.getName());
}
}
}