CS120 -- The Final Rat

  1. How many rats make a filling lunch.
    1. One rat is more than enough
    2. Two rats and I'm still hungry
    3. Three rats and a bit of pumpkin pie
    4. Four rats, more rats, I love rats!
  2. (True/False) The variable "name" should hold a person's name.  The code below should work fine.

  3. int name = "Rat Fink";
  4. (Yes/No) Do the two loops below have the exact same output on the screen?

  5. int i = 1;                                int i = 2;
    while (i < 3) {                           do {
        i++;                                      g.drawString("Rat", 100, 50 + 10 * i);
        g.drawString("Rat", 100, 50+10*i);        i++;
    }                                          } while (i < 4);
  6. (True/False) We never draw things (drawString, drawRect, etc.) from mousePressed?
  7. (True/False) A program can have more than one array.
  8. (True/False) If an array cheese has ten elements, they are numbered cheese[1]  ... cheese[10].
  9. (Two points) Fill in the constructor below.  When you're done, it should be that poisen[0] = 0, poisen[1] = 1, poinsen [100] = 100, and poinsen [1000] = 1000.

  10. public class MyPanel extends EventPanel {
       int poisen[];
       public MyPanel() {
           poisen = new int[1001];
                     // YOU FILL IN THIS PART
     
     
     
     

       }
    public void paint(Graphics g) {
       int i;
       for(i = 0; i <= 1000; i++) {
           g.drawString("There's poisen at " + poisen[i], 100, 10+10*i);
       }
    }

  11. (H/V) For the code above, does the paint draw the array vertically or horizontally?
  12. When you see a rat, what should you do?
    1. Run away screeming?
    2. Run towards with loving arms
    3. Remember that every rat has amother.
    4. Remember the killer bunny rabbit.  Attack it with a shrubbery!
  13. Fill in the paint below.  When you're done, the program should print the average of the array paws.  Recall that the average is the sum divided by the number of items.

    public class MyPanel extends EventPanel{
         int count, paws[];
         MyPanel() {
             count = 1000;
             paws = new int[1000];
             paws[0] = 231;
             paws[1] = -3;
             ...
             paws[999] = 12;
         }
         public void paint(Graphics g) {
             // YOU FILL IN THIS PART









         }
    }
  14. (Three points) Below is a controlled class for cheese.  Write me a controller class that uses this class to draw three cheeses on the screen.  When the user clicks on a cheese, that cheese should become red, and the other cheeses yellow.  There should be ONE red cheese and TWO yellow cheeses on the screen at any time.

    class Cheese {
          int myX, myY;
          Color myColor;
          public Cheese(Color c, int x, int y) {
                  myColor = c
                  myX = x;
                  myY = y;
           }
           public void paint(Graphics g) {
                   g.setColor(myColor);
                   g.drawOval(myX, myY, 30,30):
           }
           public boolean inside(int x, int y) {
                   if (x > myX && x + 30 < myX && y > myY && y+30 < myY) 
                        return true;
                   else
                        return false;
           }
           public void setColor(Color c) {
                    myColor = c:
           }
    }
  15. (Two Points) Below is a whole program.  In the space provided draw what the screen looks like when the program runs.  Assume the screen is 400x300.

    public class MyPanel extends EventPanel {
           Thing t1, t2, t3;
           public MyPanel() {
                 t1 = new Thing(100, Color.blue, 200, "Rats");
                 t2 = new Thing(200, Color.green, 100, "Rule");
                 t3 = new Thing(20,  Color.red, 100, "Rightly");
           }
           public void paint(Graphics g) {
                 t1.paint();
                 t1.paint();      // Yes there are two of t1.paint()
                 t2.paint();
                 t3.paint();
           }
    }
    class Thing {
           int myX, myY;
           Color myColor;
           String myText;
           Thing(int x, Color c, int y, String text) {
                myX = x;
                myY = y;
                myText = text;
                myColor = c:
           }
           public void paint(Graphics g) {
                g.setColor(myColor);
                g.drawString(myText, myX - 10, myY - 10);
                g.fillRect(myX, myY, 50, 3);
           }
    }