100 likes | 120 Views
Learn the process of transitioning a problem description to a class diagram in an object-oriented design. This guide outlines a step-by-step approach, starting from understanding the problem, analyzing sentence structure, defining classes, methods, and attributes, and designing constructors. A lab exercise involving a movie ticket vending machine is provided for practical application.
E N D
Object-oriented design: from problem descriptionto class diagram Glenn David Blank Computer Science & Engineering Department Lehigh University, Bethlehem, PA, USA With support from the National Science Foundation (Grants No. EIA-0087977 and 0624553) and the Pennsylvania Infrastructure Technology Alliance
From problem description to class diagram (in nine steps) 1. Read the problem description at least twice. • Ask questions about anything you don’t understand or needs more explanation. The ATM verifies whether the customer's card number and his her PIN (a number) are correct. If it is, then the customer can check the account balance, deposit cash, and withdraw cash. Checking the balance simply displays the account balance. Depositing asks the customer to enter the amount, then updates the account balance. Withdraw cash asks the customer for the amount to withdraw; if the account has enough cash, the account balance is updated. The ATM prints the customer’s account balance on a receipt.
Sentence grammar analysis 2. Put an ‘S’ next to each subject, a ‘V’ next to each verb and an ‘O’ next to each object. The ATM verifies whether the customer's card number and PIN are correct. S V O O O If it is, then the customer can check the account balance, deposit cash, S V O V O and withdraw cash. Checking the balance displays the account balance. V O S O V O Depositing asks the customer to enter an amount, then updates the balance. S V O V O V O Withdrawing asks the customer for the amount to withdraw; if the account has S V O O V S V enough cash, the balance is updated. The ATM prints the customer’s O S V S V O account balance on a receipt. O O
Analyze subjects and objects The ATM verifies whether the customer's card number and PIN are correct. S V O O OIf it is, then the customer can check the account balance, deposit cash, and withdraw cash. S V O V O V O Checking the balance simply displays the account balance. S O V O Depositing asks the customer to enter the amount, then updates the account balance. S V O V O V O Withdraw cash asks the customer for the amount to withdraw; if the account has enough cash, S O V O O V S V O the account balance is updated. The ATM prints the customer’s account balance on a receipt. O V S V O O 3. Analyze each subject and object as follows: • Does it represent a person performing an action? Then is an actor, ‘R’. • Is it also a verb (such as ‘deposit’)? Then it may be a method, ‘M’. • Does a subject take a simple value, such as ‘color’ (string) or ‘money’ (number)? Then it is probably an attribute, ‘A’. • Which remaining unmarked noun occurs most frequently? Make it ‘C’ for class. • In Eclipse, draw a class for your first class. C A M R A M M A M A R A M A A M A A A M R A R A A M A A C
Analyze methods and attributes 4. If a verb has a non-actor object it’s a method, M 5. Distinguish attributes, A, and parameters, P: • If it doesn’t distinguish instances of a class or store information about objects, it’s probably a parameter, not an attribute • Add attributes to a class in the UML class diagram • Add methods in the class diagram • While adding methods, include the parameters • See the return type of your methods: • “get” methods take the type of the attribute returned • “set” methods take “void” return type • compute methods take the type of the computed value • Methods that answer true/false questions take boolean type
Constructors 6. Design one or more constructors for the class. • A constructor creates an instance of a class. • A constructor has the same name as the class—e.g., class Person has constructor Person(). • A constructor has no return type, but may take parameters—to initialize attributes.
Any more classes? 7. If any remaining class has no attributes, it’s probably an attribute of another class 8. Otherwise, add another class to the diagram and go back to step 4 (analyze its methods and attributes) 9. Draw association links between the classes Check your design to make sure it makes sense!
Lab exercise: Design a movie ticket vending machine The movie ticket machine displays the movie title, displays the show time (e.g. “2006-11-16 7:00pm”), and displays the price of a ticket (e.g. 8.50). A customer enters money (e.g. 20.50) into the machine. The machine displays the customer’s balance (e.g. 20.50). The customer enters the number of tickets (e.g. 1) into the machine. The machine prints the tickets, and returns the customer’s balance (e.g. 12.00). Print tickets also tracks the number of available seats in the theater so it can tell the customer when the available seats are sold out.