Skip to content

Commit b61d1e7

Browse files
committed
Challenges up through static methods
1 parent 3a345b1 commit b61d1e7

File tree

6 files changed

+314
-5
lines changed

6 files changed

+314
-5
lines changed

src/projects/data_visualization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ organic matter.
1111

1212
If you've ever seen a drawing of [a plague doctor](https://en.wikipedia.org/wiki/Plague_doctor)
1313
this is part of why they had that long nosed mask. They would fill it with nice smelling herbs
14-
in order to counter the miasma they though was causing disease.
14+
in order to counter the miasma they thought was causing disease.
1515

1616
This was basically entirely wrong in terms of the mechanics of disease spread. But it wasn't all bad. Staying
1717
away from dead bodies and wearing a mask around the sick aren't the worst ideas in the world.
@@ -34,7 +34,7 @@ plotted it, and saw a straight line then that is useful information.
3434

3535
Computers are uniquely suited to making visualizations. Modern data sets
3636
are often quite large and already stored on a computer. It is easier for a computer
37-
to turn that visualization into a chart or diagram of some kind than it is
37+
to turn that data into a chart or diagram of some kind than it is
3838
for a human to sit down and labor with a sheet of paper and a ruler.
3939

4040
## Your Goal
@@ -76,7 +76,7 @@ So if you write text like this into a file and give it a `.ppm` extension your c
7676
able to show it to you as an image.
7777

7878
You will still need to figure out how you are going to represent image data in your program,
79-
how to write to produce a file like this, and how to arrange the pixels. That should all be within your
79+
how to produce a file like this, and how to arrange the pixels. That should all be within your
8080
ability at this point though.[^believe]
8181

8282
## Future Goals

src/recur

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/static_fields/challenges.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,90 @@
11
# Challenges
2+
3+
Remember the rules for this are
4+
5+
- Try to use only the information given up to this point in this book.
6+
- Try not to give up until you've given it a solid attempt
7+
8+
## Challenge 1
9+
10+
Add a static field to this `Ogre` class that keeps track of
11+
how many Ogres have been made thus far in the program.[^threads]
12+
13+
```java,editable
14+
class Ogre {
15+
// CODE HERE
16+
17+
Ogre() {
18+
// CODE HERE
19+
}
20+
}
21+
22+
class Main {
23+
void main() {
24+
// 0
25+
IO.println(Ogre.NUMBER_OF_OGRES_MADE);
26+
27+
// 1
28+
Ogre o1 = new Ogre();
29+
IO.println(Ogre.NUMBER_OF_OGRES_MADE);
30+
31+
// 2
32+
Ogre o2 = new Ogre();
33+
IO.println(Ogre.NUMBER_OF_OGRES_MADE);
34+
35+
// 3
36+
Ogre o3 = new Ogre();
37+
IO.println(Ogre.NUMBER_OF_OGRES_MADE);
38+
39+
// 4
40+
Ogre o4 = new Ogre();
41+
IO.println(Ogre.NUMBER_OF_OGRES_MADE);
42+
43+
// 5
44+
Ogre o5 = new Ogre();
45+
IO.println(Ogre.NUMBER_OF_OGRES_MADE);
46+
}
47+
}
48+
```
49+
50+
## Challenge 2
51+
52+
Initialize the `PI` and `TAU` static final fields inside of a static initializer block.
53+
`TAU` should have twice the value of `PI`.
54+
55+
```java
56+
class Maths {
57+
static final double PI;
58+
static final double TAU;
59+
60+
static {
61+
// CODE HERE
62+
}
63+
}
64+
65+
class Main {
66+
void main() {
67+
IO.println(Maths.PI);
68+
IO.println(Maths.TAU);
69+
}
70+
}
71+
```
72+
73+
## Challenge 3
74+
75+
Rename the constants in the `Doug` class in the way that would
76+
be expected of you by others.
77+
78+
```java
79+
class Doug {
80+
static final String pattyMayonnaise = "Patty Mayonnaise";
81+
static final String sKeEtEr = "Mosquito 'Skeeter' Valentine";
82+
static final String mosquito_valentine = sKeEtEr;
83+
static final String rodgerMKlotz = "Rodger M. Klotz";
84+
static final String DOUG = "Douglas Yancy Funnie";
85+
}
86+
```
87+
88+
89+
[^threads]: Part of why mutable static fields are such a nightmare is that code like this would
90+
not work when you have to write "multi-threaded" Java code. There are things you can do with normal fields to sort of "make unsafe stuff safe in a way," but static fields are a lot harder to wrangle.

src/static_methods/challenges.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,131 @@
11
# Challenges
2+
3+
Remember the rules for this are
4+
5+
- Try to use only the information given up to this point in this book.
6+
- Try not to give up until you've given it a solid attempt
7+
8+
## Challenge 1
9+
10+
Change this position class such that it doesn't only have an `x` and a `y`. It should
11+
also have a `z`.
12+
13+
Then add the following static methods. `fromZ`, `fromXY`, `fromXZ`, and `fromYZ`.
14+
15+
```java,editable
16+
class Position {
17+
int x;
18+
int y;
19+
20+
Position(int x, int y) {
21+
this.x = x;
22+
this.y = y;
23+
}
24+
25+
static Position fromX(int x) {
26+
return new Position(x, 0);
27+
}
28+
29+
static Position fromY(int y) {
30+
return new Position(0, y);
31+
}
32+
}
33+
34+
class Main {
35+
void main() {
36+
var p1 = Position.fromXY(1, 2);
37+
IO.println("(" + p1.x + ", " + p1.y + ", " + p1.z + ")");
38+
39+
var p2 = Position.fromYZ(1, 2);
40+
IO.println("(" + p2.x + ", " + p2.y + ", " + p2.z + ")");
41+
42+
var p3 = Position.fromXZ(1, 2);
43+
IO.println("(" + p3.x + ", " + p3.y + ", " + p3.z + ")");
44+
45+
var p4 = Position.fromZ(4);
46+
IO.println("(" + p4.x + ", " + p4.y + ", " + p4.z + ")");
47+
}
48+
}
49+
```
50+
51+
## Challenge 2
52+
53+
Make a static method in the `Maths` class called `quadraticFormula`.
54+
It should take in an `a`, `b`, and `c` and run the [quadradic formula](https://en.wikipedia.org/wiki/Quadratic_formula) on those values.
55+
56+
This formula will give you either one "real" solution, two real solutions,
57+
or two imaginary solutions.
58+
59+
For now just throw an exception if the solutions are imaginary
60+
and treat having one solution as having two solutions with the same value.
61+
62+
```java,editable
63+
class Maths {
64+
// CODE HERE
65+
}
66+
67+
class Main {
68+
void main() {
69+
var result = Maths.quadraticFormula(6, -17, 12);
70+
IO.println(result.solutionOne);
71+
IO.println(result.solutionTwo);
72+
}
73+
}
74+
```
75+
76+
## Challenge 3
77+
78+
Update your code above to also communicate if a solution is imaginary.
79+
Use a boolean or an enum field on whatever class you wrote to hold both solutions
80+
for this.[^other]
81+
82+
[^other]: You will learn other ways later.
83+
84+
## Challenge 4
85+
86+
Make the following code compile. Do so first by changing one of the fields to `static`
87+
then by instead passing an extra argument to the `static` method.
88+
89+
```java,no_run
90+
class Keychain {
91+
final String[] keys;
92+
93+
Keychain(String[] keys) {
94+
this.keys = keys;
95+
}
96+
97+
boolean hasKey(String key) {
98+
for (int i = 0; i < keys.length; i++) {
99+
if (keys[i].equals(key)) {
100+
return true;
101+
}
102+
}
103+
return false;
104+
}
105+
}
106+
107+
108+
class Main {
109+
Keychain keychain = new Keychain(new String[] {
110+
"house",
111+
"car",
112+
"shed"
113+
});
114+
115+
static void unlock(String thing) {
116+
if (keychain.hasKey(thing)) {
117+
IO.println("You have unlocked my " + thing);
118+
}
119+
else {
120+
IO.println("You don't have a key for my " + thing);
121+
}
122+
}
123+
124+
void main() {
125+
unlock("house");
126+
unlock("car");
127+
unlock("shed");
128+
unlock("heart");
129+
}
130+
}
131+
```

src/visibility/challenges.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,92 @@
11
# Challenges
2+
3+
Remember the rules for this are
4+
5+
- Try to use only the information given up to this point in this book.
6+
- Try not to give up until you've given it a solid attempt
7+
8+
## Challenge 1
9+
10+
The following `Ratio` class should have an invariant where `denominator`
11+
cannot be `0`.
12+
13+
```java,no_run
14+
class Ratio {
15+
int numerator;
16+
int denominator;
17+
18+
Ratio(int numerator, int denominator) {
19+
if (denominator == 0) {
20+
throw new RuntimeException("Denominator cannot be zero");
21+
}
22+
}
23+
24+
double value() {
25+
return ((double) numerator) / denominator;
26+
}
27+
}
28+
```
29+
30+
Unfortunately other classes can directly access the `denominator` field.
31+
32+
Make the denominator field private and add an accessor method for other classes to use.
33+
You can name this accessor `.denominator()` or `.getDenominator()`.
34+
35+
## Challenge 2
36+
37+
Alter the code from the last challenge such that numerator is also private and only
38+
usable via an accessor method.
39+
40+
## Challenge 3
41+
42+
Alter the code from the last challenge such that you can not only access but also
43+
mutate the `numerator` and `denominator` fields via exposed methods.
44+
The method for mutating the `denominator` should throw an exception
45+
if someone tries to set it to zero.
46+
47+
## Challenge 4
48+
49+
Given this `Pineapple` class make every method except `digest` private.
50+
51+
```java,no_run
52+
class Pineapple {
53+
void digestProtein(String name) {
54+
IO.println("Pineapple juice is digesting the proteins in " + name);
55+
}
56+
57+
void eatPork() {
58+
digestProtein("pork");
59+
}
60+
61+
void eatChicken() {
62+
digestProtein("chicken");
63+
}
64+
65+
void eatBeef() {
66+
digestProtein("beef");
67+
}
68+
69+
void eatYou() {
70+
IO.println("Pineapple juice is eating you from the inside.");
71+
}
72+
73+
void digest(String thing) {
74+
switch (thing) {
75+
case "pork" -> eatPork();
76+
case "chicken" -> eatChicken();
77+
case "beef" -> eatBeef();
78+
default -> eatYou();
79+
}
80+
}
81+
}
82+
```
83+
84+
## Challenge 5
85+
86+
Rewrite the code from the previous challenge such that the `digest`
87+
method on `Pineapple` does the same thing but no other private methods
88+
exist.
89+
90+
Note when doing this that you wouldn't need to consider other code.
91+
The behavior of the only exposed method does not change so those private
92+
methods would only be "implementation details."

src/visibility/getter_and_setters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ class Person {
3838
}
3939
```
4040

41-
The answer to that is...annoying. We'll get to it, but the short story is that its a bit of a holdover from a very weird period in the early 2000s.
41+
The answer to that is interesting. We'll get to it, but the short story is that its a bit of a holdover from a very weird period in the early 2000s.
4242

4343
I mention it specifically so that you know that there isn't any important information you are missing and you are not crazy.

0 commit comments

Comments
 (0)