CS120 Midterm II -- The Test of Klingons

  1. (Talking Question) Looking at the code, how can you tell the controller class from the controlled class.
  2. What happens when you run the following method?

  3. public void paint(Graphics g) {
          int i = 1;
          while (i >= 0) {
                 g.drawString("Klingons like chocolate!!", 100, 100);
                 i = 1 - i;
            }
    }
  4. What is the first method to run?
    1. Constructor of the controlled class
    2. Constructor of the controller class
    3. Paint of the controlled class
    4. Paint of the controller class
    5. All of the above
  5. Klingons like Vulcans
    1. Baked in chocolate
    2. On a skewer
    3. Lightly salted
    4. Not at all
  6. Suppose that both the controlled and the controller class have a mousePressed.  Which one gets called when the mouse is pressed?
    1. The controlled class
    2. The controller class
    3. Whichever one the Klingon with the gun wants
    4. None of the above
  7. (True/False) There can be only one controller class per program.
  8. (True/False) There can be only one controlled class per program.
  9. Suppose I have three itsy bittsy Klingon Battlecruisers on my screen.  All of them look the same, and do the same things.  Pick one that's true.
    1. I have to write the code for KlingonBattleCruisers exactly once.  I need exactly one 'class KlingonBattleCruiser' in my program
    2. I have to write the code for KlingonBattleCruisers exactly thrice.  I need exactly three 'class KlingonBattleCruiser' in my program.
    3. Klingons don't use BattleCruisers.  That's evil propaganda by the war-mongering Captain Kirk!
  10. (3 points) Below is a controlled class that puts a Klingon Battle Cruiser on the screen.  Using this controlled class, write me a controller class that draws one Klingon Battle Cruiser wherever the user has last clicked.  There should always be exactly one Klingon Battle Cruiser on the screen at any moment.  The Klingon Battle Cruiser should start at location (100,200).
  11. public class KlingonBattleCruiser {
              Color c;
              int x,y;
              KlingonBattleCruiser(Color startColor) {
                  c = startColor;
              }
              public void move(int newx, int newy) {
                  x = newx; y = newy;
              }
              public void paint(Graphics q) {
                    // stuff to draw the cewl battle cruiser thing
              }
              public void setColor(Color c) {
                     // code to change the color
              }
    }

     
     
     
     
  12. (3 points) Below is a class that can play Klingon Love Music through the PC speaker.  Using this class, write me a program that waits for a user to press a key.  If the user presses a '1', then song '1' should start playing.  If the user presses a '3', song three should start playing.  This is true for all digits 0 ... 9, including zero and nine.  The screen can be blank or have anything you want on it.

  13. class KlingonLoveSongs {
       int songNum;
        KlingonLoveSongs() {
             // code to get the player ready goes here
        }
        public void playSong(char s) {
              // code to stop playing the song goes here
        }
    }
  14. (3 points) Write me a controller class that has three KlingonBattleCruisers on the screen.  The first one moves when you click the mouse, to wherever the mouse was clicked.  The second one changes color when you press 'r', 'g', or 'b'.  The third one just sits there.