Skip to content

Commit 626bc8d

Browse files
committed
Challenges
1 parent d67910f commit 626bc8d

File tree

7 files changed

+637
-6
lines changed

7 files changed

+637
-6
lines changed

src/SUMMARY.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ Make them do one. -->
774774
- [Get a Constructor](./reflection/get_a_constructor.md)
775775
- [Get all Constructors](./reflection/get_all_constructors.md)
776776
- [Invoke a Constructor](./reflection/invoke_a_constructor.md)
777-
777+
- [Challenges](./reflection/challenges.md)
778778
- [Annotations](./annotations.md)
779779
- [Declaration](./annotations/declaration.md)
780780
- [Usage](./annotations/usage.md)
@@ -785,6 +785,7 @@ Make them do one. -->
785785
- [@Retention](./annotations/retention.md)
786786
- [Reflective Access](./annotations/reflective_access.md)
787787
- [@Override](./annotations/override.md)
788+
- [Challenges](./annotations/challenges.md)
788789
<!-- - [@Deprecated](./annotations/deprecated)
789790
- [@Repeatable]()Note: When doing javadoc, write about @Documented -->
790791

@@ -805,6 +806,7 @@ Make them do one. -->
805806
- [Interface Extension](./interfaces_ii/interface_extension.md)
806807
- [Static Methods](./interfaces_ii/static_methods.md)
807808
- [Static Fields](./interfaces_ii/static_fields.md)
809+
- [Challenges](./interfaces_ii/challenges.md)
808810
- [Class Extension](./class_extension.md)
809811
- [Extend a Class](./class_extension/extend_a_class.md)
810812
- [Inheritance](./class_extension/inheritance.md)
@@ -815,6 +817,7 @@ Make them do one. -->
815817
- [Relation to Interfaces](./class_extension/relation_to_interfaces.md)
816818
- [Relation to Encapsulation](./class_extension/relation_to_encapsulation.md)
817819
- [Final Classes](./class_extension/final_classes.md)
820+
- [Challenges](./class_extension/challenges.md)
818821

819822

820823
# Data Types IX
@@ -825,6 +828,7 @@ Make them do one. -->
825828
- [long](./niche_numerics/long.md)
826829
- [Unsigned Operations](./niche_numerics/unsigned_operations.md)
827830
- [float](./niche_numerics/float.md)
831+
- [Challenges](./niche_numerics/challenges.md)
828832

829833
# Projects
830834

src/annotations/challenges.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Challenges
2+
3+
4+
Remember the rules for this are
5+
6+
- Try to use only the information given up to this point in this book.
7+
- Try not to give up until you've given it a solid attempt
8+
9+
## Challenge 1.
10+
11+
Declare your own `@Favorite` and `@LeastFavorite`
12+
annotations.
13+
14+
Use them to mark the code you have written so far that is your
15+
favorite and the code that is your least favorite.
16+
17+
## Challenge 2.
18+
19+
Declare a `@LoFi` annotation. It should have an element for the `author`
20+
and another element for the `song` that you were listening to on
21+
the "[24/7 Lofi Hip Hop Radio - Beats to Relax/Study To](https://www.youtube.com/watch?v=jfKfPfyJRdk)"
22+
stream when you were writing the annotated code.
23+
24+
Chill out to some tunes, write some code, and use that annotation.
25+
26+
## Challenge 3.
27+
28+
Write a method that takes in an `Object`. Use reflection to print out the values in that `Object`'s
29+
declared fields. Skip any that are non-public but also any marked with a `@Skip` annotation
30+
that you should also define.
31+
32+
```java
33+
// CODE HERE
34+
35+
class Table {
36+
public boolean flowerPot = true;
37+
public String scissors = "green";
38+
39+
@Skip
40+
public String cat = "tabby";
41+
}
42+
43+
class Main {
44+
void main() {
45+
// CODE HERE
46+
}
47+
}
48+
```

src/class_extension/challenges.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
Write a class called `Tree` which extends `ArrayList<Apple>`.
11+
12+
Add a `.grow()` method which adds two apples to itself
13+
of arbitrary size.
14+
15+
```java,editable
16+
record Apple(double size) {}
17+
18+
// CODE HERE
19+
20+
class Main {
21+
Tree tree = new Tree();
22+
tree.grow();
23+
tree.grow();
24+
25+
// You should have inherited the toString
26+
// from ArrayList
27+
IO.println(tree);
28+
29+
// As well as all the other methods
30+
tree.add(new Apple(100));
31+
IO.println(tree);
32+
33+
for (var apple : tree) {
34+
IO.println(apple);
35+
}
36+
}
37+
```
38+
39+
## Challenge 2.
40+
41+
Rewrite your `Tree` class from above to instead extend `AbstractList<Apple>`.
42+
You can find the documentation for `AbstractList` [here](https://docs.oracle.com/javase/8/docs/api/java/util/AbstractList.html).
43+
44+
## Challenge 3.
45+
46+
Make an `abstract` class called `Fish`. It should provide a method named
47+
`swim` and a method named `howLongSwim` that returns how many times `swim`
48+
was called.
49+
50+
This
51+
```java
52+
// CODE HERE
53+
54+
class Dory extends Fish {
55+
void justKeepSwimming() {
56+
IO.println("Just keep swimming");
57+
this.swim();
58+
59+
IO.println("Just keep swimming");
60+
this.swim();
61+
62+
IO.println("Just keep swimming");
63+
this.swim();
64+
65+
IO.println("swimming");
66+
this.swim();
67+
68+
IO.println("swimming");
69+
this.swim();
70+
}
71+
}
72+
73+
class Main {
74+
void main() {
75+
var dory = new Dory();
76+
dory.justKeepSwimming();
77+
78+
IO.println(dory.howLongSwim());
79+
}
80+
}
81+
```
82+
83+
## Challenge 4.
84+
85+
Make the field you use to track how many times
86+
the fish swam `protected`. Also make the `swim`
87+
method `protected` and `abstract`.
88+
89+
Write comments in your code such that the "contract"
90+
between your class and classes implementing it
91+
is that they must keep the `swam` field up to date.
92+
93+
This
94+
```java
95+
// CODE HERE
96+
97+
class Dory extends Fish {
98+
99+
100+
@Override
101+
void swim() {
102+
IO.println("Just keep swimming");
103+
this.swam++;
104+
105+
IO.println("Just keep swimming");
106+
this.swam++;
107+
108+
IO.println("Just keep swimming");
109+
this.swam++;
110+
111+
IO.println("swimming");
112+
this.swam++;
113+
114+
IO.println("swimming");
115+
this.swam++;
116+
}
117+
}
118+
119+
class Main {
120+
void main() {
121+
var dory = new Dory();
122+
dory.swim();
123+
124+
IO.println(dory.howLongSwim());
125+
}
126+
}
127+
```
128+

src/compilation/javac.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,3 @@ output/
3434
Main$1.class
3535
Main$Thing.class
3636
```
37-
38-
To compile multiple files you need to list every `.java` file
39-
one after another.
40-
41-
```

src/interfaces_ii/challenges.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
Create an interface named `TabbyCat`. It should
11+
extend the provided `Cat` interface and provide
12+
a `lounge` method along with a default implementation of that
13+
method.
14+
15+
```java
16+
interface Cat {
17+
void purr();
18+
}
19+
20+
// CODE HERE
21+
22+
class Garfield implements TabbyCat {
23+
@Override
24+
public void purr() {
25+
IO.println("mmmm lasagna");
26+
}
27+
}
28+
29+
class Main {
30+
void main() {
31+
TabbyCat c = new Garfield();
32+
// Should come in via a default method.
33+
c.lounge();
34+
}
35+
}
36+
```
37+
38+
## Challenge 2.
39+
40+
Add a static field to the `Cat` interface
41+
which indicates the healthy amount of lasagna for a cat to
42+
consume.[^zero]
43+
44+
```java
45+
interface Cat {
46+
void purr();
47+
}
48+
49+
class Main {
50+
void main() {
51+
// 0
52+
IO.println(Cat.HEALTHY_AMOUNT_OF_LASAGNA);
53+
}
54+
}
55+
```
56+
57+
## Challenge 3.
58+
59+
Put a static method on the `Cat` interface named `garfield"
60+
which returns an instance of `TabbyCat`.
61+
62+
```java,no_run
63+
public interface Cat {
64+
// CODE HERE
65+
}
66+
```
67+
68+
```java,no_run
69+
public interface TabbyCat extends Cat {
70+
// CODE FROM PREVIOUS CHALLENGES
71+
}
72+
```
73+
74+
```java,no_run
75+
class Garfield implements TabbyCat {
76+
@Override
77+
public void purr() {
78+
IO.println("mmmm lasagna");
79+
}
80+
}
81+
```
82+
83+
```java
84+
public class Main {
85+
void main() {
86+
TabbyCat tc = Cat.garfield();
87+
tc.lounge();
88+
}
89+
}
90+
```
91+
92+
Note that this gives you a way to expose a `Garfield` instance
93+
to other packages, even if the `Garfield` class itself
94+
is `non-public`.[^unrunnable]
95+
96+
[^zero]: Zero, it can literally kill them.
97+
98+
[^unrunnable]: Apologies for the inconvenience. To make the point about
99+
package visibility I had to make the code in browser non runnable
100+
or editable. You should be able to manage at this point though.

0 commit comments

Comments
 (0)