A Good First Program

Remember, you should have spent a good amount of time in the last assignment learning how to install a text editor, run the text editor, run a command prompt, and work with both of them. If you haven't done that then don't go on, you'll not have a good time. This is the only time I'll start an exercise with a warning that you should not skip or get ahead of yourself.

public class GoodFirstProgram
	public static void main( String[] args )
		System.out.println( "Hello World!" );
		System.out.println( "Hello Again" );
		System.out.println( "I like typing this." );
		System.out.println( "This is fun." );
		System.out.println( "Yay! Printing." );
		System.out.println( "I'd much rather you 'not'." );
		System.out.println( "I \"said\" do not touch this." );

Take the above and type in into a single file named GoodFirstProgram.java. This is important as Java works best with files ending in .java.

Then, in a command prompt you compile the file by typing:

U:\My Documents\CompSci\>javac GoodFirstProgram.java

U:\My Documents\CompSci\>

If you did it right then nothing should happen. The computer will just skip a single blank line and display a prompt again. If not, then you've done something wrong. No, the computer is not wrong.

Then, assuming there were no errors, you should run the program by typing:

U:\My Documents\CompSci\>java GoodFirstProgram

If you did it right then you should see the same output I have below. If not, then you've done something wrong.

What You Should See

U:\My Documents\CompSci\>java GoodFirstProgram
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.

U:\My Documents\CompSci\>

If your output is not exactly the same, then find out why and fix it. If you have an error it will look like this:

U:\My Documents\CompSci\>javac GoodFirstProgram.java
GoodFirstProgram.java:6: ';' expected
                System.out.println( "Hello Again" ):
1 error

U:\My Documents\CompSci\>

It's important you be able to read these since you'll be making many of these mistakes. Even I make many of these mistakes. Let's look at this line-by-line.

  1. Here we ran our command in the terminal to compiler the GoodFirstProgram java file.
  2. The Java compiler then tells us that the file GoodFirstProgram.java has an error on line 6.
    In this case, the specific error is that a semicolon was expected (';' expected)
  3. It then prints this line for us.
  4. Then it puts a ^ (caret) character to point at where it thinks the problem is. Notice that there is a colon (':') at the end of the line instead of a semicolon (';')
  5. Finally, it prints out the total number of errors.

Usually the specific error messages are very cryptic, but if you copy that text into a search engine you'll find someone else who's had that error and you can probably figure out how to fix it.

What You Should Do on Your Own

You will also have a few extra things you should do to make sure you understand each exercise. Assignments turned in without these things will not receive any points. For this exercise, try these things.

  1. Make your program print another line of output.
  2. Put two slashes ('//') at the beginning of one of the println() statements. What did it do? Try to find out what these characters signify.
  3. Make your program print only one of the lines.

From now on, I won't explain how each exercise works unless an exercise is different for some reason. Each time there is code you should put in a new file, the output you should see when you run the file in the command prompt, and extra things you should do.

Copyright © 2010 Zed A. Shaw. Used by permission.

(The original Python version of this assignment is part of Zed Shaw's excellent Learn Python the Hard Way course and was translated to/reinterpreted for Java by Graham Mitchell.)