Skip to main content.

Assignment 8: Refactoring Code for Extensibility

Due: before class next Monday, November 7

Objective: This assignment is intended to get you to practice some of the programming and design techniques discussed in class recently and consists of the following activities:

Set Up

Understanding the Design

Roulette Specification

Execute the code. What is the main driver for this project?

There are many different ways to bet on the results of spinning a roulette wheel. The current program only supports three such ways; however, the next version of the program should support three additional ways to bet on the results.

Analyze the design of the current program from the perspective of how open it is to adding new kinds of bets and how closed it is to modification. To help direct your analysis, consider the following questions:

  1. What are each class's responsibilities?
  2. How many other classes know about the Bet class?
  3. What code would need to be added to Game to allow the user to make another kind of bet that paid one to one odds and was based on whether the number spun was high (between 19 and 36) or low (between 1 and 18)?

Note: There is one bug in the code: determining if Odd/Even Bet was won is incorrect.

Refactoring

Examine the code given and refactor it according to the Open-Closed Principle as well as the other design principles we have studied. You may create any new classes you want to solve this problem.

To help direct your refactoring, consider the following questions:

  1. What methods would make sense as behaviors of a Bet hierarchy (i.e., make bets open to extension)?
  2. What methods would help improve the code in the Game's methods (i.e., close it to modification)?
  3. What methods can be completely implemented in the Bet class (i.e., are constant across the hierarchy), and which completely in the Bet child classes (i.e., must be specialized across the hierarchy)?
  4. How should the Game class create the correct Bet child objects, and thus leverage the Bet child classes' functionality?

You are most focused on extensibility but also look for other code smells in your code.

Testing

You should provide a set of tests that verify your modified program still works as intended. Tests can be in the form of input data files, driver programs, or other classes that verify every line of code written works as intended. Use the best/most appropriate form of testing for the specific code under test.

To help direct your testing, consider the following questions:

  1. How can you test a program that is primarily based on randomness?
  2. Could you easily remove the randomness?
  3. Could you make it easy to turn randomness on and off?
  4. Could you make it easy to test several different kinds of bets?

Improving

Using your improved code, add the three bets given in the original specification not implemented in the code given to you, adding as little new code as possible.

In a text file, briefly compare the effort to create a new bet in your improved code to how you would have done it in the original code. To focus your answer, consider the following questions:

  1. In what ways is the refactored code simpler?
  2. In what ways is the refactored code more complex?
  3. What trade-offs did you make when refactoring the old code?
  4. Which code do you prefer and why?

Submission

Using Eclipse, make a jar file from your project, which should include your source code, in a file called assign9.jar Save the file in your cs209 directory. See Assignment 6 if you need reminders about how to copy your file to the CS department servers.

Submit your file using submitJar.sh assign8

Grading Criteria (200)

Your grade will be based on