CS 117, Winter 2002, Musicant: Exam 1 (100 points) Name:_____________________________

1. Short answers.
(a) (5 points) Consider the following two fragments of Java code:

Fragment 1:
String text = new String("Hello");
System.out.println(text.length());

Fragment 2:
System.out.println(Math.random());

In both of these cases, a method is used: length in the first example, and random in the second. Which one is the instance method, and which one is the class method? Explain the difference between instance and class methods.

(b) (5 points) Most Java compilers, including ours, convert Java programs into bytecode. What is bytecode, and how does this behavior differ from compilers for most other programming languages?

(c) (5 points) Give an example of implicit type casting, and another of explicit type casting.

2.  Code tracing
(a) (25 points) What is the output from the following code? Show your work for partial credit.

int num = 3192;						Screen Output:
int x = 0;
int y = 0;
while (num > 0)
{
  y = num % 10;
  x = x + y;
  num = (num – y) / 10;
}
System.out.println(x);

Scrap work, memory diagrams, etc.

(b) (5 points) In summary, for any positive choice of num, what does the code in part (a) accomplish?

3. Modifying code
(25 points) Rewrite the following code, using if-else statements instead of a switch statement:

Original code:					Rewritten code:

switch(x)
{
  case 1:
    System.out.println("q");
    break;
  case 2:
  case 3:
    System.out.println("b");
    break;
  case 4:
    System.out.println("e");
    System.out.println("r");
    break;
  default:
    System.out.println("t");
}

4. Instantiable Classes
(a) (25 points) Write Java code for the instantiable class Auction, which represents an online auction such as those on ebay.com. An auction object has the name of the item being sold, a minimum bid, the current high bid, and the number of bids made on the item. Implement at least one constructor and four additional methods:

public String getName() returns the name of the item.
public double getHighBid() returns the high bid on the item.
public int getNumBids() returns the number of bids made on the item.
public boolean makeBid(...) makes a bid. This method should make sure that each bid is higher than the minimum bid as well as the previous bid.  If a bid is ok, it should return true. Otherwise, it should return false.  The (...) above indicates that this is something you should fill in with more detail.

No other methods are required. DO NOT USE InputBox or OutputBox objects, or System.out.println -- this class is to be considered to be a "helper" class to other classes. If something isn't specified, do something reasonable and comment your assumptions.

(b) (5 points) Write a code fragment that creates two auctions, one for a "Rare Bee Gees CD" having a minimum bid of $15, and a second for a "Princess Bride DVD" having a minimum bid of $18. Attempt to bid $20 on the CD and $12 on the DVD. Write code to print out the high bid for each auction. You may assume that an OutputBox object named outBox exists.