forked from Together-Java/ModernJava
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Challenges | ||
|
||
Remember the rules for this are | ||
|
||
- Try to use only the information given up to this point in this book. | ||
- Try not to give up until you've given it a solid attempt | ||
|
||
## Challenge 1. | ||
|
||
Write a file named `hello.txt` and give it `Hello, world` | ||
as contents. | ||
|
||
## Challenge 2. | ||
|
||
Write a program that asks the user for a number and writes it into | ||
a file named `numbers.txt`. | ||
|
||
## Challenge 3. | ||
|
||
Update the previous program so that the list of numbers entered by a user | ||
is stored in the file. So if they give `1`, `2`, and `3` the file should contain | ||
something like the following. | ||
|
||
``` | ||
1 | ||
2 | ||
3 | ||
``` | ||
|
||
Hint: `\n` is how you embed a newline character in `String`. You might find it useful. | ||
|
||
## Challenge 4. | ||
|
||
Update the previous program to also display the biggest number given so far | ||
if the user types `biggest` instead of a number. | ||
|
||
|
||
## Challenge 5. | ||
|
||
Make the previous program behave sensibly if the file contains data that is not numbers. | ||
|
||
## Challenge 6. | ||
|
||
Complete this program. | ||
|
||
```java | ||
import java.nio.file.Path; | ||
import java.io.IOException; | ||
|
||
class Main { | ||
record Person(String name, int age) {} | ||
|
||
void main() throws IOException { | ||
var people = new Person[] { | ||
new Person("Steve Smith", 15), | ||
new Person("Stan Smith", 42), | ||
new Person("Rodger", 1601) | ||
}; | ||
|
||
var path = Path.of("people.txt"); | ||
|
||
save(path, people); | ||
|
||
people = load(path); | ||
|
||
System.out.println(people[0]); | ||
System.out.println(people[1]); | ||
System.out.println(people[2]); | ||
|
||
} | ||
|
||
void save(Path path, Person[] people) throws IOException { | ||
// Save to a file | ||
} | ||
|
||
Person[] load(Path path) throws IOException { | ||
return null; // Make actually return an array | ||
} | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
# Generics | ||
|
||
Certain types of classes, like growable arrays | ||
and hash maps, are simply holders of data. | ||
|
||
That is, almost none of how they work has to change to | ||
store different kinds of data. | ||
|
||
Generics help us make these generically useful containers. | ||
|
||
```java | ||
public class | ||
class Box<T> { | ||
T value; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Generic Parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# Interfaces | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Challenges |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Challenges | ||
|
||
Remember the rules for this are | ||
|
||
- Try to use only the information given up to this point in this book. | ||
- Try not to give up until you've given it a solid attempt | ||
|
||
## Challenge 1. | ||
|
||
Make a method that takes a `String` as an argument and returns an `int` | ||
as a result. | ||
|
||
How the value for that int is determined is up to you. | ||
|
||
```java,editable | ||
// CODE HERE | ||
void main() { | ||
int x = process("abc"); | ||
System.out.println("Got " + x); | ||
} | ||
``` | ||
|
||
## Challenge 2. | ||
|
||
Define three methods such that the given main method will run. | ||
|
||
```java,editable | ||
// CODE HERE | ||
void main() { | ||
f(g(h(4), "b"), "e", "s"); | ||
} | ||
``` | ||
|
||
## Challenge 3. | ||
|
||
Make the following `multiply` method work for negative numbers. | ||
Do this without simply multiplying using the `*` operator. | ||
|
||
```java,editable | ||
int multiply(int x, int y) { | ||
int total = 0; | ||
for (int i = 0; i < y; i++) { | ||
total += x; | ||
} | ||
return total; | ||
} | ||
void main() { | ||
System.out.println(multiply(3, 5)); | ||
// System.out.println(multiply(-5, 5)); | ||
// System.out.println(multiply(-5, -2)); | ||
// System.out.println(multiply(9, -2)); | ||
} | ||
``` | ||
|
||
## Challenge 4. | ||
|
||
Define a method, `subtractInt`, which makes the following | ||
code run and produce the "correct" result. | ||
|
||
You will need to perform a narrowing conversion. | ||
|
||
```java,editable | ||
// CODE HERE | ||
double add(double x, double y) { | ||
return x + y; | ||
} | ||
double multiply(double x, double y) { | ||
return x * y; | ||
} | ||
void main() { | ||
int x = 5; | ||
int y = 8; | ||
int z = subtractInt(add(4, 5), mul(4, 2)); | ||
System.out.println(z); | ||
} | ||
``` | ||
|
||
## Challenge 5. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# The Game | ||
|
||
The only reason you would care about how visible a particular method or field is | ||
if you were playing what I call "the game[^that]" | ||
|
||
In the game you take some unit of code, like a class, and pretend that someone is going to | ||
try and do every possible thing that you can do with it. | ||
|
||
If you have methods named `stepOne()`, `stepTwo()` and `stepThree()` - what happens if someone calls them in the wrong order. If you have a field named `denominator` - what happens if someone sets it to zero. That sort of thing. | ||
|
||
[^that]: Not the game that you just lost by remembering you were playing the game. |