-
Notifications
You must be signed in to change notification settings - Fork 1
FirstThingsFirst
When you look in the FirstThingsFirst folder, you'll see several files with funny names and a strange looking folder. Something like this:
src
.classpath
.gitignore
.project
The src
folder is where the source code will go. If you click on the name, you'll see one java file.
The other files are things that you can ignore, if you want to. You can look at them if you'd like, but don't change any of the contents.
The .classpath
file is created by Eclipse and contains information that helps Eclipse find different libraries of code other people have written.
The .gitignore
file is used by git to know which file NOT to put up on GitHub.
The .project
file is used by Eclipse to store information about our specific project.
The file starts off with a strange looking /*
that marks the beginning of a comment. In programming, a comment is ignored by the language and is intended to help a human reader understand why the code does what it does. The symbols */
mark the end of the comment. So, the comment that starts on line 1 ends on line 18.
Like most other open source files, this file starts off with a copyright notice. This particular license allows anyone to use the code as long as they make any changes to the code open source.
The next comment is a short description of why the program was written.
Then we have public class HelloWorld {
that's all colorful. The color is GitHub's way of marking code that the programming language will pay attention to.
Java is an object-oriented language. This means that (almost) everything is Java is an object. In source code, we use the word class
to describe what objects can do. (For now, skip over the word public
; we'll cover that later.) The phrase HelloWorld
is the name of our fist class. Note that this is also the name of the source code file. In Java, every source code file must contain a class
with the same name as the file.
The {
is the way Java has to group lines of source code into a block. Each {
has a matching }
symbol. All the code for our class is in the block that starts on line 27 and ends on line 39.
Then there's another comment that goes from line 29 to line 35. You can read that at your leisure.
Next, the code says public static void main( String[] args ) {
. This phrase marks the beginning of a method. There's that word public
again. And, again, we're going to skip it and cover it in a later lesson. We're also going to skip static
. And void
. The word main
has special meaning to Java. It marks the part of the code that executes when the program starts running. It labels what we call the entry point of the software. Not every class you write will have a main
method. We'll cover what's in the parenthesis in another lesson. Finally, there's another {
(called the "opening curly bracket" or "opening curly brace"). This marks another block of code; the code that will run when the method main
is called.
The next line is System.out.println( "Hello world!" );
. In short, when we want to write a message to the console, we start with System.out.println
. Then we put the message we want to write inside the parenthesis and in quotation marks. How would you change that line so the system prints Hello Eric
?
The semicolon ;
is used to mark the end of one instruction for the language. In a more complicated program, there would be more instructions followed by more semicolons. But, in this simple example we only need one instruction.
The }
(called the "closing curly bracket" or "closing curly brace") on line 38 marks the end of the block that starts on line 36.
Sure. Now that we have System.out.println(...)
to put sentences on the screen (in other words, output), it's time to get words from the user (in other words, input). There's something similar to System.out
called System.in
that we'll use to get the words the user types at the keyboard.
The first thing we need to do is set up a special place in memory to store the letters and words the user types. This is done on line 41 with the byte[] buffer = new byte[255];
instructions. This sets up enough room to remember 255 letters that the user types.
Then, on line 42, we have the program put a message on the screen so the user knows to type something, in this case, their name.
Line 43 is where the magic happens. This tells the program to wait for the user to type on the keyboard and remember everything they type up to and including the enter key. All that happens from this: System.in.read(buffer);
Next, things might get a little confusing. What we have in buffer
are all the letters the user typed, but the program doesn't know they all belong together to form a word. In computer terms, we need to tell the program to take those letters and build a string out of them. You might ask "What's a string?" Well, we've already been using them, I just haven't called them by that name. In programming, everything between double quotation marks (") is a string. We can also make strings out of other information. In this case, we'll use the letters the user typed to form a string. That's what this part of line 44 does: new String(buffer)
. So, this tells the program to take the letters in buffer
and make a new
String
out of them.
Now, a couple of paragraphs ago I mentioned that System.in.read(buffer)
will remember everything the user types up to and including the enter key. So, this new
String
we just made has a special symbol at the end. If we were to print the string, we would not be able to see this special symbol the way we can see letters like 'E' and 'r'. There are several special symbols that we can type but cannot see. Another is the space key, since we don't see a space symbol, but we see a gap in between words. These special symbols are call "white space." Since the enter key is not part of someone's name, I think it should be removed from the String
. We can do that by telling the String
to .trim()
itself. When a String
.trim()
itself, it removes any white space from the beginning and from the end of the string.
To review, the line String name = new String(buffer).trim();
takes the letters in the buffer, pulls them together to make a word (or several words), removes the enter, and remembers that word as name
.
The last thing the program does is to put another message on the screen. But what, exactly, is the message? We have the String
"Hello there, "
and the String
name
and the String
"!"
. You might be wondering what the +
is doing in there. You're probably used to seeing +
in math class, with expressions like 2 + 3 = 5
where we put two numbers together to make a larger number. What does plus have to do with String
? The people who made the Java language wanted an easy and short way of putting two String
s together to make a larger String
. Since putting String
s together sounds like putting two numbers together, they decided to use the plus sign. It's confusing, yes, especially to someone new to programming.
Let's say I run the program and type my name, Eric, when the computer displays "What's your name?" The final message will be Hello there, Eric!
Are you feeling overwhelmed? Does it seem like too much new information to understand? Are you unsure of what's next? Well, I can tell you there's no quiz or exam. Remember, our goal is to program a robot to go out and compete against other, similar robots.
Java is a big language. This give us (and the other teams) many options on how to make the robot work. But, there's so much information to remember. Don't get discouraged. Very few people know the entire Java language and all the tools it offers. I've been working with Java for many years, and I still need to look up details and information about parts of the language. We don't need to know everything to make good use of the language in programming our robot. We're going to work through many more exercises and many programs making the robot do fun and interesting things. You can handle this. Most people learn best by doing, and we're going to be doing a lot of the doing.
You will learn about programming, Java, and robotics.