University of Washington, CSE 142 (190)

Lab 3: Parameters

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

Today's lab

Goals for today:

Exercise : Parameters

Exercise : String values

Exercise : Parameter Mystery practice-it

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

continued on the next slide...

Exercise - jGRASP Debugger

continued on the next slide...

Exercise - jGRASP Debugger

Exercise : Math Expressions

Java has built-in math functions (from textbook page 150):
Method Description Example
Math.abs absolute value Math.abs(-308) returns 308
Math.ceil ceiling (rounds upward) Math.ceil(2.13) returns 3.0
Math.floor floor (rounds downward) Math.floor(2.93) returns 2.0
Math.max max of two values Math.max(45, 207) returns 207
Math.min min of two values Math.min(3.8, 2.75) returns 2.75
Math.pow power Math.pow(3, 4) returns 81.0
Math.round round to nearest integer Math.round(2.718) returns 3
Math.sqrt square root Math.sqrt(81) returns 9.0

continued on the next slide...

Exercise - Math Expressions

Write the results of each of the following expressions. Make sure to use the proper type (.0 for a double).

Math.ceil(9.17)
10.0
Math.pow(2, 4)
16.0
Math.sqrt(64)
8.0
Math.floor(12.73) + Math.max(8, 5)
20.0
Math.abs(Math.min(-1, -3))
3
Math.ceil(Math.random())
1.0
-Math.pow(2, 2) + Math.pow(-2, 3) + Math.pow(2, -2)
-11.75
Math.round(4.25) + Math.round(5.38) + Math.round(6.49)
15

Exercise : Printing Strings practice-it

Cumulative algorithms

Cumulative algorithms

Exercise : Powers of 2 practice-it

Exercise : Powers of n practice-it

Exercise : printSquare practice-it

We suggest you skip this problem if the lab time is more than half over already.

Exercise : printGrid practice-it

We suggest you skip this problem if the lab time is more than half over already.

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 3 and try to solve them! Note that you won't be able to do every problem from Chapter 3; some involve concepts we have not learned yet, such as return and Scanner.