Skip to content

Fixed several typos and mistakes #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Chapter_11.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public class MyBufferedByteReader {
}
}
----
<1> Here we use the try-with-resources syntax again. This time we create an instance of `FileInputReader` and then and instance of `BufferedInputReader` providing the `FileInputReader` as an argument. This is how we connect to fragments of the pipe that uses the file _abc.dat_ as the source of data.
<1> Here we use the try-with-resources syntax again. This time we create an instance of `FileInputReader` and then an instance of `BufferedInputReader` providing the `FileInputReader` as an argument. This is how we connect to fragments of the pipe that uses the file _abc.dat_ as the source of data.

<2> Under the hood the `BufferedInputReader` reads bytes from the disk in chunks into a memory buffer, and then the method `read` reads one byte at time from memory.

Expand Down Expand Up @@ -254,7 +254,7 @@ If you want to read a text file into a collection, where each element contains o
List<String> myFileLines = Files.readAllLines(Paths.get("abc.dat"));
----

Both `readAllBytes` and `readAllLines` do not use buffers, but for small files is not important. For more efficient reading you can ask the class `Files` to create a buffered stream. For example, the program `MyTextFileReader` uses the method `newBufferedReader` for more efficient reading. Here I used the same file _abc.dat_ located in the root directory of our IDEA project Chapter11.
Both `readAllBytes` and `readAllLines` do not use buffers, but for small files it is not important. For more efficient reading you can ask the class `Files` to create a buffered stream. For example, the program `MyTextFileReader` uses the method `newBufferedReader` for more efficient reading. Here I used the same file _abc.dat_ located in the root directory of our IDEA project Chapter11.

[source, java]
----
Expand Down Expand Up @@ -334,7 +334,7 @@ This output was produced by the `catch` section from `MyTextFileReader` where th

==== Writing Into a File

Writing into a file is as reading one. Start with creating create an instance of the `Path` object. If you want to write bytes, create a byte array, populate it with data and call the method `write` on the class `Files`. If you want to write some text into a file, just convert the text from a `String` into a byte array and then call the method `write`, for example:
Writing into a file is as reading one. Start with creating an instance of the `Path` object. If you want to write bytes, create a byte array, populate it with data and call the method `write` on the class `Files`. If you want to write some text into a file, just convert the text from a `String` into a byte array and then call the method `write`, for example:

[source, java]
----
Expand Down Expand Up @@ -539,10 +539,10 @@ The program will print the following:
----
I've got Mary
I've got Smith
I've got 123
I've got 123.5
----

The JVM placed `Mary` into the array element `args[0]`, `Smith` into `args[1]`, and `123` into `args[2]`.
The JVM placed `Mary` into the array element `args[0]`, `Smith` into `args[1]`, and `123.5` into `args[2]`.

Command-line arguments are always being passed to a program as strings. It’s the responsibility of a program to convert the data to the appropriate data type, for example:

Expand Down Expand Up @@ -587,7 +587,7 @@ public class FileCopy {
if (args.length != 2) {
System.out.println(
"Please provide source and destination file names, for example:");
System.out.println("java FIleCopy abc.datcopyOfabc.dat");
System.out.println("java FileCopy abc.dat copyOfabc.dat");

// Exit the program
System.exit(0);
Expand Down
8 changes: 4 additions & 4 deletions Chapter_13.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ In this section our goal is to draw a 2D ping-pong table, which should look as a

Let's start with creating a new JavaFX project in IntelliJ IDEA. Select JavaFX as a project type. Press Next, and enter PingPong as a project name on the next popup window. Press the button Finish and IDEA will generate a new project for you. Rename (right-click | Refactor | Rename) the file _sample.fxml_ into _pingpong.fxml_, the `Main` class into `PingPong`, and `Controller` into `PingPongController`. Change the name of the package from _sample_ to _pong_ in the same fashion.

Now let's draw the ping-pong table in Scene Builder. Right-click on the file _pingpong.fxml_ and open it in Scene Builder. As you've seen in Chapter 7, the generated FXML uses the `<GridBag>` as a default container. You'll see an empty `GridBag` in the Hierarchy pane. The left side of the Scene Builder panels will look as follows:
Now let's draw the ping-pong table in Scene Builder. Right-click on the file _pingpong.fxml_ and open it in Scene Builder. As you've seen in Chapter 7, the generated FXML uses the `<GridPane>` as a default container. You'll see an empty `GridPane` in the Hierarchy pane. The left side of the Scene Builder panels will look as follows:

[[FIG13-2]]
image::images/fig_13_Group.png[]
Expand Down Expand Up @@ -183,7 +183,7 @@ To enable our GUI to react on keyboard events right after the program starts, we

To set the focus to the `Group` container we'll need to do two things:

1. Enable the `Group` to receive the focus by useing the attribute `focusTraversable="true"` in _pingpong.fxml_.
1. Enable the `Group` to receive the focus by using the attribute `focusTraversable="true"` in _pingpong.fxml_.

2. Right after the stage is displayed in the `PingPong` class, we'll call the method `requestFocus` on the `Group` container. The method `start` in `PingPong` will look like this (I've added just the last line to the code generated by IDEA):
+
Expand Down Expand Up @@ -350,7 +350,7 @@ Now let's teach the keys Up and Down to move the Kid's paddle vertically. Pressi
----
final int PADDLE_MOVEMENT_INCREMENT = 6;
----
Pressing the key once will change the vertical position of the paddle by 7 pixels. Seven is not a magical number, and you can use any other integer here.
Pressing the key once will change the vertical position of the paddle by 6 pixels. Six is not a magical number, and you can use any other integer here.

The new version of the controller will use the `@FXML` annotations to inject the references to the GUI components. To update the position of the kid's paddle on the GUI we'll use data binding explained in Chapter 8. We'll also add the method `initialize` that is invoked by the Java runtime once when the controller object is created. Finally, we'll write the code in the methods `process_key_Down` and `process_key_Up` to move the kid's paddle vertically.

Expand Down Expand Up @@ -838,7 +838,7 @@ To check for the ball contact with the Kid's paddle you'd write this line:
checkForBallPaddleContact(kidPaddle);
----

Now let's write the method `bounceTheBall`, which should be very similar to `moveTheBall`. The ball should move from left to write, if you've been decreasing the x-coordinate in `moveTheBall`, you'll need to decrease it now. If you've been stopping the game where the coordinate of the ball was less than -20, now it has to be more than the table width plus 20. I could refactor the method `moveTheBall` to introduce these values as methods arguments, but let's keep it as a small project for you. As long as you understand how the code works, you should be able to do it on your own. Here's the code of the method `bounceTheBall`:
Now let's write the method `bounceTheBall`, which should be very similar to `moveTheBall`. The ball should move from left to right, if you've been decreasing the x-coordinate in `moveTheBall`, you'll need to decrease it now. If you've been stopping the game where the coordinate of the ball was less than -20, now it has to be more than the table width plus 20. I could refactor the method `moveTheBall` to introduce these values as methods arguments, but let's keep it as a small project for you. As long as you understand how the code works, you should be able to do it on your own. Here's the code of the method `bounceTheBall`:

[source, java]
----
Expand Down
4 changes: 2 additions & 2 deletions Chapter_14.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Software developers make changes in their local copies of the application's code

Now Joe has the latest Mary's code, but she doesn't have the latest Joe's code changes. When Joe decides to commit his latest code to the shared repository, Mary can check them out to her computer.

In this chapter I'll introduce you to a popular VCS called Git and to the code hosting facility called GitHub. VCS's are used by programmers writing code in any languages, not only in Java. As a matter of fact you can keep track of any files using Git and store them in GitHub or other hosts. If your computer gets lost or stolen you can always recover the code by _pulling_ it from GitHub.
In this chapter I'll introduce you to a popular VCS called Git and to the code hosting facility called GitHub. VCSs are used by programmers writing code in any languages, not only in Java. As a matter of fact you can keep track of any files using Git and store them in GitHub or other hosts. If your computer gets lost or stolen you can always recover the code by _pulling_ it from GitHub.

I was using Git as a VCS and GitHub as a shared repository of the drafts of all chapters of this book, and you can find them on the GitHub at https://github.com/yfain/Java4Kids[https://github.com/yfain/Java4Kids].

Expand Down Expand Up @@ -85,7 +85,7 @@ Typically, my work with Git-controlled projects consists of the following activi

==== The status Command

I always start working with Git by opening my project directory and typing _git status_ to see which files I've modified since the last time I've committed the changes to Git repository. Let's do it in the directory _gitIntro_. I havn't committed any files to my brand new code repository just yet, and Git will respond with the following status:
I always start working with Git by opening my project directory and typing _git status_ to see which files I've modified since the last time I've committed the changes to Git repository. Let's do it in the directory _gitIntro_. I haven't committed any files to my brand new code repository just yet, and Git will respond with the following status:

[[FIG14-4]]
image::images/fig_14_git_status.png[]
Expand Down
2 changes: 1 addition & 1 deletion Chapter_3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Java also has a special keyword `final`, and if it’s used in a declaration of

==== How Much Memory is Needed

Variables are stored in a computer's memory and occupy more or less space depending on the data type. We measure memory in bits, bytes, kilobytes (1024 bytes), megabytes (1024 kilobytes or Kb), gigabytes (1024 Mb) and so on. A bit is the smallest piece of data that can be stored in memory. It can hold either 1 or 0. A byte consists of eight bits.
Variables are stored in a computer's memory and occupy more or less space depending on the data type. We measure memory in bits, bytes, kibibytes (1024 bytes), mebibytes (1024 kibibytes or KiB), gibibytes (1024 MiB) and so on. A bit is the smallest piece of data that can be stored in memory. It can hold either 1 or 0. A byte consists of eight bits.

* A `char` variable occupies two bytes in memory.

Expand Down
14 changes: 7 additions & 7 deletions Chapter_5.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

In this chapter I'll focus on discussing how you can program _behavior_ in Java. Classes have attributes (e.g. color, number of doors) and exhibit behavior (e.g. eat, sleep, or dive). There is a special group of programming languages that are called _functional_ (e.g. JavaScript, Scala et al). In these languages you can simply write functions like `eat`, `sleep`, or `dive`, and these functions can live their independent lives and don't have to be declared in classes. In _object-oriented_ languages behavior is implemented in methods defined in classes.

The phrase _to implement a method_ simply means _to write code in a method_. Starting from Java 8 you can also program behavior in so called _lambda expressions_ covered later in this chapter.
The phrase _to implement a method_ simply means _to write code in a method_. Starting from Java 8 you can also program behavior in so-called _lambda expressions_ covered later in this chapter.

So far I was declaring and implementing behavior in methods located inside Java classes. But there is a way and a reason to separate the method declaration from its implementation. Such separation can be done using _interfaces_.

=== Java Interfaces

While you can just create a Java class and implement all its methods there, a more formal way of declaring the behavior of the class in a separate `interface`. Then a class declaration would include the keyword `implements` followed by the name of the interface. This is a way to declare an application program interface (API) - what the class can do - so the programmer can quickly look at the short declaration of the interface rather than going through a longer code of the class.

For years, Java _interfaces_ were used mainly for holding method declarations and `final` `static` variables. For example, the interface `Talkable` contains the declaration of just one method `talk`:
For years, Java _interfaces_ were used mainly for holding method declarations and `final` `static` variables. For example, the interface `Talkative` contains the declaration of just one method `talk`:

[source, java]
----
interface Talkable{
interface Talkative {
public void talk();
}
----
Expand Down Expand Up @@ -53,7 +53,7 @@ public class Cat implements Talkative {
}
----

These three classes use the Java keyword `implements`, which means they promise to implement all abstract methods in the interface(s) listed after the keyword `implements` otherwise the Java compiler will complain: "You promised to implement all abstract methods from `Talkable`, where are they? Keep your promise!"
These three classes use the Java keyword `implements`, which means they promise to implement all abstract methods in the interface(s) listed after the keyword `implements` otherwise the Java compiler will complain: "You promised to implement all abstract methods from `Talkative`, where are they? Keep your promise!"

What if we need to create the class `Fish` that uses interfaces? Fish don't talk. But they swim. Dogs can talk (bark) and swim, right? Why won't we declare yet another interface `Swimmable` with the methods `swim` and `dive`?

Expand Down Expand Up @@ -512,7 +512,7 @@ public class CalculatorWithLambdas {

The difference between `CalculatorWithAnonymousClasses` from the previous section and `CalculatorWithLambdas` is that the former implements the functional interface as anonymous classes and the latter as lambdas. Lambda expressions offer a concise way of implementation of functional interfaces. To write a lambda expression you need the play by the rules:

1. Declare an the interface that has only one abstract method.
1. Declare an interface that has only one abstract method.
2. Make sure that the arguments of your lambda expression match the arguments of the abstract method.
3. Make sure that the return value of your lambda expression matches the return value of the abstract method.

Expand Down Expand Up @@ -603,11 +603,11 @@ In Chapter 9 I'll show you more lambda expressions while explaining how to proce

2. Create a package named _pets_.

3. In the package _pets_ recreate the final versions of classes `Dog`, `Fish` and interfaces `Swimmable` and `Talkable` from the section Interfaces.
3. In the package _pets_ recreate the final versions of classes `Dog`, `Fish` and interfaces `Swimmable` and `Talkative` from the section Interfaces.

4. In the package _pets_ create a new class `Pet` with a constructor that will take the name of the pet (of type `String`) as an argument.

5. Change the declarations of the classes `Dog` and `Fish` so each of them extend `Pet` while implementing `Talkable` and `Swimable` interfaces.
5. Change the declarations of the classes `Dog` and `Fish` so each of them extend `Pet` while implementing `Talkative` and `Swimable` interfaces.

6. Create the class `PetMaster` from the section "Interfaces", but give pets names while instantiating classes `Dog` and `Fish`.

Expand Down
Loading