You have just been hired by the Object-Oriented Language
Development Company (OLD Co.) They have begun development of
what they believe to be a radically new computer game, with the
wholly original name of Pong. Figure 1 shows a picture of the
OLD Co. Pong game:
Pong is a two player game in which each player attempts to prevent the pong-ball from reaching the wall behind his pong-paddle. A player deflects the pong-ball before it reaches the wall by placing his pong-paddle into path of the pong-ball. This causes the ball to bounce off of the paddle in the opposite direction. If the ball reaches the wall behind a player's paddle, the opponent scores. The number of points scored is dependent on the region of the wall contacted by the ball. The closer to the center of the wall the greater the number of points scored.
The high-paid software engineers at OLD Co. have spent years
designing the Pong program. They have identified the classes
that describe the objects the program will use. They have
carefully defined the public interfaces of each of the objects.
The specifications for these classes were handed off to the
programmers who completed the implementation of the graphics
portions of the applications. You've been hired to
implement the remaining classes.
The remainder of this writeup describes how to get setup to complete the Pong game and the details of the classes you must write.
Before beginning the lab you will need to do the following
things:
ProjectS10-2
directory for developing
your source code. Pong
directory to contain the
finished application (.class files). Pong
directory:
Pong.class
Pong$1.class
PongCanvas.class
PongBallTimer.class
|
PongBall.class
PongPaddle.class
PongScore.class |
These files contain the compiled Java byte-code for OLD
Co.'s nearly complete version of the Pong game.
Pong
directory and running the main
method of the Pong
class using the Java Virtual Machine (java Pong
).
If you have downloaded everything correctly, the Pong game
should appear. However, as described above the game is
incomplete and therefore it does not work! The paddles and the
ball do not move and the score-keeping mechanism does not
work. Your assignment is to write and test three classes that are
missing from the Pong game. These classes are the PongScore
,
PongPaddle
and PongBall
classes.
The PongBall
Class:
Description:
The PongBall
class describes the object which the Pong application uses
to represent the ball that bounces around the screen.
When the Pong application is executed it creates a new PongBall
object that is positioned at the center of the playing
field. Approximately every 10th of a second the
Pong application calls the move()
method on
its PongBall
object causing the ball to update
its position. Following the call to move()
the
Pong application uses the getX()
and getY()
methods to find the new location of the ball. If the Pong
application determines that the ball would have collided
with a wall or a paddle it will invoke the bounceX()
or the bounceY()
method to change the
horizontal or vertical direction of the ball. Finally, the
Pong application will draw the ball on the screen at its new
location and the process will be repeated again in another
10th of a second.
Implementation:
Implement the PongBall
class in the file PongBall.java
in your ProjectS10-2
directory (Note: not in
your Pong
directory!)
To implement the PongBall
class you will need
to define instance data for the x and y position of the ball
as well as the velocity of the ball in the x and y
directions. Additional information about the constructors
and instance methods of the PongBall
class can
be found in the JavaDocs
for the PongBall
class. Your
implementation of the PongBall
class must meet
the specifications given in the JavaDocs, so please study it
carefully.
Testing:
One way to test the PongBall
class would be
to add it to the Pong application and see if the ball begins
moving correctly. However, because we do not know precisely
how the Pong application uses the PongBall
class, it would be very difficult to determine simply by
watching the Pong application if the PongBall
class behaves correctly in every situation. Thus, before
adding the PongBall
class to the Pong
application we will test its behavior independently.
To test the PongBall
class you will need to
write a JUnit class named PongBallTest
that
tests all the methods you wrote. The test class should
create several PongBall
objects and test their
instance methods. For example, create a PongBall with an
initial position and velocity, the move it, then get its
position and assert that it has been calculated
correctly. Here's a sample test method:
Writing your test programs in this way makes it easy to tell if the class you are writing is working correctly even if the remainder of the application isn't working yet.
You will need to add additional code to the PongBallTest
class to test the bounceX()
and bounceY()
methods.
Integration:
Once you have fully tested your PongBall
class you can integrate it into the Pong application. To
integrate your PongBall
into the Pong
application copy the PongBall.class
file from
your ProjectS10-2
directory into your Pong
directory.
Now when you execute the instruction java Pong
the Pong application will run using your PongBall
class. When the Pong window appears, press the "B" key on
the keyboard and the ball should begin bouncing around the
playing field.
The PongScore
Class:
Description:
The PongScore
class describes the object that the Pong application uses to
keep track of the score for each player.
When the Pong application is executed it creates two PongScore
objects, one for the blue player and one for the green
player. Each time the ball contacts the end wall behind the
blue paddle, the scorePoints(...)
method of
the green player's PongScore
object is
invoked, and vice versa. When invoking the scorePoints(...)
method, the Pong application passes it an argument
corresponding to the number of points indicated in the
region of the wall that was contacted. The Pong application
will then invoke the getScore()
method to
determine the score to be displayed beside the player's name
below the playing field.
Implementation:
Implement the PongScore
class in the file PongScore.java
in your ProjectS10-2
directory. Your
implementation must meet the specifications given in the JavaDocs
for the PongScore
class.
Testing:
To test the PongScore
class write a JUnit
class named PongScoreTest
. Be sure to
create several PongScore
objects and to call
the scorePoints(...)
method with several
different arguments.
Integration:
Once you have fully tested your PongScore
class you can integrate it into the Pong application by
copying the PongScore.class
file from your ProjectS10-2
directory into your Pong
directory.
When you run the Pong application and press the "B" key, the ball will again begin to bounce around the playing field. However, now each time the ball contacts the end wall the opponent's score will be incremented by the appropriate number of points.
The PongPaddle
Class:
Description:
The PongPaddle
class describes the objects that the Pong application uses
to keep track of the size and location of the pong paddles.
When the Pong application is run it creates two new PongPaddle
objects: one for the blue player positioned at the left end
of the playing field, the other for the green player
positioned at the right end of the playing field. When the
blue player presses the "A" key, the Pong application
invokes the moveUp(...)
method of the blue
player's PongPaddle
object. Conversely, when
the blue player presses the "Z" key the Pong application
invokes the moveDown(...)
method. Similar
actions are taken using the green player's PongPaddle
object when the "M" and "K" keys are pressed. The Pong
application then uses the getLeftX()
, getTopY()
,
getRightX()
and getBottomY()
methods to determine where on the screen the blue and green
paddles should be drawn. These methods are also used by the
Pong application to determine when the ball collides with
one of the paddles.
Implementation:
Implement the PongPaddle
class in the file PongPaddle.java
in your ProjectS10-2
directory. Your
implementation must meet the specifications given in the JavaDocs
for the PongPaddle
class.
Testing:
To test the PongPaddle
class write a JUnit
class named PongPaddleTest
. Be sure to create
several PongPaddle
objects and to call all of
the instance methods several several times with several
different arguments (when appropriate).
Integration:
Once you have tested your PongPaddle
class
you can integrate it into the Pong application by copying
the PongPaddle.class
file from your ProjectS10-2
directory into your Pong
directory.
When the Pong application is run, pressing the "B" key begins the ball bouncing as before. However, now when the "A" or "Z" keys are pressed the blue paddle will move up or down. Similarly, when the "K" or "M" keys are pressed the green paddle will move up or down.
Congratulations! You now have a complete working Pong application!
Copy all the .class files from the Pong
directory
into your BlueJ project folder. Then submit this folder as
usual.