Problem 4

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.

  1. Create a folder in your pbd-solutions directory called 04.
  2. Create a file within that 04 directory called GoodFirstProgram.java.
  3. Type out the code below into the .java file and save it.
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.");
    }
}

In the terminal, navigate to the 04 directory. Then compile the file.

~/.../pbd-solutions/04$ javac GoodFirstProgram.java
~/.../pbd-solutions/04$

Pro tip: You can also right-click folder in File Manager and select Open Terminal Here and it will navivate there for you!

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:

~/.../pbd-solutions/04$ 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

~/.../pbd-solutions/04$ 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.
~/.../pbd-solutions/04$

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:

~/.../pbd-solutions/04$ javac GoodFirstProgram.java
GoodFirstProgram.java:6: ';' expected
                System.out.println( "Hello Again" ):
                                                   ^
1 error

~/.../pbd-solutions/04$

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.

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.)


◄ 3: An Important Message 5: Comments and Slashes ►



Adapted from ProgrammingByDoing.com
©2013 Graham Mitchell

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.