Building Java Programs

Lab 1: Java Basics

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 1: 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 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 1 - run jGRASP and create file

continued on next slide...

Exercise 1 - copy/paste

continued on next slide...

Exercise 1 - save

jGRASP save dialog box

continued on next slide...

Exercise 1 - compile

continued on next slide...

Exercise 1 - run

Exercise 2: 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 taking CSE 142.
I hope it is a lot of fun!

The teachers are named Benson and Marty.

I hope they give me a grade of 4.0!

Exercise 3: 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 4: 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 5: 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 6: 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 7: What's the output?

Exercise 8: Syntax errors

answer on next slide...

Exercise 8 - 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 8 - corrected version

Exercise 9: 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
\\backslash

Example:

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

Exercise 10: What's the output? (2)

Exercise 11: Escape sequences

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 12: Spikey

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