Introduction: 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...

2 answer below »

Introduction:

    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 majority of the implementation. However, just before the project was completed, all of the programmers were offered huge salaries by start-up dot com companies and simply quit on the spot! In exchange for a big check I agreed to write these classes for OLD Co. But being, ever the educator, I decided that writing these classes would be a good experience for you. Of course, I keep the check from OLD Co!


    The remainder of this lab describes how to get setup to complete the Pong game and the details of the classes you must write.



Setup:

    Before beginning the lab you will need to do the following things:



    1. Create alab5directory within yourcs132directory. All of your files for this lab must be saved in thiscs132/lab5directory.


    2. Create aPongdirectory within yourlab5directory.


    3. Download the following files into your/lab5/Pongdirectory:
      (Right click on the filename and choose "Save Link As..." from the pop-up menu)


      NOTE:When saving thePong$1.classfile, you may need to edit the filename. Netscape wants to save it as:Pong_1.class. Change the filename to the correct name before saving it!


      These files contain the compiled Java byte-code for OLD Co.'s nearly complete version of the Pong game.



    4. Test your setup by changing into thelab5/Pongdirectory and running themainmethod of thePongclass using the Java Virtual Machine (javaPong&). 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. As with lab #2, the error messages about missing fonts that appear in the terminal window when running the Pong program may be safely ignored.



Assignment:

    Your assignment is to write and test three classes that are missing from the Pong game. These classes are thePongScore,PongPaddleandPongBallclasses.



    ThePongBallClass:




      Description:



        ThePongBallclass describes the object which the Pong application uses to represent the ball that bounces around the screen. The following class diagram illustrates the information and operations that are defined by thePongBallclass.





        When the Pong application is executed it creates a newPongBallobject that is positioned at the center of the playing field. Approximately every 10thof a second the Pong application calls themove()method on itsPongBallobject causing the ball to update its position. Following the call tomove()the Pong application uses thegetX()andgetY()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 thebounceX()or thebounceY()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 10thof a second.




      Implementation:



        Implement thePongBallclass in the filePongBall.javain yourlab5directory (Note: not in yourlab5/Pongdirectory!)


        To implement thePongBallclass 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 thePongBallclass can be found in theJava Documentation for thePongBallclass. Your implementation of thePongBallclass must meet the specifications given in the Java Documentation, so please study it carefully.




      Testing:



        One way to test thePongBallclass 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 thePongBallclass, it would be very difficult to determine simply by watching the Pong application if thePongBallclass behaves correctly in every situation. Thus, before adding thePongBallclass to the Pong application we will test its behavior independently.


        To test thePongBallclass you will need to write a class namedPongBallTestthat contains amainmethod. Themainmethod in thePongBallTestclass should create severalPongBallobjects and test their instance methods. For example, the following snippet of code will create aPongBallobject and test thegetX(),getY()andmove()methods:


        public class PongBallTest { public static void main(String[] args) { PongBall ball = new PongBall(10,20,2,4); System.out.println("x should be 10, x is: " + ball.getX()); System.out.println("y should be 20, y is: " + ball.getY()); ball.move(); System.out.println("x should be 12, x is: " + ball.getX()); System.out.println("y should be 24, y is: " + ball.getY()); ball.move(); System.out.println("x should be 14, x is: " + ball.getX()); System.out.println("y should be 28, y is: " + ball.getY()); } }

        Notice that the output of the test program indicates both the correct values and the actual values that are computed. Writing your test programs in this way makes it easy to tell if the class you are testing is working correctly.


        You will need to add additional code to thePongBallTestclass to test thebounceX()andbounceY()methods.




      Integration:



        Once you have fully tested yourPongBallclass you can integrate it into the Pong application. To integrate yourPongBallinto the Pong application copy thePongBall.classfile from yourlab5directory into yourlab5/Pongdirectory.


        Now when you execute the instructionjavaPong&the Pong application will run using yourPongBallclass. When the Pong window appears, press the "B" key on the keyboard and the ball should begin bouncing around the playing field.





    ThePongScoreClass:




      Description:



        ThePongScoreclass describes the object that the Pong application uses to keep track of the score for each player. The following class diagram illustrates the information and operations that are defined by thePongScoreclass.





        When the Pong application is executed it creates twoPongScoreobjects, one for the blue player and one for the green player. Each time the ball contacts the end wall behind the blue paddle, thescorePoints(...)method of the green player'sPongScoreobject is invoked, and vice versa. When invoking thescorePoints(...)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 thegetScore()method to determine the score to be displayed beside the player's name below the playing field.




      Implementation:




      Testing:



        To test thePongScoreclass write a class namedPongScoreTestthat contains amainmethod. YourPongScoreTestprogram should use a format similar to that used inPongBallTest. Be sure to create severalPongScoreobjects and to call thescorePoints(...)method with several different arguments.




      Integration:



        Once you have fully tested yourPongScoreclass you can integrate it into the Pong application by copying thePongScore.classfile from yourlab5directory into yourlab5/Pongdirectory.


        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.





    ThePongPaddleClass:




      Description:



        ThePongPaddleclass describes the objects that the Pong application uses to keep track of the size and location of the pong paddles. The following class diagram illustrates the the information and operations that are defined by thePongPaddleclass:





        When the Pong application is run it creates two newPongPaddleobjects: 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 themoveUp(...)method of the blue player'sPongPaddleobject. Conversely, when the blue player presses the "Z" key the Pong application invokes themoveDown(...)method. Similar actions are taken using the green player'sPongPaddleobject when the "M" and "K" keys are pressed. The Pong application then uses thegetLeftX(),getTopY(),getRightX()andgetBottomY()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 thePongPaddleclass in the filePongPaddle.javain yourlab5directory. Your implementation must meet the specifications given in theJava Documentation for thePongPaddleclass.



        Bonus [+5 pts.]:Implement thePongPaddleclass using ajava.awt.Rectangleobject for instance data instead of the four values shown in the class diagram. Note: the public interface of thePongPaddleclass must not change.




      Testing:



        To test thePongPaddleclass write a class namedPongPaddleTestthat contains amainmethod. YourPongPaddleTestprogram should use a format similar to that used inPongBallTest. Be sure to create severalPongPaddleobjects and to call all of the instance methods several several times with several different arguments (when appropriate).




      Integration:



        Once you have tested yourPongPaddleclass you can integrate it into the Pong application by copying thePongPaddle.classfile from yourlab5directory into yourlab5/Pongdirectory.


        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!





Grading

    This lab will be worth 50 points. ThePongScore,PongPaddleandPongBallclasses will be worth 11 points each. Your score for each program will be determined by the correctness of its execution as well as its formatting and readability. The programs in thePongScoreTest.java,PongPaddleTest.javaandPongBallTest.javafiles will be worth 5 points each. These programs will be scored based on their ability too test each of the objects completely, as well as their formatting and readability. (That's 48 points, so 2 points for your name!)


    On the due date for this lab, you must hand in a printout of the following files:




    • PongScore.java


    • PongScoreTest.java


    • PongPaddle.java


    • PongPaddleTest.java


    • PongBall.java


    • PongBallTest.java


    These files must also appear in yourcs132/lab5directory.


    When writing your programs please be sure to pay attention to the following readability and maintainability issues:



    • Align and indent your code following the examples in the text or on the course web pages.

    • Use blank lines in your code as clues to the reader that indicate logically distinct parts of your program.

    • Choose descriptive names for your variables.

    • Use comments to describe the role of statements in solving the problem if it is not obvious from the Java code itself.

    • Use constants instead of "magic numbers."

    • Name your variables and constants using the Java naming conventions we discussed in class.


Refer this page:http://nifty.stanford.edu/2003/pong/student/pongLab.html

You may need more class files for this assignment from this page.
Answered Same DayMar 13, 2021

Answer To: Introduction: You have just been hired by the Object-Oriented Language Development Company (OLD Co.)...

Ankit answered on Mar 23 2021
124 Votes
cs132/lab5/Pong/Pong$1.class
synchronized class Pong$1 extends java.awt.event.WindowAdapter {
void Pong$1(Pong);
public void windowClosing(java.awt.event.WindowEvent);
}
cs132/lab5/Pong/Pong.class
public synchronized class Pong {
private PongCanvas canvas;
private javax.swing.JFrame
myFrame;
public void Pong(int, int);
public void show();
public void hide();
public static void main(String[]);
}
cs132/lab5/Pong/PongBall.class
public synchronized class PongBall {
private int x;
private int y;
private int xVelocity;
private int yVelocity;
public void PongBall(int, int, int, int);
public void bounceX();
public void bounceY();
public int getX();
public int getY();
public void move();
}
cs132/lab5/Pong/PongBallTimer.class
public synchronized class PongBallTimer extends java.util.TimerTask {
private PongBall ball;
private PongCanvas canvas;
private PongScore p1;
private PongScore p2;
private PongPaddle paddle1;
private PongPaddle paddle2;
public void PongBallTimer(PongBall, PongCanvas, PongPaddle, PongPaddle, PongScore, PongScore);
public void run();
}
cs132/lab5/Pong/PongCanvas.class
public synchronized class PongCanvas extends javax.swing.JPanel implements java.awt.event.KeyListener {
private int width;
private int height;
private PongBall ball;
private PongPaddle paddle1;
private PongPaddle paddle2;
private PongScore player1;
private PongScore player2;
private java.util.Timer ballTimer;
public void PongCanvas(int, int);
public boolean isFocusable();
public boolean isFocusTraversable();
public java.awt.Dimension getPreferredSize();
public java.awt.Dimension getMinimumSize();
public java.awt.Dimension getMaximumSize();
public void paintComponent(java.awt.Graphics);
public void paintBorder(java.awt.Graphics);
public void keyPressed(java.awt.event.KeyEvent);
public void keyReleased(java.awt.event.KeyEvent);
public void keyTyped(java.awt.event.KeyEvent);
}
cs132/lab5/Pong/PongPaddle.class
public synchronized class PongPaddle {
private int x;
private int y;
private int width;
private int height;
public void PongPaddle(int, int, int, int);
public void PongPaddle(java.awt.Rectangle);
public void moveUp(int);
public void moveDown(int);
public int getLeftX();
public int getTopY();
public int getRightX();
public int getBottomY();
}
cs132/lab5/Pong/PongScore.class
public synchronized class PongScore {
private int score;
public void PongScore();
public int getScore();
public void scorePoints(int);
}
cs132/lab5/PongBall.class
public synchronized class PongBall {
private int x;
private int y;
private int xVelocity;
private int yVelocity;
public void PongBall(int, int, int, int);
public void bounceX();
public void bounceY();
public int getX();
public int getY();
public void...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here