University of Washington, CSE 142 (190)

Lab 2: Expressions, Variables, and Loops

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 : Expressions (2.1)

Write the results of each of the following expressions.

12 / 5 + 8 / 4
4
3 * 4 + 15 / 2
19
-(1 + 2 * 3 + (1 + 2) * 3)
-16
42 % 5 + 16 % 3
3

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? 6
b--;                  // b? 9
c = c + a;            // c? 16

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
  9. line 11: and should be in quotes with surrounding spaces

Exercise - corrected version

Exercise : simple for loop

Exercise : for loop practice

continued on the next slide...

Exercise - fill in a table

We want to produce the following output:

*******
*****
***
*

Fill in the table below indicating how many stars appear on each line of output.

Line Stars
1
7
2
5
3
3
4
1

continued on the next slide...

Exercise - complete the expression

We need an expression for the number of stars on each line of this form:

multiplier * line + constant

Exercise : Sequence of numbers

Exercise : for loop practice

continued on the next slide...

Exercise - expected output

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
2.5 * 2 + 17 / 4
9.0
4.5 / 3 / 2 + 1
1.75
46 / 3 / 2.0 / 3 * 4/5
2.0
50 / 9 / 2.0 + 200 / 10 / (5.0 / 2)
10.5

Exercise : i, j, and k

What are the values of i, j, and k after the following statements?
Write your answers on the right. (What is the code really doing?)

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

(answer on next slide)

Exercise - answer

Exercise : Birthday variables

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 : What's the output? practice-it

Exercise : Sequence of characters

Exercise : What's the output?

Exercise - answer

Exercise : Number lines, part 1 practice-it

Exercise : Number lines, part 2 practice-it

Exercise : Number lines, part 3 practice-it

Exercise : printing a design practice-it

Exercise : SlashFigure practice-it

Exercise : SlashFigure2

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 2 and try to solve them! We suggest doing some Exercises related to loops.