Skip to content

Commit f80749a

Browse files
committed
constructors challenges
1 parent 27660a4 commit f80749a

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
- [Invariants](./constructors/invariants.md)
291291
- [Overloads](./constructors/overloads.md)
292292
- [Delegation](./constructors/delegation.md)
293+
- [Challenges](./constructors/challenges.md)
293294
- [Global Fields](./global_fields.md)
294295
- [Default Values](./global_fields/default_values.md)
295296
- [Final Fields](./global_fields/final_fields.md)

src/constructors/challenges.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Challenges
2+
3+
- Try to use only the information given up to this point in this book.
4+
- Try not to give up until you've given it a solid attempt
5+
6+
## Challenge 1.
7+
8+
Write a `Shoe` class. Give each shoe a `name` field and a `quality` field.
9+
Have these fields be initialized inside of a constructor.
10+
11+
```java,editable
12+
enum Quality {
13+
SUPA_FINE,
14+
FINE,
15+
SUB_FINE
16+
}
17+
18+
// CODE HERE
19+
20+
void main() {
21+
Shoe nike = new Shoe("Nikes", Quality.SUB_FINE);
22+
System.out.println(
23+
"SHOE: " + nike.name + ", " + nike.quality
24+
);
25+
26+
27+
Shoe moccasin = new Shoe("Moccasins", Quality.SUPA_FINE);
28+
System.out.println(
29+
"SHOE: " + moccasin.name + ", " + moccasin.quality
30+
);
31+
}
32+
```
33+
34+
## Challenge 2.
35+
36+
Add a new `price` field to the `Shoe` class you wrote above.
37+
38+
Add a new constructor which accepts a third argument to set the `price`.
39+
Keep the old two argument constructor around as well. When that one is
40+
used set `price` to `null`.
41+
42+
Hint: Use `Double` to represent a nullable price.
43+
44+
```java,editable
45+
enum Quality {
46+
SUPA_FINE,
47+
FINE,
48+
SUB_FINE
49+
}
50+
51+
// CODE HERE
52+
53+
void main() {
54+
Shoe jays = new Shoe("Air Jordans", Quality.FINE, 130.0);
55+
System.out.println(
56+
"SHOE: " + jays.name + ", " + jays.quality + ", $" + jays.price
57+
);
58+
59+
Shoe nike = new Shoe("Nikes", Quality.SUB_FINE, 25);
60+
System.out.println(
61+
"SHOE: " + nike.name + ", " + nike.quality + ", $" + jays.price
62+
);
63+
64+
65+
Shoe moccasin = new Shoe("Moccasins", Quality.SUPA_FINE);
66+
System.out.println(
67+
"SHOE: " + moccasin.name + ", " + moccasin.quality + ", $" + jays.price
68+
);
69+
}
70+
```
71+
72+
## Challenge 3.
73+
74+
Alter the `Shoe` class so that the `price` field is final.
75+
Alter its constructors so that if they are given a negative
76+
value they throw an exception instead of finishing normally.
77+
78+
Keep in mind that while `null` is allowed (you might not know the price)
79+
a negative number wouldn't be. Nobody is paying you to take their shoes.[^holes]
80+
81+
```java,editable
82+
enum Quality {
83+
SUPA_FINE,
84+
FINE,
85+
SUB_FINE
86+
}
87+
88+
// CODE HERE
89+
90+
void main() {
91+
Shoe jays = new Shoe("Air Jordans", Quality.FINE, 130.0);
92+
System.out.println(
93+
"SHOE: " + jays.name + ", " + jays.quality + ", $" + jays.price
94+
);
95+
96+
Shoe nike = new Shoe("Nikes", Quality.SUB_FINE, 25);
97+
System.out.println(
98+
"SHOE: " + nike.name + ", " + nike.quality + ", $" + jays.price
99+
);
100+
101+
102+
Shoe moccasin = new Shoe("Moccasins", Quality.SUPA_FINE);
103+
System.out.println(
104+
"SHOE: " + moccasin.name + ", " + moccasin.quality + ", $" + jays.price
105+
);
106+
107+
Shoe shouldCrash = new Shoe("Base Ball Cleats", Quality.SUPA_FINE, -10);
108+
}
109+
```
110+
111+
[^holes]: If they do, you are going to dig holes in the desert in search of treasure that rightfully belongs
112+
to you.
113+
114+
## Challenge 4.
115+
116+
If you haven't yet, rewrite your `Shoe` constructors so only one of them actually sets fields
117+
and the other just delegates to that one.
118+
119+
```java,editable
120+
enum Quality {
121+
SUPA_FINE,
122+
FINE,
123+
SUB_FINE
124+
}
125+
126+
// CODE HERE
127+
128+
void main() {
129+
Shoe jays = new Shoe("Air Jordans", Quality.FINE, 130.0);
130+
System.out.println(
131+
"SHOE: " + jays.name + ", " + jays.quality + ", $" + jays.price
132+
);
133+
134+
Shoe nike = new Shoe("Nikes", Quality.SUB_FINE, 25);
135+
System.out.println(
136+
"SHOE: " + nike.name + ", " + nike.quality + ", $" + jays.price
137+
);
138+
139+
140+
Shoe moccasin = new Shoe("Moccasins", Quality.SUPA_FINE);
141+
System.out.println(
142+
"SHOE: " + moccasin.name + ", " + moccasin.quality + ", $" + jays.price
143+
);
144+
145+
Shoe shouldCrash = new Shoe("Base Ball Cleats", Quality.SUPA_FINE, -10);
146+
}
147+
```

0 commit comments

Comments
 (0)