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

Exercise : Printing Strings practice-it

  • Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:

    printStrings("abc", 5);
    

    will print the following output:

    abcabcabcabcabc
    
  • Complete the program with a main method. Try calling your method several times from main and see what happens when you pass different values.

Cumulative algorithms

  • A cumulative algorithm is one where you incrementally accumulate a larger value by repeatedly adding, multiplying, etc., and storing the result into a variable over and over.
  • Key aspect of a cumulative algorithm: A loop, and a variable declared outside the loop whose value is modified inside the loop.
  • Example: Cumulative algorithm to sum the numbers 1-100:
    int sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum = sum + i;
    }
    System.out.println(sum);    // 5050
    
  • Some of the following problems ask you to write cumulative algorithms.

Exercise : Powers of 2 practice-it

  • Write a Java method printPowersOf2 that uses a cumulative algorithm to print out the powers of 2 up to 2x. It should take a parameter that determines x; that is, the exponent of the highest power of 2 to be printed. For example, the following call:
    printPowersOf2(10);
    
    should produce the following output, because 210 = 1024:
    1 2 4 8 16 32 64 128 256 512 1024
    
    (For this problem it's okay if your numbers have a .0 at the end of them.)
  • Complete the program by writing a main method. Call your method several times in main to ensure that it works. Also check your answer using Practice-It by clicking the check-mark icon above.

Exercise : Powers of n practice-it

  • Modify your program from the previous exercise to have a method printPowersOfN that prints the powers of any given number n up to n^x. For example, the following call:
    printPowersOfN(3, 5);
    
    should produce the following output, because 35 = 243:
    1 3 9 27 81 243
    
    and the call:
    printPowersOfN(2, 10);
    
    should produce the same output as printPowersOf2(10); :
    1 2 4 8 16 32 64 128 256 512 1024
    

Exercise : printSquare practice-it

We suggest you skip this problem if the lab time is more than half over already.
  • Write a method printSquare that accepts min and max integers and prints a square of lines of increasing numbers. The first line should start with the minimum; each line that follows should start with the next-higher number. The numbers on a line wrap back to the minimum after it hits the maximum. For example, the call:
    printSquare(3, 6);
    
    should produce the following output:
    3456
    4563
    5634
    6345
    
  • If the maximum passed is less than the minimum, the method produces no output. Check your answer using Practice-It from the check-mark icon above.

Exercise : printGrid practice-it

We suggest you skip this problem if the lab time is more than half over already.
  • Write a method called printGrid that accepts two integers representing a number of rows and columns and prints a grid of integers from 1 to (rows*columns) in column-major order.
  • For example, the call:
    printGrid(4, 6);
    
    should produce the following output:
    1 5 9 13 17 21
    2 6 10 14 18 22
    3 7 11 15 19 23
    4 8 12 16 20 24
    
  • Check your answer using Practice-It from the check-mark icon above.

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.

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