University of Washington, CSE 142

Lab 1: Static Methods; Expressions

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

lab document created by Marty Stepp, Stuart Reges, Whitaker Brand and Hélène Martin

Basic lab instructions

Today's lab

Goals for today:

Exercise : Meet your neighbors

In this exercise, you will meet your neighbors and write a program that displays facts about them.

Create a complete Java program in a class named Neighbors.

Introduce yourself to two people around you (get up to do so if you have no immediate neighbors). Ask them for their full name, including their middle initial. Make sure you know how to spell their name correctly! Ask them for an interesting fact about themselves.

Your program should output the name and interesting fact of two of your neighbors. You may put all of your code in the main method.

Your output should look like the following:

Talib K. Greene's mother is an English professor in New York.
Jermaine L. Cole was born in Germany.

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

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

Download a program from lecture and 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.

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:

Compare your method choice with your neighbor's. Discuss any different decisions you made and ask a TA if you have questions about them.

Exercise : Practice verifying output

Your homework must match expected output exactly. You can use our Output Comparison Tool web page to check your output.

output comparison tool screenshot

In this exercise, we will check whether a program produces correct output. Download the program and open it in your jGRASP editor.

continued on the next slide...

Exercise : Practice verifying output, cont'd

Which output line number does not match?
4
What should be the output for that line?
"Four score and seven years ago,
Which expected blank line number is missing?
8

Expressions

Recall that Java has expressions to represent math and other computations. Expressions may use operators, which are evaluated according to rules of precedence. Every expression produces a value of a given type.

Type Description Example Result
int integers (up to 231 - 1) 3 + 4 * 5 23
double real numbers (up to 10308) 3.0 / 2.0 + 4.1 5.6
String text characters "hi" + (1 + 1) + "u" "hi2u"

Exercise : Expressions (2.1)

Write the results of each of the following expressions. If you're stuck, ask a TA or neighbor.

12 / 5 + 8 / 4
4
3 * 4 + 15 / 2
19
-(1 + 2 * 3 + (1 + 2) * 3)
-16
42 % 5 + 16 % 3
3
2.5 * 2 + 17 / 4
9.0
4.5 / 3 / 2 + 1
1.75

Exercise : More expressions (2.1)

Write the results of each of the following expressions.

5 * 6 / 4 % 3 - 23 / (14 % 6)
-10
30 % 9 + 5 % 8 - 11 % 4 % 2
7
1 + 9 / 2 * 2.0
9.0
46 / 3 / 2.0 / 3 * 4/5
2.0
50 / 9 / 2.0 + 200 / 10 / (5.0 / 2)
10.5

jGRASP Interactions Pane

continued on the next slide...

Exercise : Using jGRASP Interactions Pane

In this exercise, you'll use the Interactions Pane to quickly discover the result of some expressions that would be difficult to evaluate by hand. Copy/paste each expression below into the Interactions Pane to evaluate it, then input the answer into this slide.

123 * 456 - 789
55299
3.14 + 1.59 * 2.65
7.3535
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
1024
2 + 2 + "xyz" + 3 + 3
"4xyz33"

(For the last expression, the Interactions Pane doesn't put "" quotes around Strings when displaying results, so you must add those yourself if needed. For example, if the Interactions Pane gives you a result of abc123, it should be written here as "abc123".)

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 "

(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!