Assignment 2: Object-Oriented Design
Due Wednesday October 21st, 2009 11:55PM
This assignment covers topics in requirements and design lectures and in the book, Head First Object-Oriented Analysis and Design. Before starting, you will want to read chapters 6 through 9 in the book. You will also need to verify your UML syntax against a UML reference when you complete diagramming questions as the graders will be deducting for syntax mistakes.
-
(45 points)
Presume for a moment that you are contracting with a customer who runs
a fitness center. This customer wants you to write a piece of software
to help her manage her business. You have asked for information about
how the business runs and she has given you this unusually complete and
simple description:
Ignoring the user interface for a moment, you will be designing a piece of software which can accommodate the above requirements.
My center is a membership-only affair; every member has, for any month, one of two kinds of membership. The first one is the basic membership which just allows the customer access to most of the facilities: the weight room, pool, and so on. But if the customer has a premium membership, he or she can make appointments with a personal trainer as part of the deal. We allow up to five appointments a month with the premium membership, although anyone can make an appointment if they want to pay a fee. The customer might choose to pay this fee to get more than five appointments in a month or if he or she doesn't have a premium membership.
I need to be able to keep track of the customers, their memberships, the personal trainers, and their appointments. I also have to have a history of this information; if a customer switches from a basic to a premium membership, I need to know when they switched and how long they held the old membership. I also need to be able to keep track of when appointments happen so I don't double-book my trainers.
I might have to change my pricing at some point, so I need a way to keep track of what the prices of memberships were and when. But when I change my pricing, it shouldn't affect current customers; they keep paying at the old rate until they change their membership.
Finally, it's possible for members to lapse in their payments. I want to be forgiving to long-standing customers; they can be late up to a month without being refused entry to the fitness center. But I only want to be that lenient to members who have had some kind of membership for the last six months or more; everyone else has to pay on time or be suspended until they're paid up.
- (15 points) Draw a UML class diagram which illustrates the above model. Make sure to include appropriate relationships on the diagram, notation indicating the arity of these relationships (1-to-1, 1-to-N, etc.), and a few functions and members that are associated with key entities.
- (10 points) Draw a UML state diagram which indicates the different states a customer can be in. Example states might include current, overdue, suspended, and inactive. Use the event[guard]/action syntax to indicate what causes state transitions in your diagram.
- (10 points) Draw a UML activity diagram which shows the use case for a customer making an appointment with a trainer. Remember to take into account scheduling conflicts and whether or not the customer has to pay a fee for the appointment.
- (10 points) Requirements are rarely complete the first time they are provided. Consider the above requirements and the model of a membership-oriented business. Suggest a requirement which may exist even though the owner has not expressed it and explain how you would adjust one of the above diagrams to accommodate this change.
-
(30 points Chapter 8 of the HFOOA&D textbook describes a
number of software principles, among them DRY, LSP, OCP, and SRP.
Identify in each of the following cases which of these principles is
violated and how the code can be corrected.
-
(15 points) Consider the following snippet of code from a
hypothetical 2D action game set in space. Hint: what if we
wanted to add asteroids to our game too?
public class SpaceObject { public static final int PLAYER_TYPE = 0; public static final int MONSTER_TYPE = 1; private int x; private int y; private int type; public void makeMove() { switch (type) { case PLAYER_TYPE: makePlayerMove(); break; case MONSTER_TYPE: makeMonsterMove(); break; default: throw new IllegalStateException("Bad type: " + type); } } ... } -
(15 points) Consider the following snippet of code from a
GUI component from a hypothetical game of checkers.
protected void paintComponent(Graphics g) { for (int y=0;y<model.boardHeight();y++) { for (int x=0;x<model.boardWidth();y++) { g.setColor(Color.WHITE); g.fillRect(x,y,getCellWidth(),getCellHeight()); g.setColor(Color.BLACK); g.drawRect(x,y,getCellWidth(),getCellHeight()); // draw piece if (model.getPlayerOccupying(x,y)==Player.PLAYER1) { g.setColor(Color.BLACK.brighter()); int nx = x+2; int ny = y+2; int nw = getCellWidth()-4; int nh = getCellHeight()-4; g.fillOval(nx,ny,nw,nh); g.setColor(Color.BLACK); g.drawOval(nx,ny,nw,nh); } else if (model.getPlayerOccupying(x,y)==Player.PLAYER2) { g.setColor(Color.RED.brighter()); int nx = x+2; int ny = y+2; int nw = getCellWidth()-4; int nh = getCellHeight()-4; g.fillOval(nx,ny,nw,nh); g.setColor(Color.RED); g.drawOval(nx,ny,nw,nh); } } } }
-
(15 points) Consider the following snippet of code from a
hypothetical 2D action game set in space. Hint: what if we
wanted to add asteroids to our game too?
-
(15 points) Chapter 9 of the HFOOA&D textbook describes the
difference between contractual programming and defensive programming.
In each of the following cases, describe which should be used and
briefly justify your answer.
- (5 points) Code for desktop publishing software which interacts with user-provided third-party plugins.
- (5 points) Code which operates on a resource-limited device such as a personal music player.
- (5 points) Code which runs on a mainframe in a nuclear power plant that manages reactor coolant levels.
-
(10 points) Draw a UML sequence diagram illustrating the
execution of the following program. (You are not required to trace
through code which is not shown below.
public class SimpleProgram { public static void main(String[] arg) { SomeClass a = new SomeClass(); a.foo(5); SomeClass b = new SomeClass(); b.foo(5); a.foo(6); SomeOtherClass c = new SomeOtherClass(); c.bar(3); } } class SomeClass { public void foo(int x) { System.out.println(x*x); } } class SomeOtherClass { public int bar(int x) { int y = baz(x); int z = baz(x); return y+z; } public int baz(int x) { return x+1; } }
Submission Instructions
Submit your answers via WebCT. Handwritten UML diagrams are acceptable as long as they are legible. If we cannot read your diagram with a reasonable amount of effort, we will not assign credit for it. You may wish to use a UML diagramming application. Your homework must be submitted electronically; we will not accept paper copies.