University of Washington, CSE 142

Lab 1: Static Methods; Expressions and Variables

Except where otherwise noted, the contents of this document are Copyright 2011 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 : 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

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?");
    }
}

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");
   }
}

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.

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".)

Variables

Recall that you can use a variable to store the results of an expression in memory and use them later in the program.

type name;                       // declare
name = value or expression;        // assign a value
...
type name = value or expression;   // declare-and-initialize together

Examples:

double iphonePrice;
iPhonePrice = 499.95;

int siblings = 3;
System.out.println("I have " + siblings + " brothers/sisters.");

Exercise : Variable declaration syntax

Which of the following choices is the correct syntax for declaring a real number variable named grade and initializing its value to 4.0?

Exercise : Variable assignment syntax

Suppose you have a variable named grade, set to 1.6:

double grade = 1.6;   // uh-oh

Suppose later in the program's code, we want to change the value of grade to 4.0. Which is the correct syntax to do this?

Exercise : a, b, and c practice-it

What are the values of a, b, and c after the following statements? Write your answers in the boxes on the right.

int a = 5;
int b = 10;
int c = b;

a = a + 1;            // a? 6
b = b - 1;            // b? 9
c = c + a;            // c? 16

Exercise : ComputePay

The following program redundantly repeats the same expressions many times. Download it and open it in jGRASP, then modify the program to remove the redundancy using variables. Use an appropriate type for each variable.

The program's output should be the same after your modifications. No expression should be computed more than once in the code.

Exercise : Syntax errors

answer on next slide...

Exercise - answer

  1. line 4: missing + between "x is" and x
  2. line 4: cannot print the value of x before assigning it a value
  3. line 6: cannot assign 15.2 into a variable of type int
  4. line 6: should not redeclare the variable's type
  5. line 7: " mark should be between now and +
  6. line 10: should not write the word int here
  7. line 10: variable y should be same type as x
  8. line 10: does not properly set y to be 1 more than x (should not write the word int here)
  9. line 11: and should be in quotes with surrounding spaces

Exercise - corrected version

Exercise : i, j, and k

What are the values of i, j, and k after the following statements?

int i = 2;
int j = 3;
int k = 4;
int x = i + j + k;

i = x - i - j;            // i? 4
j = x - j - k;            // j? 2
k = x - i - k;            // k? 1

Exercise : Equation

Suppose you have a real number variable x. Write a Java expression that computes a variable named y storing the following value:

y = 12.3x4 - 9.1x3 + 19.3x2 - 4.6x + 34.2

(We haven't learned a way to do exponents yet, but you can simulate them using several multiplications.)

Use the example program on the next slide to test your code.

Exercise - Example code

Copy/paste this program into jGRASP to test your solution.

// expected output:
// y is 7043.7

public class EquationY {
    public static void main(String[] args) {
        double x = 5;

        double y = put your expression for y here ;

        System.out.println("y is " + y);
    }
}

(answer on next slide)

Exercise - answer

double y = 12.3*x*x*x*x - 9.1*x*x*x + 19.3*x*x - 4.6*x + 34.2;

If you want an added challenge, try to come up with a way to compute the above value while using the * operator no more than 4 times.

(click Next → for answer)

double y = (((12.3 * x - 9.1) * x + 19.3) * x - 4.6) * x + 34.2;

Exercise : Lanterns practice-it

Exercise : Spell your name

Exercise : Birthday variables

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!