CoSc 200 Laboratory #4
Classes (Push Ball)
For this lab, we would like you to write a program that allows the user to inflate, deflate, drag, and push two pretty balls around the screen. Here is a demo of what we have in mind:
We will help you design this program by identifying the classes and methods needed. In particular, you will need two classes named SimpleButton and PushBall and a class that is an extension of WindowController.
The SimpleButton class is simple modification to the MyButton class discussed in lecture. There should be
The PushBall class is similar to the Shirt class discussed in lecture, the FunnyFace class discussed in the text, and the BBall class provided as a supplement. The PushBall class should have
The program, which will extend the WindowController class, will make use of the SimpleButton and PushBall classes. Your program class should have
First, design each of the three classes. Start with a file that you name "SimpleButton.java". Write a comment describing the purpose of the SimpleButton class, your name as author, and the date. For each planned method, write a comment describing the purpose or action of the method and write a signature line. Add instance variables with descriptive names and/or explanatory comments. Repeat this process for the other two classes you need to design and implement. Once you are finished, or get stuck, discuss your design with the instructor, student assistants, or a classmate.
Second, implement the SimpleButton class. Of course, you should do this one method at a time. Compile often to catch syntax errors. Test your implementation. Note that testing is somewhat more complicated because the class you are implementing is not a program. You will need to use the SimpleButton class in a small program. For example, you could construct a button in the begin method, change its color in the onMousePressed method, and do a System.out.println of the return of the "contains" method in the onMouseClicked method.
Third, implement the part of your main program that displays the four buttons. This provides a further test of the SimpleButton class.
Fourth, implement enough of the PushBall class so that you could implement the appearance of the balls at the startup of your program, and then do this program implementation. In order to successfully compile PushBall.java, you may need to comment out (place // in front of) signature lines of methods you are not ready to implement. It is better to invest the short amount of time to do this rather than trying to implement everything at once and getting mired in 50 syntax errors, or worse, several hidden logic errors.
Fifth, implement enough of the PushBall and program classes so that you can inflate and deflate the balls. Of course, you should first implement and test inflation. Once that works properly, then deflation should be easy to implement.
Sixth, implement enough of the PushBall and program classes so that you can drag around the balls. At this point, there will be no interaction between the balls: the dragged ball will pass right through the other ball. Notice that once you finish this step, you could receive 18 out of the 20 available points for this project. This assumes that you use appropriate style and design. A program that does not compile or run will not receive more than 10 points, because no points will be awarded for correctness. It may seem extreme, but we really want incremental programming to be rewarded.
Seventh, add interaction to the PushBall and program classes so that one ball can push the other ball around. This may be the most difficult part of this lab, so here is some help with the mathematics. The idea is that everything is fine until one ball is moved in such a way as to overlap the other ball. At that point, we need to move the other ball opposite to the direction of the overlap and far enough so that the two balls are just touching. You should include in your PushBall class a method with the signature
public void interactsWith(PushBall other)
that will move this ball an appropriate amount based on the location of the other ball if there is overlap between the two balls. Your program class will need to use this method whenever an overlap is possible, that is, when one ball is dragged around or inflated.
Value Feature Style, Design, and Efficiency 3Includes informative comments. 2Uses appropriate names and scope. 1Uses appropriate formatting 2Uses appropriate method signatures. 2Uses efficient code. Correctness 2Panel with four simple buttons is displayed at startup. 2Two balls, each consisting of at least 3 graphical objects, are displayed at startup. 2Each ball can be dragged with the mouse. 2Each ball can be inflatted and deflatted by button presses. 2Each ball can push the other ball.
As with previous lab assignments, you should submit your ".java" files through Blackboard. This time you will probably have three files to submit. You can either add these files separately before you submit, or you can first zip together these files into a single ".zip" file and submit this single ".zip" file.
Submission Deadline: 3:00pm on Tuesday, January 31. Note that once you submit, it is impossible to submit a revision.
Resubmission and Late Submission Deadline: 3:00pm on Tuesday, February 7. A 5 point penalty will be assessed for a resubmission or late submission. These should be submitted to Lab03Late.
Good luck and have fun!