Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Sep 14, 2024
1 parent 0a0309c commit d73dbe7
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions src/arguments/challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
82 changes: 82 additions & 0 deletions src/files/challenges.md
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
}
}
```


2 changes: 1 addition & 1 deletion src/files/creating_a_folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src/first_steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 12 additions & 1 deletion src/generics.md
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;
}
```
1 change: 1 addition & 0 deletions src/generics/generic_parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Generic Parameters
12 changes: 12 additions & 0 deletions src/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/interfaces.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Interfaces

1 change: 1 addition & 0 deletions src/null/challenges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Challenges
88 changes: 88 additions & 0 deletions src/return_values/challenges.md
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.


11 changes: 11 additions & 0 deletions src/visibility/the_game.md
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.

0 comments on commit d73dbe7

Please sign in to comment.