Building Java Programs

Lab 1: Ch. 1: Java Basics, Static Methods

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

lab document created by Marty Stepp and Stuart Reges

Today's lab

Goals for today:

Exercise : Compile and run a program in jGRASP

figure

Recall from lecture: 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 - run jGRASP and create file

continued on next slide...

Exercise - copy/paste

continued on next slide...

Exercise - save

jGRASP save dialog box

continued on next slide...

Exercise - compile

continued on next slide...

Exercise - 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 : 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 : 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

Static methods

Recall the syntax for writing a static method. Methods are useful for representing a program's structure and capturing common code to avoid redundancy:

public static void name() {
    statements;
}

Example:

public static void song() {
    System.out.println("This is the song that never ends,");
    System.out.println("Yes, it goes on and on, my friends.");
}

Exercise : FightSong practice-it

Go, team, go!
You can do it.

Go, team, go!
You can do it.
You're the best,
In the West.
Go, team, go!
You can do it.

Go, team, go!
You can do it.
You're the best,
in the West.
Go, team, go!
You can do it.

Go, team, go!
You can do it.

The following program produces the output at left, but it has poor structure and redundancy. Download it and open it in jGRASP, then add at least two static methods.

continued on the next slide...

Exercise : FightSong, cont'd

Go, team, go!
You can do it.
Go, team, go! 
You can do it.
You're the best,
In the West.
Go, team, go!
You can do it.
Go, team, go! 
You can do it.
You're the best,
In the West.
Go, team, go!
You can do it.
Go, team, go!
You can do it.

Did you choose your methods well? Avoid the following pitfalls:

Please ask your TA or neighbor to check out your methods if you're not sure whether you made good choices.

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 "

(You can check your output on the Output Comparison Tool web page.)

Exercise : Spikey practice-it

Exercise : Lanterns practice-it

Exercise : Spell your name

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from the book and try to solve them!