CS 117 Spring 2003, Exam 1     Name ___________________________________________

1. Short answer problems.
(a) How does Java code for defining class methods differ from defining object methods?

(b) How does Java code for calling class methods differ from calling object methods?

(c) Write a fragment of Java code to print a random integer in the range 3 through 7, inclusive. Make sure that the likelihood of picking each integer in the range is the same.

(d) Why does Java provide both float and double types? Why wouldn't one always use double?

(e) For the three code fragments below, one of them gives different output than the other two. Which one is different, and why?

// Fragment (i)
for (int i=0; i < 3; i++)
  System.out.println(i);

// Fragment (ii)
for (int j=1; j < 4; j = j + 1)
  System.out.println(j-1);

// Fragment (iii)
for (int j = 0; j <=3; j++)
  System.out.println(j);

2. (a) What value is returned from the following method, if I call it with an argument of 4? Show your work for partial credit, but indicate clearly where you have written the actual return value.

public int mystery(int n)
{
  int answer;
  switch(n) {
  case 1:
    answer =  1;
    break;
  default:
    answer = n + mystery(n-1);
  }
  return answer;
}

(b) Rewrite this method using a loop instead of recursion.

3. (a) What is the output of this code? Show your work for partial credit, but indicate clearly where you have written the actual computer output.

int sum = 0;
for (int i=0; i < 2; i++)
  for (int j=5; j > 2*i; j--)
    sum = sum + (j-i);
System.out.println(sum);

(Note: j-- is shorthand for j=j-1).

(b) Rewrite the above code with while loops, instead of for loops.

4. (a) Write the code for the class Bowler, which represents a human bowler. Bowlers have two characteristics of interest: name, and high score. Include a constructor, appropriate mutator and accessor methods, and an additional method called updateHighScore. This method should take a single bowling score as a parameter, and update the high score to this new value only if this new value is, in fact, a greater score than the previous high score. You do not need to test whether or not this new score is a legitimate bowling score (e.g. <= 300). updateHighScore should return a boolean value that is true if the high score was in fact changed and false otherwise.

You should have no input or output statements in your Bowler class (System.out.println, SimpleInput.getInteger, etc.). 

(b) Create a class called BowlerTester that does some straightforward testing of your Bowler class.
_____________________________________________________________________________

Extra blank paper: