Skip to content

Commit c69309a

Browse files
committed
More challenges
1 parent 5c513b2 commit c69309a

File tree

6 files changed

+249
-2
lines changed

6 files changed

+249
-2
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ Have a project that is just klarna but the your goal is "never write this progra
552552
- [Package-Private Constructors](./packages/package_private_constructors.md)
553553
- [Subpackages](./packages/subpackages.md)
554554
- [Reverse Domain Name Notation](./packages/reverse_domain_name_notation.md)
555+
- [Challenges](./packages/challenges.md)
555556

556557
# Data Types IV
557558

@@ -564,6 +565,7 @@ Have a project that is just klarna but the your goal is "never write this progra
564565
- [Check for Equality](./records/check_for_equality.md)
565566
- [Return Multiple Values](./records/return_multiple_values.md)
566567
- [Shorthand](./records/shorthand.md)
568+
- [Challenges](./records/challenges.md)
567569

568570
- [Integers II](./integers_ii.md)
569571
- [Integer from a String](./integers_ii/integer_from_a_string.md)

src/encapsulation/implicit_interfaces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ Then back within Java there are things which form "an interface"
1515
which may or may not use the `interface` keyword. The static methods available on a class,
1616
the constructors that are public, the set of classes that come with a library, etc.
1717

18-
I think a more general concept "the implicit interface." Everything you can observe about a thing forms its "implicit interface."[^implicit]
18+
I think a more general concept is "the implicit interface." Everything you can observe about a thing forms its "implicit interface."[^implicit]
1919

2020
[^implicit]: I call it "implicit" because there is no place where you write down "all the properties of a thing" that is not the thing itself. And by thing I mean field, method, class, group of classes, package names - everything.

src/integers_ii/integer_from_a_string.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
If you have a `String` which contains text that can be interpreted
44
as an integer you can convert it to an `int` using the `parseInt`
5-
static method on `Integer`.
5+
static method on `Integer`.[^already]
66

77
```java
88
~void main() {
@@ -41,3 +41,5 @@ try {
4141
~}
4242
```
4343

44+
[^already]: You should actually already know this. I just never explained explicitly that parseInt was a static method or showed that you could catch _only_ the `NumberFormatException`. In my defense, `IO.readln`
45+
at one point came far later in the book.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,105 @@
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+
Put this code in a file named `Pickle.java`
11+
12+
```java
13+
class Pickle {
14+
boolean dill;
15+
}
16+
```
17+
18+
Put this code in a file named `Ramen.java`
19+
20+
```java
21+
class Ramen {
22+
void mixWith(Pickle pickle) {
23+
IO.println("This is gross.");
24+
if (pickle.dill) {
25+
IO.println("Extra super gross.");
26+
}
27+
}
28+
}
29+
```
30+
31+
And finally put this code in `Main.java`
32+
33+
```java
34+
class Main {
35+
void main() {
36+
var pickle = new Pickle();
37+
pickle.dill = true;
38+
39+
var ramen = new Ramen();
40+
41+
ramen.mixWith(pickle);
42+
}
43+
}
44+
```
45+
46+
Then run the entire program from your terminal by using `java Main.java`
47+
in the directory where you made these files.
48+
49+
## Challenge 2
50+
51+
Move `Ramen.java`, `Pickle.java`, and `Main.java` into a folder named `src`.
52+
53+
Make sure you can still run the program by typing `java src/Main.java`.
54+
55+
## Challenge 3
56+
57+
The following code uses an anonymous main class. Make it instead use a named class.
58+
59+
```java,editable
60+
int dogs = 99;
61+
62+
void main() {
63+
while (dogs > 0) {
64+
IO.println(dogs + " dogs on the floor.");
65+
IO.println("Wake one up, take it for a walk");
66+
dogs--;
67+
}
68+
IO.println("No more dogs on the floor.");
69+
}
70+
```
71+
72+
## Challenge 4
73+
74+
75+
Combine the following programs while keeping them in separate files.
76+
This will require you to give names to both classes as well as
77+
parameterize the "dogs on the floor" program in some way.
78+
79+
```java
80+
int dogs = 99;
81+
82+
void main() {
83+
while (dogs > 0) {
84+
IO.println(dogs + " dogs on the floor.");
85+
IO.println("Wake one up, take it for a walk");
86+
dogs--;
87+
}
88+
IO.println("No more dogs on the floor.");
89+
}
90+
```
91+
92+
93+
```java
94+
void main() {
95+
while (true) {
96+
try {
97+
int dogs = Integer.parseInt(IO.readln("How many dogs are on the floor? "));
98+
IO.println("TODO"); // Call the first program here somehow.
99+
} catch (RuntimeException e) {
100+
IO.println("Goodbye!");
101+
}
102+
}
103+
104+
}
105+
```

src/packages/challenges.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 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+
Take all the code you have written so far and put into one project.
11+
12+
To do this put everything under one `src` folder just in different packages.
13+
You will need to convert any usages of the anonymous main class into
14+
named classes, but other than that it should be doable.
15+
16+
If you have already been doing that, just start making use of packages.
17+
They are, first and foremost, an organizational tool. Use them to organize
18+
your code as you see fit.
19+
20+
## Challenge 2.
21+
22+
In the packages above have all the growable array implementations you were
23+
asked to make in one package named `collections`. Import these collections
24+
wherever you want to use them with `import collections.GrowableIntArray;`
25+
or similar declarations.
26+
27+
This will require marking the classes and many of the methods within `public`.
28+
29+
## Challenge 3.
30+
31+
Read through [the list of packages that come with Java](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/module-summary.html). You don't need to understand everything you are looking at; just
32+
browse and try to find at least one class that interests you.
33+
34+
Import that class and try to use it.

src/records/challenges.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 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+
Replace the usage of a normal class in the following code with a record.
11+
12+
```java,editable
13+
import java.util.Arrays;
14+
15+
class InventoryItem {
16+
String name;
17+
18+
InventoryItem(String name) {
19+
this.name = name;
20+
}
21+
}
22+
23+
class Main {
24+
void main() {
25+
var item1 = new InventoryItem("sword");
26+
var item2 = new InventoryItem("shield");
27+
var item3 = new InventoryItem("armor");
28+
29+
InventoryItem[] items = {
30+
item1,
31+
item2,
32+
item3
33+
};
34+
35+
IO.println(Arrays.toString(items));
36+
}
37+
}
38+
```
39+
40+
## Challenge 2.
41+
42+
Make an instance of the `Tien` record and print it out.
43+
44+
```java,editable
45+
enum TienMove {
46+
DODON_RAY,
47+
TRI_BEAM,
48+
EXPLODE
49+
}
50+
51+
record Chiaotzu(
52+
Move memorableMove
53+
) {}
54+
55+
record Tien(
56+
Chiaotzu onlyFriend,
57+
Move firstMove,
58+
Move secondMove
59+
) {}
60+
61+
class Main {
62+
void main() {
63+
Tien tien;
64+
// CODE HERE
65+
IO.println(tien);
66+
}
67+
}
68+
```
69+
70+
## Challenge 3.
71+
72+
What will this program output when run?
73+
74+
1. `true` then `true`
75+
2. `true` then `false`
76+
3. `false` then `true`
77+
4. `false` then `false`
78+
79+
Write down your guess and then try running it.
80+
81+
```java
82+
class Position {
83+
int x;
84+
int y;
85+
86+
Position(int x, int y) {
87+
this.x = x;
88+
this.y = y;
89+
}
90+
}
91+
92+
record Location(int x, int y) {}
93+
94+
class Main {
95+
void main() {
96+
var p1 = new Position(7, 1);
97+
var p2 = new Position(7, 1);
98+
IO.println(p1.equals(p2));
99+
100+
var l1 = new Location(8, 3);
101+
var l2 = new Location(8, 3);
102+
IO.println(l1.equals(l2));
103+
}
104+
}
105+
```

0 commit comments

Comments
 (0)