common/conf common/conf CSE 142 Lab 1: Java basics

University of Washington, CSE 142

Lab 1: Java basics

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

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Basic lab instructions

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 : 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 MyFirstProgram.java.

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 : 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 MyFirstProgram program has good indentation.

continued on the next slide...

Exercise : Practice indentation, cont'd

Our Indenter Tool web page can fix a program's indentation.

indenter tool screenshot

In this exercise, we will use the Indenter Tool to fix the following program that has poor indentation. Download it and open it in jGRASP:

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

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 "

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

Exercise : Spikey practice-it

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!