CS 1401, Quiz 8

Date: Monday, October 22, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________
Class section (9:30 am or 11:30 am): _______________________________

1. Mark what is covered and what is not covered in Chapter 8:

2. Write a method named total for computing the sum of three given integers.

3. Call your method total in the main method to compute how many pizzas the student ordered if three groups of students ordered 4, 5, and 3 pizzas. (You do not need to write the entire main method, just the part that assigns the corresponding value to the variable howMany and calls your method.)

4. Trace, step by step, how the computer will perform the needed computations, and check that the result is indeed correct (should be 4 + 5 + 3 = 12).


Solution:

public int total(int num1, num2, num3){
  int sum = num1 + num2 + num3;
  return sum;
}

in the method main:

int howMany = total(4, 5, 3);

Tracing:

 -------------
|  --------   |
| |    4   |  |
|  --------   |
|    num1     |
|  --------   |
| |    5   |  |
|  --------   |
|    num2     |
|  --------   |
| |    3   |  |
|  --------   |
|    num3     |
|-------------|
|  ---------  |
| |   12    | |
|  ---------  |
|    sum      |
 -------------

 ------------
|    12      |
 ------------
   howMany