Skip to content

Commit f08cd22

Browse files
committed
More Challenges
1 parent 9c2678a commit f08cd22

File tree

11 files changed

+157
-8
lines changed

11 files changed

+157
-8
lines changed

book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ enable = true # whether or not to enable section folding
1515
level = 0 # the depth to start folding
1616

1717
[preprocessor.features]
18-
command = "python3 features.java"
18+
command = "python3 features.py"
1919
# Going to start writing the rest of the book
2020
# Assuming this is true
2121
toplevel_anonymous_class = false
File renamed without changes.

src/SUMMARY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ Javadoc and Documentation comments
294294
- [Inner Classes]()
295295
- [Annotations]()
296296
- [Reflection]()
297+
298+
The terminal
299+
javac
300+
java
301+
javadoc
302+
jshell
297303
-->
298304

299305
<!--

src/arguments/challenges.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
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+
Write a method named `printSquare` which takes one `int` argument named `size`.
11+
12+
The `size` argument should control how big of a square is output.
13+
14+
```java
15+
// CODE HERE
16+
17+
void main() {
18+
printSquare(4);
19+
System.out.println();
20+
21+
printSquare(3);
22+
System.out.println();
23+
24+
printSquare(2);
25+
System.out.println();
26+
27+
printSquare(1);
28+
System.out.println();
29+
}
30+
```
31+
32+
## Challenge 2.
33+
34+
What happens if a negative number is given to your `printSquare`?
35+
36+
Make it so that if a negative number is given, it works the same as if a positive number
37+
was given.
38+
39+
40+
```java
41+
// CODE HERE
42+
43+
void main() {
44+
printSquare(3);
45+
System.out.println();
46+
printSquare(-3);
47+
System.out.println();
48+
49+
System.out.println();
50+
printSquare(-2);
51+
System.out.println();
52+
printSquare(2);
53+
}
54+
```
55+
56+
## Challenge 3.

src/arguments/final_arguments.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void main() {
3131

3232
This has the same use as regular final variables. If there are lots of lines
3333
of code where a variable might be reassigned, it can be useful to not have
34-
to read all that code to know that it does happen.
34+
to read all that code to know that it does happen.[^opinion]
3535

36-
Generally though, adding final to arguments make it harder to read the code, simply
36+
[^opinion]: Adding `final` to all arguments can make it harder to read the code, simply
3737
because of visual noise.

src/loops_ii/challenges.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,51 @@ Do it using a `for` loop and again using a `while` loop.
9898

9999
## Challenge 5.
100100

101+
Draw a square.
102+
103+
Make it so that you can make the square bigger or smaller by
104+
changing a variable at the start of the program.
105+
106+
```
107+
*****
108+
*****
109+
*****
110+
*****
111+
```
112+
113+
## Challenge 6.
114+
115+
Draw a rectangle.
116+
117+
Make it so that you can make the rectangle bigger or smaller in either dimension by
118+
changing a variable at the start of the program.
119+
120+
```
121+
******
122+
******
123+
******
124+
```
125+
126+
## Challenge 7.
127+
128+
Draw a circle!
129+
130+
Make it so that you can make the circle bigger or smaller by
131+
changing a variable at the start of the program.
132+
133+
```
134+
**
135+
****
136+
******
137+
********
138+
******
139+
****
140+
**
141+
```
142+
143+
## Challenge 8.
144+
145+
Draw a smiley face!
146+
147+
Make it so that you can make the smile bigger or smaller by
148+
changing a variable at the start of the program.

src/loops_ii/labeled_continue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Labeled Continue
22

33
Labeled continues also work the same in `for` loops as `while` loops, but with the hopefully expected caveat that
4-
the statement of a `for` loop will always run when you get to the top of it.
4+
the statement of a `for` loop will always run when you get to the top of it.[^uncommon]
55

66
```java
77
label:

src/methods/challenges.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,39 @@ Remember the rules for this are
55
* Try to use only the information given up to this point in this book.
66
* Try not to give up until you've given it a solid attempt
77

8-
##
8+
## Challenge 1.
9+
10+
Declare a method named `printSquare`. When invoked it should print a square.
11+
12+
```java
13+
// CODE HERE
14+
15+
void main() {
16+
printSquare();
17+
}
18+
```
19+
20+
## Challenge 2.
21+
22+
Declare a method named `printSquareThreeTimes`. When invoked it should print three squares
23+
by invoking a method named `printSquare` three times.
24+
25+
```java
26+
// CODE HERE
27+
28+
// Code from challenge 1 here
29+
30+
void main() {
31+
printSquareThreeTimes();
32+
}
33+
```
34+
35+
## Challenge 3.
36+
37+
Write a program that contains at least four methods. Have fun with it.
38+
39+
```java
40+
void main() {
41+
// sing a song or something.
42+
}
43+
```

src/methods/main.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
The `main` method works the same as any other method. Java just treats it special by choosing to
44
call it in order to start your programs.
55

6+
```java
7+
void main() {
8+
System.out.println("Java will start here");
9+
}
10+
```
11+
612
This means you can do anything in your `main` method you can do in any other method, including returning early.
713

814
```java
@@ -14,4 +20,5 @@ void main() {
1420
}
1521

1622
System.out.println("WONT RUN");
17-
}
23+
}
24+
```

src/retu

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

0 commit comments

Comments
 (0)