CompSci 6 - Classwork - Sep 27, 2006

Today's classwork focuses on graphics, inheritance and ArrayList.

Snarf the L0927 to get started.

  1. Using the Dimension object, add an instance variable to the BouncingBall class so that it remembers the size of the ball. Thus, instead of using a constant value like RADIUS, each ball could have a different size. Note that since the ball is round, the height and width in Dimension will be the same size. Change the constructor of BouncingBall so that the size can be set when the object is created, that is pass in the size as a parameter. This means that you should delete the constants RADIUS and DIAMETER and instead use the size that you are passing in.

  2. Verify your code by changing the Canvas constructor so that it creates two BouncingBall objects of different sizes.

  3. Using inheritance, create a subclass BouncingSmileythat represents a smiley face that can bounce off the walls like a BouncingBall. It should share the state of a BouncingBall (how do you share state between a super class and its subclass?) and move the same as a BouncingBall. It will be painted differently. Your smiley face should be able to be constructed with at least a center position, size, and velocity. Your smiley face should have at least a head, two eyes, and mouth that are correctly positioned and sized proportionally to the center and size given when the class is created. Note that you will need to change the pen color before drawing the eyes and mouth. Feel free to be creative about adding extra features to your smiley face (e.g., a hat, nose, teeth, headband, etc.).

    Note to draw a shape, you specify the top-left point in the bounding box, the height and width.

  4. Verify your code by adding code to the Canvas constructor so that it creates two BouncingSmiley objects of different positions and sizes in addition to the original BouncingBall objects, so you should have two bouncing balls and two bouncing smileys.

  5. In addition to the 4 bouncing objects you already have in the Canvas (leave them alone for this part), now create one ArrayList of 20 bouncing objects, 10 small red bouncing balls (all the same size) and 10 small blue bouncing smileys (all the same size but a different size then the red balls). Try creating them all with the same myCenter and myVelocity. Explain in a README file what happens and why.

  6. Now create random values for the 20 balls and smileys in the ArrayList so that myCenter is at random locations on the canvas, and myVelocity is values between -10 and 10 (but not 0).

Submit

Put both of your names at the front of your README file if you worked with a partner.

Electronically submit through Eclipse your Java code and README file to the Lab14 folder.