COSC 110 A, Spring 2005
Review Questions for Chapters 5 and 6
Tom Linton
  1. Suppose that songs is an arrayList of Song objects and each Song object has a play() method that plays the song (so you can listen to it on your computer).
    1. Write Java code that declares and creates an instance of the Random class named randy.
    2. What line of code must appear at the top of your source code file in order for the code from part (a) to work correctly?
    3. What does the following code fragment do (use common language to describe the code, so don't simply translate each line of code into English)?

      for (int count = 1; count <= 3; count++) {
          int index = randy.nextInt(songs.size());
          Song mySong = (Song) songs.get( index );
          mySong.play();
      }
    4. Write Java code that plays, at random, one of the first 3 Songs in the arrayList songs.
    5. Write Java code that randomly selects the Song at index 3 of songs, or the Song at index 6 of songs and plays the chosen song.
  2. Write Java code that declares and intializes a class constant named SPADES whose value is 3.
  3. What is the difference between the access modifiers public and private?
  4. Write a JavaDoc comment, using the @param and @return tags, and the header line for a method named toMiles() that has a single input parameter named kilometers (an int) and converts the input parameter to miles (a double) and returns that result.
  5. If you just want to use a class and its methods, but don't care about the actual code that defines this class, would you look at this class's implementation or interface?
  6. In terms of testing a class using BlueJ, what is a fixture?
  7. In terms of the diary-prototype project (with a Day class and an Appointment class), give an example of a positive test for the Day class and an example of a negative test for the Day class.
  8. What does the String class's trim() method do?
  9. Assuming that the String variables firstName and lastName have already been declared and given values, write Java code that declares and creates a String variable named password that consists of the first 3 characters of lastName in uppercase followed by the first 2 characters of firstName in lowercase.