Asteroids Test

  1. Suppose that you want to make a Boss object in Asteroids that has the following characteristics:Below, write the entirety of the code for the Boss class.














  2. The code below describes a "snake" GameObject:

import java.util.Random;
public class Snake extends GameObject {
   private static Random r = new Random();
   private Snake head;
   
   public Snake() {
      super(r.nextDouble() * 600, r.nextDouble() * 600, "asteroid");
      head = null;
      setTeam(-1);
   }
   
   public void step() {
      if(head == null) {
         // Move randomly
         setVelocity(getVX() + r.nextDouble() * 2 - 1,
                     getVY() + r.nextDouble() * 2 - 1);
         super.step();
      } else {
         // Move toward your head
         setPosition((head.getX() + getX()) / 2,
                     (head.getY() + getY()) / 2);
      }
   }
   
   public void collision(GameObject other) {
      if(other.getTeam() == 1) {
      } else if(head == null) {
         head = (Snake)other;
         head.setTeam(1);
      } else {
         head.collision(other);
      }
   }
}

Suppose that I do this:

Snake a = new Snake();
Snake b = new Snake();
a.collision(b);
a.collision(new Snake());

Draw me a sketch showing all the Snake objects that were created, with arrows showing what Snake each of the variables a and b now refers to, and arrows showing what the head of each Snake object refers to. If a variable is null, don't draw an arrow for it.

  1. A Bus is a GameObject that moves slowly across the screen. It will stop at various "stops" - the x coordinates that are divisible by 50 - but only if a stop has been requested since the last stop.

public class Bus extends GameObject {
   private int stopTimer;
   private boolean stopRequested;
   
   public Bus() {
      super(0, 50, "bus");
      stopTimer = 30;
      stopRequested = false;
   }
   
   public void step() {
      if(stopTimer > 0) {
         stopTimer--;
      }
      if(stopTimer == 0) {
         setX(getX() + 1);
         if(shouldStop()) {
            stopTimer = 30;
            stopRequested = false;
         }
      }
   }
   
   public boolean shouldStop() {
      if((getX() % 50 == 0) && stopRequested) {
         return true;
      } else {
         return false;
      }
   }
   
   public void requestStop() {
      stopRequested = true;
   }
}

    a) Can I press the button to request the next stop while the bus is still waiting at the current stop?


    b) The bus Game creates a single Bus and lets it move across the screen. When the user presses the space bar, a stop is requested. Fill in the code of the Game class below.

public class Game extends GameObject {
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
}

    c) Suppose that you want to make a new type of bus, ExpressBus, which stops every 200 pixels instead of every 50 pixels, but which makes every stop, regardless of whether one was requested. Fill in the contents of the ExpressBus class below.

public class ExpressBus extends Bus {
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
}

  1. Read through the classes below.

    public class A {
       public void greet(String name) {
          System.out.print("Hello, " + name);
       }
    }
    public class B extends A {
       public void greet() {
          greet("stranger");
       }
    }
    public class C extends B {
       public void greet(String name) {
          System.out.print("Hi, " + name);
       }
    }

    What will be printed out if I type (new C()).greet()?