University of Washington, CSE 142

Lab 2: for loops and parameters

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:

for loops

A for loop repeats a group of statements a given number of times.

for (initialization; test; update) {
    statement(s) to repeat;
}

Example:

for (int i = 1; i <= 10; i++) {
    System.out.println("We're number one!");
}

Exercise : simple for loop practice-it

Exercise : Verify solution in Practice-It!

screenshot Our Practice-It! system lets you solve Java problems online.

Exercise : Bottles of beer practice-it

Exercise : jGRASP Debugger

continued on the next slide...

Exercise - jGRASP Debugger

continued on the next slide...

Exercise - jGRASP Debugger

Exercise : for loop table practice practice-it

Create a table of the number of stars on each line:

*******
*****
***
*
line stars
1
7
2
5
3
3
4
1

multiplier: How much does the # of stars change between lines? -2

shift: Given your multiplier, what must be added to get that many stars on line 1? 9

Test your loop expression in Practice-It! using the checkmark icon above. Use the form:

for (int stars = 1; stars <= multiplier * line + shift; stars++) {

Exercise : printing a design practice-it

Write a program to produce the following output using nested for loops. Use a table to help you figure out the patterns of characters on each line.

-----1-----
----333----
---55555---
--7777777--
-999999999-
Line Dashes Numbers
1
5
1
2
4
3
3
3
5
4
2
7
5
1
9
dashes expression
-1
* line +
6
numbers expression
2
* line +
-1

Test your loop expressions in Practice-It! using the checkmark icon above. Use your expressions in the loop tests of the inner loops of your code.

Exercise : SlashFigure practice-it

Write a Java program in a class named SlashFigure to produce the following output with nested for loops. Use a loop table if necessary to figure out the expressions.

!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////
Line \ ! /
1
0
22
0
2
2
18
2
3
4
14
4
4
6
10
6
5
8
6
8
6
10
2
10
multiplier
2
-4
2
shift
-2
26
-2

Test your code in Practice-It! or the Output Comparison Tool.

Class constants

A class constant is a global value that cannot be changed.

public static final type name = expression;

Example:

public static final int DAYS_PER_WEEK = 7;
public static final double TAX_RATE = 0.10;

for loop expressions w/ constant

When adding a class constant to a loop expression, it affects the constant that must be added in the expression. Suppose we have the two loop expressions below for figure sizes of 5 and 9. The third line of the table shows the general formula that would be used if we turned our figure's size into a constant named SIZE.

size expression relationship
5 8 * line + 16 16 = 3 * 5 + 1
9 8 * line + 28 28 = 3 * 9 + 1
SIZE 8 * line + (3 * SIZE + 1)

continued on the next slide ...

Exercise : for loop table w/ constant

You already found loop expressions for the slash figure at size 6. Now make a table at size 4 and use the two to generalize the loop expression in terms of a constant for the figure size.

!!!!!!!!!!!!!!
\\!!!!!!!!!!//
\\\\!!!!!!////
\\\\\\!!//////
Line \ ! /
1
0
14
0
2
2
10
2
3
4
6
4
4
6
2
6
\ and /
size 6  2 * line +
-2
size 4  2 * line +
-2
size SIZE  2 * line +
-2
!
size 6 -4 * line +
26
size 4 -4 * line +
18
size SIZE -4 * line + (
4
 * SIZE +
2
)

Exercise : SlashFigure2

Test your code in the Output Comparison Tool.

Parameters

A parameter allows you to pass in a value to a method as you call it.

public static void name(type name) {   // declare
methodName(expression);                // call

Example:

public static void squared(int num) {
    System.out.println(num + " times " + num + " is " + (num * num));
}
...
squared(3);          // 3 times 3 is 9
squared(8);          // 8 times 8 is 64

Exercise : Parameters output

Solving "Parameter Mystery" problems

Exercise : Parameter Mystery practice-it

Exercise : Syntax errors

Exercise - answer

  1. line 5: cannot use variable y without declaring and initializing it
  2. line 5: cannot declare the type of y in the method call
  3. line 6: cannot call printer without the correct number of parameters (2, in this case)
  4. line 7: cannot call printer by passing the correct type of parameters (double, in this case)
  5. line 8: cannot refer to the variable z: it is in scope inside printer, not main
  6. line 11: must provide a type for x
  7. line 11: must provide a variable name for the second parameter
  8. line 12: must refer to the parameters using the exact same spelling
  9. line 13: cannot refer to variables in main that were not passed into printer as a parameter

Exercise - Corrected version

Exercise : printSquare practice-it

Exercise : printGrid practice-it

Exercise : Parameter Mystery practice-it

Exercise : Parameter Mystery practice-it

Exercise : Number lines, part 1 practice-it

Exercise : Number lines, part 2 practice-it

Exercise : Number lines, part 3 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!