MIT 6.030: 
Introduction to Interactive Programming

Laboratory 2: Interfaces, Classes, and Objects 
Solutions

Contents

Pre-lab Questions 

Q: Your ball classes will implement both the Ball and Animate interfaces. Suppose you want to write a ball class called "Bouncer". How would you write the first line of the class?

A:   public class Bouncer implements Ball, Animate

Q:  What methods must your class have?

A:

   public double getX();
   public double getY();
   public double getRadius();
   public void setWorld(World theWorld);
   public void userClicked(double atX, double atY);
   public void userTyped(char key);
   public void act(); 

Q: Write an entire class called Dud that implements Ball and Animate. The ball represented by a Dud object should just sit at (0, 0) with a radius of 5 and should do absolutely nothing.

A:

    public class Dud implements Ball, Animate
    {
        public double getX() 
        {
	    return 0; 
	}   

        public double getY() 
        {
	    return 0; 
	}   

        public double getRadius() 
        {
	    return 5; 
	}   

	public void setWorld(World theWorld) { } 
	public void userClicked(double atX, double atY) { } 
	public void userTyped(char key) { } 
	public void act() { } 
    }

Q:  What would happen if you created two Dud instances?

A:  It would appear to the user as if one Ball were stationary at 0,0. However, Java would actually be using two instances of the Dud class.

Q: The Dud class you just wrote didn't really need any fields (if you used fields anyway, then you were probably thinking ahead). Suppose that we want to keep the x, y, and radius values for the class in fields, so that later we can change their values. What would you have to add to or change in the Dud class to do this?

A:

    public class Dud implements Ball, Animate
    {
        private double x; 
	private double y; 
	private double radius; 

        public double getX() 
        {
	    return this.x; 
	}   

        public double getY() 
        {
	    return this.y; 
	}   

        public double getRadius() 
        {
	    return this.radius; 
	}   

	public void setWorld(World theWorld) { } 
	public void userClicked(double atX, double atY) { } 
	public void userTyped(char key) { } 
	public void act() { } 
    }

Q: Read ahead in this lab to the place where you define the act method in the Bouncer class (under Zooooooom!). Write down the fields you'll need for Bouncer, and also write down the body of the act method, and the constructor.

A:

Necessary fields:

double x      -- the X-position of the Ball
double y      -- the Y-position of the Ball 
double xVel   -- the horizontal velocity of the Ball
double yYel   -- the vertical velocity of the Ball
World  world  -- the world in which this Ball resides 

Bouncer Constructor:

    public Bouncer()
    {
	// Initialize the x and y positions of this Bouncer object
	this.x    = 0; 
	this.y    = 0; 

        // Initialize the x and y velocities of this Bouncer object
        this.xVel = Math.random() - 0.5; 
	this.yVel = Math.random() - 0.5;

	// Note:  you don't have to initialize "world" because this is done
	// through a call to setWorld().  However, it is a good idea
	// to explicitly assign "world" to null, so there is no confusion
	// as to what its initial value is supposed to be
	this.world = null; 
    }

Bouncer act() method:

    public void act() 
    {
        if (this.getX() + this.getRadius() >= this.world.getMaxX() ||
	    this.getX() - this.getRadius() <= this.world.getMinX())
	{
	    this.xVel *= -1; 
	}

	if (this.getY() + this.getRadius() >= this.world.getMaxY() ||
	    this.getY() - this.getRadius() <= this.world.getMinY())
	{
	    this.yVel *= -1; 
	}
	
	this.x += this.xVel; 
	this.y += this.yVel; 
    }

Q: Check out the following code. Assume that theWorld is a field that has already been set up appropriately, and that the code you're looking at is part of a method in a class that implements Ball. Can you figure out what might go wrong?   (Hint:  look carefully at the documentation for the World interface, above.)  How would you fix it?

   Ball closest = theWorld.getClosestBall(this);
   double xdist = closest.getX() - this.getX();

A: This one was tricky. The solution lies in the fact that theWorld.getClosestBall() returns null when closest is the only ball in theWorld. This results in an error when closest.getX() is called in the subsequent line, because you cannot invoke a method on null. (This error is called a NullPointerException. You may have seen this pop up in other places throughout the year.)

Laboratory

The following are possible implementations of the Dud, Bouncer and Exploder classes. Many variations on these solutions are possible. If you have questions, please ask one of the course staff.


This course is a part of Lynn Andrea Stein's Rethinking CS101 project at the MIT AI Lab and the Department of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology.