Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- HW04   


See Assignments Page for due date!

You will need to use Full Java language level for this assignment.

Be sure to have downloaded and installed StarUML before starting the first part of problem 2.

Save all of your work in a directory called HW04 on your U: drive (Owlnet account) inside a directory called Comp201 .

You will be writing JavaDocs for this assignment.  Click here for more information about JavaDocs.

  1. (50  pts total) The supplied code for Ballworld used in HW03 contains a class called Randomizer whose methods are all static.  In this exercise, you are to transform Randomizer into a Singleton class and modify the code of Ballworld used in HW03 to work with the new implementation of Randomizer

    Create a subdirectory called prob1 in HW04.  Copy the code for Ballworld in HW03 into prob1.  Copy the entire main Ballworld directory, including the DrJava project

    Modify the code for Ballworld as specified in the following.

    1. (10 pts) Define an interface, IRandomizer, that contains all the methods of Randomizer, none of which should be static.   Add a javadoc description of the interface.   This comment goes right above the class declaration.  See the supplied code for examples.
    2. (5 pts) Define Randomizer to implement IRandomizer using the Singleton pattern.
    3. (5 pts) Add the following method to your Randomizer class
      
      	/**
      	* Returns a random choice of one of two objects, x and y, where probX is the probability 
      	* that x will be picked (0<=x<=1)..
      	*/
      	public Object randomChoice(Object x, Object y, double probX) {
      	    return (Math.random() < probX) ? x: y;
      	}
      FYI: the syntax [[boolean expression] ? [result1] : [result2] evaluates to result1 if the boolean expression is true and evaluates to result2 if the boolean expression is false.
    4. (2 pts) Add the randomChoice method to the IRandomizer interface as well.
    5. (28 pts total) Now that you have redefined Randomizer, the code for Ballworld will not compile.  The compiler will basically tell you that it does not recognize the calls to the static methods of the old class Randomizer.  You will need to rewrite BallControl and the appropriate concrete ball subclasses to properly use the new Randomizer singleton object.  The basic idea is to add an IRandomizer field to each of the classes that need randomization and pass a concrete IRandomizer to the constructor in order to initialize the IRandomizer field.  In doing so, you now have the freedom to use any IRandomizer implementation that you like.
      1. (2 pts) Add an IRandomizer field to BallControl; 
      2. (3 pts) Change the BallControl constructor to accept an IRandomizer parameter and use this parameter to initialize the IRandomizer field.
      3. (3 pts) Find the main method in BallControl and modify the call to the constructor of BallControl so that it is handed the singleton instance of Randomizer.
      4. (5 pts) Replace all calls in BallControl to the static methods of the old Randomizer class with the corresponding method calls on the IRandomizer field.
      5. (15 pts) Repeat the same code modification to CurveBall and any other ABall subclasses that make use of the old Randomizer. Do not modify their constructors however, as this will cause the dynamic class loading to break. Just create a private IRandomizer field and initialize it to the Randomizer singleton.

      Your modified Ballworld code should now compile and run.

       

  2. (80 pts total)  Pizza Unlimited will customize their pizzas to any shape you want.  Ready to go are 4" x 6" rectangular pizzas and 5" diameter round pizzas.  The pizzas come in two standard kinds of crust: thin and thick.  The thin crust is chewy and costs the house $0.15/in2 to make.  The thick crust is crunchy and costs the house $0.17/in2 to make.  Besides these standard crusts, Pizza Unlimited will customize the crust to your specification as well.  Of course, the cost per in2 for customized crusts will vary.  The pizzas of different shapes and crusts are sold at various prices.  Pizza Unlimited wants to know which the most profitable kind of pizza is.  For example, is the 4" x 6" thin crust at $4.99 more profitable than the 5" round thick crust at $4.69?

    First, create a subdirectory under HW04 called prob2.  Create the src and classes subdirectories.  Create a project in StarUML using the the src directory as the default directory.   Save the StarUML project and the code it generates to the src directory.

    Create a project in DrJava  and save it in the prob2 directory.    Set the build directory to be the classes directory.

    You will need to add javadoc documentation to every class, every method (including all parameters and return values) and every field you write.   

    1. (10 pts) Use a UML class diagram to describe the design of your pizzas.  Hint: is it the pizza or the crust that has a shape? Use StarUML to create a UML diagram of your design.
    2. (20 pts) In your DrJava project, load the code you just created using StarUML and  finish writing the Java code for your pizza design.    Write appropriate JUnit test classes for each of the classes in your design. The test classes will have appropriate test methods as required by the problems listed below. 
    3. (5 pts) Override the toString() method in appropriate classes of your pizza design to return a String representation of a pizza that shows its price, its shape with the relevant dimension, and the crust type.  For example, the standard 4" x 6" thin crust at $4.99 has the String representation: [$4.99 Rectangle(4, 6) thin and chewy].  
      • (5 pts) Write an appropriate JUnit test method in the appropriate test class. The test method  must cover all possible combination of shapes and crusts. 
    4. (10 pts) Given two pizzas, each of certain shape, crust, and price, write an appropriate class to find out which one is more profitable and return it.  
      • (10 pts) Write an appropriate JUnit test method in the appropriate test class. What cases should the test cover?  Explain your reason for testing only those cases. 
    5. (10 pts) Given three pizzas, each of certain shape, crust, and price, write an appropriate class to find out which one is more profitable and return it.  
      • (10 pts) Write an appropriate JUnit test method in the appropriate test class.  What cases should the test cover?  Explain your reason for testing only those cases. 

      .

  3. (85 points total) In this problem, you will take your old Ballworld code and modify it to use strategies instead of ABall subclasses. Your final code should match the UML diagram given in Lec10.  

    You will need to add javadoc documentation to every class, every method (including all parameters and return values) and every field you write.   

     

    1. Copy all your existing Ballworld code, including the DrJava project, to a new "prob3\Ballworld2" subdirectory inside your HW04 directory.
    2. You will need a few updated files that will simply replace the ones your have in your new Ballworld2\src\ballworld package directory: BallControl.java, BallGUI.java, BallControl2.java and BallGUI2.java (Don't overwrite the code in your other directories!!!)
    3. ( 10 pts) Create a new interface in the ballworld package called IUpdateStrategy that has one method called updateState that takes a Ball (not ABall!) object as a parameter and returns void.
    4. (10 pts total) Copy your ABall.java file to a new file called Ball.java and modify the code inside Ball.java such that
      1. (3 pts) the class and its constructor are called Ball.
      2. (3 pts) There is a new field of type IUpdateStrategy
      3. (4 pts) The IUpdateStrategy field is initialized by the constructor to the value supplied by a new input parameter to the constructor. This new input parameter should be added to the end of the list of input parameters. (See the UML diagram for reference).
    5. (5 pts) Copy the code from the Ballworld web page for the SwitcherStrategy and MutliStrategy.
    6. (40 pts) Create subclasses of IUpdateStrategy called StraightStrategy, ColorStrategy (aka "ColorChanging"), BreathingStrategy, and CurveStrategy with the corresponding behaviors.
    7. Test that your code runs by using the BallControl class. In DrJava, just right-click the BallControl class and select "Run Document's Main Method".
    8. If you had problems getting Prob. 1 above to work properly, do the following:
      1. Rename your IRandomizer.java and Randomizer.java files to IRandomizer.java.broken and Randomizer.java.broken respectively.
      2. Copy the following class files into your ballworld package directory, overwriting the existing class files: IRandomizer.class and Randomizer.class (Note: it is an Honor code violation to attempt to decompile any supplied class files.)
    9. (10 pts total) Write a strategy called DrunkenStrategy:
      1. (5 pts) DrunkenStrategy hold two instances of IUpdateStrategy, which are initialized to specific concrete strategies of your choice.
      2. (5 pts) Using the IRandomizer.randomChoice method, randomly pick on the those two strategies to use when the DrunkenStrategy is asked to updateState. Choose whatever probability you'd like, though non-50% probabilities make more interesting behavior (try different values!)..
        The syntax to use randomChoice requires a "downcast" because the return value of randomChoice is of type "Object", not "IUpdateStrategy". The syntax to downcast something, for instance here, "x", to"MyClass" is "(MyClass) x". You can call methods directly on the downcasted entity as such: "((MyClass) x).myMethod()"
      3. (10 pts) Increase the number of strategies held to 4 strategies and modify/augment your updateState code so that 1 of the 4 strategies is picked at random. Don't overwrite your old code. Comment it out and write your new code below it.
    10. Test that they work by using running the main method of BallControl2 (change the properties of your project at this point to use BallControl2 as the main method invoked by the F4 key). If you hold the mouse over the various buttons and textfields, a "tool tip" will pop up to tell you what each one does.
    11. Enjoy!

215 points total.

 

When you have completed your homework, zip up the entire HW04 directory and submit the zipped file using the file upload link on the Assignments Page.

 

 


Last Revised Thursday, 03-Jun-2010 09:50:22 CDT

©2008 Stephen Wong and Dung Nguyen