Building Java Programs

Lab: Java Basics

Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.

Today's lab

Goals for today:

Exercise : Compile and run a program

figure

A Java program must be compiled, or translated into binary instructions. Then it can be executed or run. When you run a program, it displays output messages to the user in a text window called a console.

For this exercise, let's compile and run a short program that we will provide to you. (See the following slides.) If you get stuck, ask a classmate or TA for help.

Exercise - create a Java program

continued on next slide...

Exercise - save

continued on next slide...

Exercise - compile/run

Exercise : Modify an existing program

Modify your MyFirstProgram file to produce the following console output. Note the blank lines; you should include those in your output.

Hello, world!
I am learning to program in Java.
I hope it is a lot of fun!

I hope I get a good grade!

Maybe I'll change my major to computer science.

Exercise : Exploring syntax errors

Discover what error messages the compiler produces when you make each of the following mistakes. How many unique error messages are you able to cause the compiler to produce?

Notice that the error messages don't always make it obvious what is wrong. But they usually tell you the right line number to fix.

Exercise : What's the output? practice-it

Escape sequences

An escape sequence inserts a special character into a println statement.

SequenceSpecial character
\nnew-line (goes to the next line)
\ttab (indents output by roughly 8 spaces)
\"quotation mark
\\backslash

Example:

System.out.println("I said \"hello\" to Fred.");

Exercise : What's the output? practice-it

Exercise : MuchBetter practice-it

Write a complete Java program named MuchBetter that produces the following output (note the blank line):

A "quoted" String is
'much' better if you learn
the rules of "escape sequences."

Also, "" represents an empty String.
Don't forget: use \" instead of " !
'' is not the same as "

Exercise : Spikey practice-it

Exercise : Practice indentation

Programs should be indented properly to make them easier to read:

Example:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
        System.out.println("How are you?");
    }
}

Make sure that your Neighbors program has good indentation.

continued on the next slide...

Exercise : Practice indentation, cont'd

In this exercise, we will fix a program that has poor indentation. Download it and open it in your editor:

continued on the next slide...

Exercise : Practice indentation, cont'd

public class Icky {
    public static void main(String[] args) {
        System.out.println("Well-indented programs");
        System.out.println("look much better.");
        System.out.println("Please fix me");
        System.out.println("so I look nicer");
    }
}

Exercise : Syntax errors

answer on next slide...

Exercise - answer

  1. line 1: missing { after Tricky
  2. line 2: missing void before main
  3. line 2: missing [] after String
  4. line 3: missing " marks around Hello world
  5. line 4: system should be System (uppercase S)
  6. line 4: Pritnln should be println (lowercase P and fixed spelling)
  7. line 4: ? should be before "
  8. line 5: missing semicolon after ()
  9. line 7: missing ) after "
  10. line 8: System.println should be System.out.println
  11. line 8: { should be }

Exercise - corrected version

Exercise : Readings

Read one or more of the following articles and discuss with your neighbors: