Review and Preview
In the first two classes, you’ve learned about the structure of a Java program, some rules for typing code, and how to compile and run a Java program. Do you have some ideas of projects you would like to build using Java? If so, great. Beginning with this class, you will start to develop your own programming skills. In each class to come, you will learn some new features of the Java language. In this class, you will write your first Java program. To do this, you first need to learn about some of the basic components of the Java language. You will learn about variables, assignment statements and some simple operators.
Creating a Java Program
Recall from Class 2 that a Java statement does something. In the Welcome example, we saw a statement that printed some information (“Welcome to Java for Kids!”). Each program we build in this class will be made up of many Java statements for the computer to process. Creating a computer program using Java (or any other language) is a straightforward process. You have a particular task you would like the computer to do for you. You tell the computer in a logical, procedural set of steps how to accomplish that task.
It’s relatively easy to write out solution steps to a problem in our language (English, in these notes). The difficult part is you have to talk to the computer in its own language. It would be nice if we could just write “Hey computer, here’s two numbers – add them together and tell me the sum.”
A human might understand these instructions, but a computer won’t. Why? First, the computer needs to be told how to do tasks in very specific, logical steps. For this little addition example, the steps would be:
Next, we need to talk to the computer in its own language. We translate each solution step into a statement (or statements) in the computer’s language. And, in this course, the computer’s language is Java. To be able to tell the computer how to do any task, you need to have a thorough understanding of the Java language. Your understanding of Java will allow you to translate your programming steps into a language the computer can understand.
Another thing to remember as you write Java programs is that you need to be logical and exact. A computer will follow your instructions – even if they’re wrong! So, as you learn Java, we will emphasize the need to be exact. Once you write exact and logical Java code, the computer is very good and fast at doing its job. And, it can do some pretty amazing things. Let’s look at a couple of other examples of writing out a programming task as a series of steps to illustrate some things a computer can do.
What if your principal asks you to average the test scores of the 352 students in your school? Those steps are:
Not too hard, huh? Notice here that the second step can be further broken down into smaller steps. To add up 352 scores, you would:
In these steps, the computer would do the same task (adding a number) 352 times. Computers are very good at repeating tasks – we will see that this process of repetition is called looping.
You will build code for this example in Class 7.
Computers are also very good at playing games with you (that’s why video games are so popular). Have you ever played the card game “War?” You and another player take a card from a standard playing deck. Whoever has the ‘highest’ card wins the other player’s card. You then each get another card and continue the comparison process until you run out of cards. Whoever has the most cards once the game stops is declared the winner. Playing this game would require steps similar to these:
Things are a bit more complicated here, but the computer is up to the task. The first step requires the computer to shuffle a deck of cards. How do you tell a computer how to do this? Well, before this course is over, you will know how. For now, just know that it’s a series of several programming steps. We will put the Java program for such a specific task in its own area called a method (similar to the main method seen in our little Welcome example). This makes the program a little easier to follow and also allows use this code in other programs, an advantage of object-oriented programming. Notice Step 4 requires the computer to make a decision – determining which card is higher. Computers are very good at making decisions. Finally, Step 5 asks us to repeat the handing out of cards – another example of looping. You will also build this program in Class 7.
If all of these concepts are not clear at the moment, that’s okay. They will become clearer as you progress through this course. I just wanted you to have some idea of what you can do with Java programs. Just remember, for every Java program you create, it is best to first write down a series of logical steps you want the computer to follow in performing the tasks needed by your program. Then, converting those steps into the Java language will give you your Java program – it’s really that simple. This class begins instruction in the elements of Java. And, in subsequent classes, you learn more and more Java, adding to your Java vocabulary. We’ll start slow. By the end of this course, you should be pretty good at “talking Java.”