Assignment 6: Classes and objects

Introduction to Computer Program Design: Assignment 6

days Due 1 Dec 7pm

ALERT! This is a Pass/Fail (P/F) Assignment

Goals

This assignment will give you experience in defining classes for objects with fields and constructors.

Online part

Start with the online part as it will help you consolidate your knowledge for the programming part.

This part is compulsory and you must pass all the online questions related to this assignment by the deadline to pass this assignment.
The Challenge questions are optional but would help your learning.

right Go to the Online questions now and answer all the Assignment 6 questions.

right There is no 24 hours grace period for these questions.

Programming part

To Submit:

  • Ball.java and BallGame.java

right Do not rename these files. You must use the provided template file and not remove the setupGui and main methods.
right When you have submitted the files, check that you can read the files listed on the submission page, and complete the submission process.

Zip file

BallGame: Pass level

You must complete this part even if you want to do the Challenge level.

Description

The BallGame program is a very simple projectile game that involves launching a ball from one side of the window to knock target balls off their shelves. You launch the ball by clicking the mouse; the position of the mouse governs the direction and speed of the ball.

Instructions and Tips:

You will have to
right Complete the Ball class
right Modify the BallGame class to add a second target ball to the game (on a separate shelf).

Remember to use the constants in both class.

The Ball class:

right The state of a ball consists of
  • its horizontal position,
  • its height above the floor (from the floor to the bottom of the Ball),
  • its horizontal speed - how far it will move to the right in each step,
  • its vertical speed - how far it will move up (or down if the speed is negative) in each step, and
  • its color.

right Each Ball object will have a different starting position. Therefore, the constructor for Ball must have parameters for the initial position (x and height). The initial speed (horizontal and vertical) is always 0.

right A Ball should have at least the following methods:
  • The draw() method should draw the ball at its current position.
    • The ball is a circle of diameter DIAM.
    • It should not draw anything if the ball is past the RIGHT_END.
    • Note, the draw() method should not call UI.sleep(...)
  • The step() method should make the ball move one step (depending on the horizontal speed and vertical speed).
    • Never let the ball go below the floor (the height should never be negative).
    • If the ball is not stationary, then gravity should act on the ball by reducing its vertical speed by 0.2. (the vertical speed can be negative - that just means the ball is falling downwards).
    • Note: the step() method should not call draw()
  • The setXSpeed(double xSpeed) method should set the horizontal speed of the ball.
  • The setYSpeed(double ySpeed) method should set the vertical speed of the ball.
  • The getHeight() method should return the height of the ball above the floor.
  • The getX() method should return the horizontal position of the ball.

The BallGame class:

Each time round the while loop in runGame (ie, each "time tick"), the program will
  • check whether the projectile ball has gone off the right side, in which case it creates a new projectile ball.
  • move both balls by one step.
  • redraw the world and the balls in their new position

right Add a second target ball to the game (on a separate shelf), by modifying these two methods:

  • runGame
  • drawGame
    tip drawGame will need extra parameters.

BallGame: Challenge level

right Implement at least two out of the following suggestions:
  • Allow lots of targets
  • Make the ball bounce off the ground.
  • Make the collisions more realistic (the provided version knocks the target off the shelf at a fixed velocity, regardless of how the ball hit the target, and allows the launched ball to go right through the target when they are both moving). It would be nice if you could bounce the ball off a target and hit other targets as well.
  • Show the expected speed of the ball as a line while moving the mouse around (before clicking the mouse to launch the ball).
  • Make the targets be a different kind of object that fall in a different way
  • Add extra objects that can be knocked over and might fall against the targets (think "angry birds"!).
  • Allow two players to compete against each other with a projectile ball at each side.