COMP 310
Spring 201

HW02:  Inheritance-based Ballworld

Home  Info  Canvas   Java Resources  Eclipse Resources  Piazza

Assignment Instructions

The assignment is to create Ballworld implementation where the ball implementations are sub-classes of an abstract ball.

See the on-line demo of the inheritance-based Ballworld.   You are to replicate all the functionality of the demo, except that you can make your own kind of balls. You are NOT required to replicate the specific ball behaviors in the demo! 

There is nothing sacred about the class names used in this, or any assignments!   For instance, if you prefer to call your "balls" something like "ASprite" instead of the "ABall" mentioned below, feel free to do so.   The only requirement is that you use names that mean something in the context of your program.

Step 0: Copy your HW01 code as per the instructions given in class to create both a new HW02 project in Eclipse as well as a new HW02 folder in SVN.    Ask for help if you're not sure what to do!   See Copying a Source-controlled Project or Changing the Repository Location of a Project


  1. Other than than the code for the individual concrete ball subclasses, no code in the rest of the system can reference a ball sub-class.
  2. Balls should bounce correctly off the sides of a panel on the main frame, not the entire frame.   Balls should bounce from their edges, not their middles or only one side.
  3. The application should be able to dynamically load any ball sub-class, given its fully qualified name at run-time (i.e. typed into a text field).
  4. The application should be able to clear out any balls that are already on the screen.
  5. At least 3 different types of ball sub-classes must be implemented which have distinct behaviors.  This can include changing colors, curving around, changing radius, etc.  
    • "Straight" ball does NOT qualify as one of the ball behaviors!
    • You are not required to replicate the same behaviors as in the demo.  Make your own!

Turn-in instructions:  

  1. Be sure that your code is commiting to a separate HW02 folder in SVN!   
  2. Add any instructions on how run your program in preferably a README.txt file or at least in the comments of your main controller file.
  3. Commit your code.


Recommended Methodology

A structured, planned approach to this assignment will save you a lot of time and head scratching.  It is key to understand what processes are happening where and how information flows from part of the system to the other, in particular, between the model and the view.

Break your work down into pieces where you fully understand each piece BEFORE coding anything!  Work on getting one small piece working at a time before moving to the next part.

Design --> Code --> Test --> Fix Design --> Fix Code --> Retest --> etc.


What Processes Take Place in the Model vs in the View?

Building a Controller

Building a GUI

The Variant and Invariant Aspects of a Ball

 Understanding Painting and Animation


Dynamic Ball Loading


Bouncing Balls

Click for full size image
Ball bouncing off a wall

In the above example, the new horizontal position is NOT the negative of the original horizontal position relative to the wall!

Provided Code

Don't kill yourself trying to re-invent what is given to you.  The following code snippets are provided to you:

 How to turn a ball

To change the direction of a ball without changing its speed, you need to rotate the velocity vector.   This can be easily accomplished by a simple matrix multiplication, which when multiplied out, becomes:

Vx, new = Vx, old cos(θ) - Vy, old sin(θ)

Vy, new = Vy, old cos(θ) + Vx, old sin(θ)

where θ is the clockwise angle of rotation per timer tick.   That is, for a positive angle value above, the ball will turn to the right.    Sine and cosine are static methods of the Math class.  (Physics-minded types will recognize the above equation as that for counter-clockwise rotation, but since computer graphics uses a left-handed coordinate system with the positive y-axis pointing downward, the resultant rotation is in the opposite direction as one would expect.)


Other Tasks


Grading Criteria:

  1. Replicating the behavior of the demo:
    1. Overall look: panel with controls plus separate display area -- 5
    2. Ability to type in ball class name - 5
    3. Ability to create the ball of the desired type - 15
    4. Ability to clear the balls - 10
    5. Ability to have multiple balls of multiple types on screen simultaneously - 15
    6. Correct bouncing behavior off edges of display area and edges of balls. - 10
    7. At least 3 different types of balls being made - 15
    8. Discretionary points - 5
  2. No references to concrete ball subclasses in code -- 15
  3. Other programming style and operational issues/discretionary points -- 5



© 2017 by Stephen Wong