Com S 362 --- Object-Oriented Analysis and Design HOMEWORK 1: OOP IN JAVA (File $Date: 2002/09/13 04:34:18 $) Due: problem 1, Sept. 6, 2002 (in class at 11AM), problem 2, Sept. 13, 2002 (in class at 11AM), problems 3-4, Sept. 16, 2002 (in class at 11AM). READINGS Read chapters 1-2 (although you can skim chapter 1) and 5 of Arthur J. Reil's book Object-Oriented Design Heuristics (Addison-Wesley, Reading, MA, 1996.) You will also need to read something about Java, if you're not familiar with it. We recommend reading the interesting (to you) parts of the "new to Java" section on-line at http://developer.java.sun.com/developer/onlineTraining/new2java/ or get one (or more) of the books listed in the course syllabus. For learning about writing user interface code in Java, we recommend Kathy Walrath and Mary Campione's book "The JFC Swing Tutorial: A Guide to Constructing GUIs" (Addison-Wesley, Reading, MA, 1999). This is available online from: http://java.sun.com/docs/books/tutorial/uiswing/index.html WHAT TO HAND IN All code should compile, and run correctly under Java 1.4. Hand in a printout of your code. By doing this you certify that your code works correctly. If your code does not work correctly you can still hand it in for partial credit, but attach an explanation of what about it does not work. 1. (15 points) [BadStack] This problem relates to the readings from Arthur J. Reil's book, chapter 2. Consider the code in the file: /home/course/cs362/public/homework/BadStack.java This compiles, and if you tested it (and you could, using BadStackTest.java as described in problem 2(b) below), it would work fine. It has javadoc comments and even a JML specification (in the //@ comments). However, it is bad in at least one of Reil's heuristics. Briefly explain all the ways in which it violates these heuristics. Use the line numbers in the file's comments to make it clear what parts of the file you are referring to. 2. [expressions] This problem relates to the readings from Arthur J. Reil's book, chapter 5. It will also teach you something about JUnit testing. Consider the code in the Java files found in the directory: /home/course/cs362/public/homework/expressions/ This code all compiles, and if you tested it (as described below), it would work fine. It has accurate javadoc comments. However, it violates several of Reil's heuristics. a. (20 points) Briefly explain all the ways in which it violates Reil's heuristics. Hand in an explanation of this, which can be handwritten if necessary. b. (30 points) Refactor (i.e., rewrite) the code so that it does not violate these heuristics, but still has the same classes. You may need to add more classes and change inheritance relationships. The code should still compile and pass the tests given in the file ExprTest.java, although you may need to change the declarations of the fixture variables to get the tests to compile. To run the JUnit tests, make sure that the JUnit jar file, which lives in /opt/junit/junit.jar on the department Linux machines, is in your CLASSPATH. To do this you may need to do something like the following (for the bash shell on Linux): CLASSPATH=".:/opt/junit/junit.jar" export CLASSPATH or (for the shell tcsh): setenv CLASSPATH ".:/opt/junit/junit.jar" On a Windows machine, you will have to edit from the Control Panel the System / Advanced / Environment Variables to set the CLASSPATH variable equal to the value .;C:\cygwin\usr\local\junit\junit.jar (assuming you installed junit in the directory C:\cygwin\usr\local\junit\junit.jar, if not adjust the above by changing this file name. Then you can run the tests by simply executing java ExprTest from the directory where you compiled ExprTest and the other classes. Hand in a printout of all of the code for your revision, and a printout of ExprTest if you changed it. Doing this certifies that your code compiles and that the tests all pass. If not, hand in a printout and also an explaination of the problem you had, for partial credit. 3. [PinBallGame] This problem is to get you familiar with objects and classes, interfaces, and inheritance in Java. The program and exercises in this problem are adapted from Tim Budd's book "Understanding Object-Oriented Programming with Java" (Addison-Wesley, 1992). You may want to read the text that accompanies this program at the URL ftp://ftp.cs.orst.edu/pub/budd/java/pinball/pinball.ps (this is a postscript file, which you can print on any printer that understands postscript, like the laser printers in the department, or read on-line using the free reader available at http://www.cs.wisc.edu/~ghost/). You should also read the book "The JFC Swing Tutorial" described above for help on this and the following problem. To start with, copy the files in the directory /home/course/cs362/public/homework/budds_pinball/ into a directory of your own. For example, on linux, do cd ~ mkdir budds_pinball cd budds_pinball cp /home/course/cs362/public/homework/budds_pinball/* . to copy all of these files. a. (15 points) Read the code that you copied. Does it violate any of Reil's heuristics (from chapters 2 and 5)? If so describe which heuristics it violates. If not, give an example of how Budd's code follows each of three (3) separate heuristics from Reil's book. b. (10 points) [Budd's exercise 1, p. 117] The class Peg inherits from ScorePad, which in turn inherits from Hole, which in turn inherits from Ball. For each of these classes, describe all the methods defined in the class that are (i) inherited from parent or ancestor classes, and (ii) for each method inherited from a class that is not the class's direct parent, indicate from which parent class the method's definition occurs. c. (15 points) [Budd's exercise 4] On some platforms it may be difficult to halt the PinBall application once it has finished. Add a button labeled "Quit" to the bottom (South) part of the application window. When pressed, this button should of execute the method call System.exit(0). To do this, you'll have to modify the class PinBallGame to add a new button, and the nested private class MouseKeeper to handle clicks inside the button's region. d. (15 points) [Slo-mo] On my machine the balls move very quickly, making it difficult to see what's happening for debugging purposes. Add a button, next to the Quit button, labeled "Slo-mo" that slows down the animation enough so you can see what's happening more clearly. e. (30 points) [Budd's exercise 6] Currently balls do not test to see if they intersect with other balls. Modify the program by making PinBall implement the PinBallTarget interface, and any multiple listing targets as well as the list of balls. Hand in a printout of any files you have changed in the course of your work on this problem. Highlight (with a highlighter or circle with a pen) the areas of the program that have changed. For each of these areas, write the letter of the subproblem (a-d) next to it, so we can see what part of the problem that solves. It may be helpful to mark such changes with comments in the code also. Be sure your name is on each printout. (You don't need to print out any files that have not changed at all.) 4. (50 points) [Budd's exercise 9] This the program is also about constructing simple user interfaces in Java. It also requires you to implement interfaces from the AWT. See the readings in the previous exercise. Using the program of the previous exercise as an example, write a Java program that places a red circle in the middle of its window. The circle should change color to blue when the mouse enters the window, then return to red when the mouse leaves the window. When the mouse is clicked inside the window, the circle should change color to green and remain green for 1000 milliseconds, before returning to blue. Finally if the mouse is clicked within the bounds of the circle and released outside the circle, circle should be moved so as to be centered on the location of the mouse release. Budd's code (for problem 3) does not include documentation comments, because he has textual explanations in his book surrounding the code. However, you should include documentation comments for all of your classes and methods for this problem. We will subtract 10 points if you have no documentation comments. We will also subtract points if your code violates significant design heuristics, or is hard to read. Hand in a printout of the Java files you wrote for this problem. Each printout should be clearly labeled with your name and this problem's number.