KaiquanMah commited on
Commit
9f2f0be
·
verified ·
1 Parent(s): a3fe381

this.attribute1 >= Class2.PUBLICSTATICATTRIBUTE2

Browse files
Week 6: Methods of OO Programming/04. Fish within permitted limits ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Once upon a time there was a fishing competition.
2
+ In the category 'FishMeasurements', the minimum and maximum length and weight of fish allowed in the competition are defined as category variables.
3
+
4
+ The 'Fish' class models a single fish.
5
+ The class has attributes length and weight.
6
+ For the Fish class, write the public method
7
+
8
+ public boolean allowed()
9
+
10
+ which returns true or false depending on whether the given Fish object is within the allowed limits in a competition.
11
+
12
+
13
+
14
+
15
+
16
+
17
+ import java.util.Random;
18
+ public class Test{
19
+ public static void main(String[] args){
20
+ final Random rnd = new Random();
21
+
22
+
23
+ System.out.println("Testing the method allowed...");
24
+
25
+ for (int i=0; i<10; i++){
26
+ Fish fish = new Fish(rnd.nextInt(80) + 10, rnd.nextInt(7) + rnd.nextDouble());
27
+ System.out.println("" + fish);
28
+ System.out.println(fish.allowed() ? "Allowed" : "Not allowed");
29
+ }
30
+
31
+ }
32
+ }
33
+
34
+
35
+ class FishMeasurements{
36
+ public static final int MINIMUM_LENGTH = 25;
37
+ public static final int MAXIMUM_LENGTH = 75;
38
+ public static final double MINIMUM_WEIGHT = 1.0;
39
+ public static final double MAXIMUM_WEIGHT = 5.0;
40
+ }
41
+
42
+
43
+ class Fish{
44
+ private int length;
45
+ private double weight;
46
+
47
+ public Fish(int length, double weight){
48
+ this.length = length;
49
+ this.weight = weight;
50
+ }
51
+
52
+
53
+
54
+ // ADD
55
+ public boolean allowed() {
56
+ boolean allowed = false;
57
+ if (this.length >= FishMeasurements.MINIMUM_LENGTH &&
58
+ this.length <= FishMeasurements.MAXIMUM_LENGTH &&
59
+ this.weight >= FishMeasurements.MINIMUM_WEIGHT &&
60
+ this.weight <= FishMeasurements.MAXIMUM_WEIGHT) {
61
+ allowed = true;
62
+ }
63
+
64
+ return allowed;
65
+ }
66
+
67
+
68
+
69
+
70
+ public String toString(){
71
+ return "Length:" + length + ", weight:" + weight;
72
+ }
73
+ }
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+ Testing the method allowed...
84
+ Length:22, weight:1.692058179594544
85
+ Not allowed
86
+ Length:26, weight:0.5860256329845314
87
+ Not allowed
88
+ Length:85, weight:3.3517439256862196
89
+ Not allowed
90
+ Length:33, weight:3.5979307720496427
91
+ Allowed
92
+ Length:77, weight:5.786405319523382
93
+ Not allowed
94
+ Length:35, weight:2.368043874796682
95
+ Allowed
96
+ Length:58, weight:4.67477898551802
97
+ Allowed
98
+ Length:50, weight:5.1839866609623915
99
+ Not allowed
100
+ Length:44, weight:3.8234901017754215
101
+ Allowed
102
+ Length:12, weight:1.6789547620765946
103
+ Not allowed
104
+
105
+
106
+
107
+