diff --git a/src/SUMMARY.md b/src/SUMMARY.md index d0c1034..bd4eccf 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -190,6 +190,7 @@ - [Return in void methods](./return_values/return_in_void_methods.md) - [Conversion](./return_values/conversion.md) - [Unreachable Statements](./return_values/unreachable_statements.md) + - [Challenges](./return_values/challenges.md) # Data Types III @@ -198,6 +199,7 @@ - [Null as Unknown](./null/null_as_unknown.md) - [Checking for null](./null/checking_for_null.md) - [NullPointerException](./null/null_pointer_exception.md) + - [Challenges](./null/challenges.md) - [Boxed Primitives](./boxed_primitives.md) - [Integer](./boxed_primitives/integer.md) - [Double](./boxed_primitives/double.md) @@ -458,8 +460,7 @@ - [Write to a File](./files/write_to_a_file.md) - [Read from a File](./files/read_from_a_file.md) - [Create a Folder](./files/creating_a_folder.md) - - [Delete a Folder](./files/deleting_a_folder.md) - - [Delete a File](./files/deleting_a_file.md) + - [Challenges](./files/challenges.md) # Data Structures & Algorithms II @@ -488,8 +489,9 @@ - [Override equals and hashCode](./objects/override_equals_and_hashCode.md) - [Variance and Casting](./objects/variance_and_casting.md) - [Subclasses](./objects/subclasses.md) -- [Generics](./generics.md) - [Interfaces](./interfaces.md) +- [Generics](./generics.md) + - [Generic Parameters](./generics/generic_parameters.md) # Data Types V diff --git a/src/arguments/challenges.md b/src/arguments/challenges.md index cc83488..e7c1229 100644 --- a/src/arguments/challenges.md +++ b/src/arguments/challenges.md @@ -53,3 +53,39 @@ void main() { ``` ## Challenge 3. + +Write a method with four overloads such that +the code in `main` can run unchanged. + +```java,editable +// CODE HERE + +void main() { + f(2); + f("b"); + f('9'); + f(new String[] { "s" }); +} +``` + +## Challenge 4. + +Call the defined methods in a way that outputs "I did it!" + +```java,editable +void i() { + System.out.print("I"); +} + +void did(String what) { + System.out.println("did " + what); +} + +void space() { + System.out.print(" "); +} + +void main() { + // Code here +} +``` \ No newline at end of file diff --git a/src/files/challenges.md b/src/files/challenges.md new file mode 100644 index 0000000..d255025 --- /dev/null +++ b/src/files/challenges.md @@ -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 + } +} +``` + + diff --git a/src/files/creating_a_folder.md b/src/files/creating_a_folder.md index 78e4e42..697e2db 100644 --- a/src/files/creating_a_folder.md +++ b/src/files/creating_a_folder.md @@ -14,6 +14,6 @@ class Main { } ``` -This, like the other methods in `Files`, throws an `IOException`. +This, like the other methods in `Files`, might throw an `IOException`. `Files.createDirectory` will fail if the folder already exists. diff --git a/src/first_steps.md b/src/first_steps.md index 15549ef..2a56d24 100644 --- a/src/first_steps.md +++ b/src/first_steps.md @@ -32,7 +32,7 @@ System.out.println("Hello, World!"); This bit of magic here - `System.out.println` - is a "statement" that "prints" the text inside the `(` and `)` as well as a "new line" to the screen. -`print` with new `l`i`n`e. +**print** with new **l**i**n**e. If you were to replace it with `System.out.print`, then the output would lack that new line. This makes the following program be functionally identical to the first. diff --git a/src/generics.md b/src/generics.md index c7e8cef..be82b98 100644 --- a/src/generics.md +++ b/src/generics.md @@ -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 \ No newline at end of file +class Box { + T value; +} +``` \ No newline at end of file diff --git a/src/generics/generic_parameters.md b/src/generics/generic_parameters.md new file mode 100644 index 0000000..1c5ae41 --- /dev/null +++ b/src/generics/generic_parameters.md @@ -0,0 +1 @@ +# Generic Parameters diff --git a/src/getting_started.md b/src/getting_started.md index dd99463..dfe9124 100644 --- a/src/getting_started.md +++ b/src/getting_started.md @@ -23,7 +23,19 @@ Run the installer, selecting all the default options. ## Mac OS +Download the "JDK .pkg" from [adoptium.net](https://adoptium.net/temurin/releases/?version=22&os=mac). +Run the installer, selecting all the default options. + + +## Linux + +Linux is a little annoying. If you are using it you are likely used to it +by now, but you can use [adoptium.net](https://adoptium.net/temurin/releases/?version=22&os=linux) like everyone else, but there is no universal installer there. + +You can either download the `.tar.gz` file that matches your machine, extract it, +and add the `bin` folder to your `PATH`, or you can try to find an installer for your +specific linux distribution. ## repl.it diff --git a/src/interfaces.md b/src/interfaces.md index 89ce18f..98291ef 100644 --- a/src/interfaces.md +++ b/src/interfaces.md @@ -1 +1,2 @@ # Interfaces + diff --git a/src/null/challenges.md b/src/null/challenges.md new file mode 100644 index 0000000..9358534 --- /dev/null +++ b/src/null/challenges.md @@ -0,0 +1 @@ +# Challenges diff --git a/src/return_values/challenges.md b/src/return_values/challenges.md new file mode 100644 index 0000000..e33720d --- /dev/null +++ b/src/return_values/challenges.md @@ -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. + + diff --git a/src/visibility/the_game.md b/src/visibility/the_game.md new file mode 100644 index 0000000..a3a82cb --- /dev/null +++ b/src/visibility/the_game.md @@ -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. \ No newline at end of file