1/29/08 Notes

To Notes

CSC 212 -- 1/29/08

 

Review Questions

  1. What is wrong?

    Ans: A constructor cannot be declared with the return value void or any other return value.

  2. What is wrong?

    Ans: (1) The second line should be java Coin. (2) The Coin class does not contain a main method, so java Coin will not work. The calls should be:

  3. Rewrite this flip method so that it does not use an if statement:

    Ans:

  4. Draw a reference diagram for the following code:

    Ans: The variable p contains an arrow that points to the object with instance variables Alice, F, 23. The variable q contains an arrow that points to the object with instance variables Shawn, M, 19. When the statement q = p is executed, the arrow for q that points to the object Shawn is changed to point to the object Alice.

  5. Write a main method for the Coin class that flips a coin 10 million times, keeping track of the number of heads obtained.

    Ans:

  6. Write a main method for the Coin class that keeps track of the longest run of heads obtained.

  7. What is wrong with this for loop that adds up the odd numbers between 1 and 100?

 

Common Bugs for the Clock Lab Exercise

 

Arrays

 

Searching an Array

 

Sorting