University of Washington, CSE 142 (190)

Lab 1: Java Basics; Static Methods

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

lab document created by Whitaker Brand and Marty Stepp

Basic lab instructions

Today's lab

Goals for today:

Exercise : Compile and run a program in jGRASP


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 our first 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 : Practice turning in a program

In CSE 142 you'll use a web turnin system for your homework. Let's practice turning in a file by submitting your

Exercise : Practice verifying output

Part of your homework grades come from producing correct output exactly.

Use our Output Comparison Tool web page to check if your output is correct.

Exercise : Indentation

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

The following program has poor indentation. Paste it into jGRASP and fix it.

  public class Icky {
public static void main(String[] args) {
System.out.println("Properly indented programs");
     System.out.println("look ever so much better!");
 System.out.println("please fix me");
        System.out.println("and make me beautiful again");

Exercise : Practice the Indenter Tool

In CSE 142 you can use a web page called the Indenter Tool to automatically correct the indentation of a program.

Exercise : What's the output? practice-it

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.

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


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

Exercise : What's the output? (2) practice-it

Exercise : MuchBetter practice-it

Write a complete Java program 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 Comparison Tool web page.)

Exercise : Spikey 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() {


    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

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 Chapter 1 and try to solve them!

.html" -->