-
Notifications
You must be signed in to change notification settings - Fork 1
SecondThingsSecond
Eclipse is a program that has many things that you can customize in it. Some of those customizations are stored in the file org.eclipse.jdt.core.prefs
in this folder. These settings are specific for this SecondThingsSecond
project and won't always be set the same way for other projects. This file is not intended for people to change directly. Instead, you make changes to menus and dialog boxes in Eclipse, and Eclipse writes out this file.
When I made the FirstThingsFirst
project, I was using the default settings for Eclipse and there were no customizations to store. As I've been working on the SecondThingsSecond
project, I wanted to have things configured in a way I'm more used to and I think is easier to read.
There's a HelloWorld.java
file in the src
folder, just like in FirstThingsFirst
. I thought this was new?
Yes, you might recognize the file in the source folder as the file from the FistThingsFirst
folder. But, it's not identical--we're going to be expanding it in this lesson. In particular, we're going to be adding a game.
No, not a first person-shooter or Minecraft (but, Minecraft is written in Java, so what you're learning here can help you make mods for it). We haven't covered enough of the Java language and libraries to be ready for those kinds of games yet. We're going to work on a guessing game in this lesson.
But first, I want to go over the changes I made to the code in HelloWorld.java
that is copied from the FirstThingsFirst
folder.
In Java, there's a way to package code that gets used over and over again into what's called a (code) library. This package can then be used by other people. To tell a Java program we want to use code from a library, we use an import statement. I'll go over what Random
and Scanner
are after a couple of more headlines.
Line 46 looks new and different. Why are the lines starting there in the gray color the comments are in?
With Java, there are two ways to add comments to code. The first was introduced in the FirstThingsFirst
project. That style uses /*
to mark the beginning of some comments and */
to mark the end of the comments. The other way to add comments to code is begin the comment with //
. This style of comment ends at the end of the current line. So, line 46, 47, and 48 are all comments.
I'm going to let you in on a secret. It's only a secret because not enough programmers seem to know. So, I'm telling you early in your training so you know. When most people (even programmers) hear "computer programming" they think "Oh, that's making a computer do stuff." That's only part of what computer programming is about, and a small part of it. Most of computer programming is about problem solving. For us, the "problem" is to build and write code for a robot that will succeed in competitions.
Once we have some ideas on how we want our robot to work, we need to communicate with each other on the specific steps our robot will take. We put those steps in the source code. We put comments in the source code explaining why the robot is performing those specific steps. So, if one person writes code for the first step, someone else can get the code, know where the first step ends, and start adding code for the next step.
Also, when we get new ideas about how a step should work, the comments will help us find the location of the code that needs to be changed to get the new step to work. Writing code is almost always a group activity (except for homework), and comments, along with the source code, is an important way for the members of the group to communicate with each other.
Visually grouping things that work together to accomplish something and distancing them from things that do other things is important for people understand code. Most programming languages ignore blank space (there's a programming language named Python that does look at it) so people can use it to help guide the reader.
If you look at a book, there's usually a gap between one section and the next. That's a visual clue that there are two sections, rather than one. Some websites use a blank line to separate one paragraph from another (that's what this particular page does, anyway).
On line 58 there's another System.out.print
to ask the user if they want to play a game. The next line then reads what the user types on the keyboard. Line 60 is a new statement and is kind of complicated, so I'm going to explain it from the inside out.
I'm going to start with buffer[0]
. If you remember, we first set up buffer
on 49 by saying byte[] buffer = new byte[ 255 ];
. That line sets aside 255 places in memory to store letters. We've used buffer
again a couple of times without seeing a square bracket [
but now there's more on line 60.
- Introduce the if test
- array access
- zero-based indexing
- equality (vs. assignment)
- logical or operation
- character constants (vs. strings)
- How the random number bound parameter works
- More on Scanner
- It uses System.in like we did before
- booleans
- true and false
- While loops
- the not operator