CS 151: Object-Oriented Design September 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 2 Design a Day Class Answer questions such as How many days between now and the end of the year? What day is 100 days from now? A Day object represents a particular day. Some limitations Uses the Gregorian calendar only No time of day No timezone _
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 3 Methods of the Day Class Day(int year, int month, int date) Constructor. int daysFrom(Day d) Return the number of days between day d and the day represented by the object. The return value can be negative if day d comes before, or 0 if it’s the same day. Example: int n = today.daysFrom(birthday); Day addDays(int count) Return the day that is count days from the day represented by the object. The count value can be negative. Example: Day later = holiday.addDays(5); int getYear() int getMonth() int getDate() Getter methods.
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 4 The Day Class Note that d.addDays(n).daysFrom(d) equals n (d + n) - d == n d1.addDays(d2.daysFrom(d1)) equals d2 d1 + (d2 - d1) == d2 Methods daysFrom() and addDays() are not trivial! April, June, September, November have 30 days. February has 28 days, except in leap years it has 29 days All other months have 31 days. Leap years are divisible by 4, except after 1582, years divisible by 100 but not 400 are not leap years. There is no year 0; year 1 is preceded by year -1. In the switchover to the Gregorian calendar, ten days were dropped: October 15, 1582 is preceded by October 4.
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 5 Class Day Implementation, Version 1 Private fields private int year private int month private int date Private helper methods private Day nextDay() private Day previousDay() private int compareTo(Day other) private static int daysPerMonth(int y, int m) private static boolean isLeapYear(int y) Inefficient day-at-a-time calculations. Methods nextDay() and previousDay() do all the dirty work. Extraneous Day objects created and discarded.
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 6 Class Day Implementation, Version 2 Instead of private year, month, and date fields, use a Julian day number, the number of days from January 1, 4713 BCE. private int julian Now public methods daysFrom() and addDays() are trivial. Private helper methods do all the dirty work of converting back and forth between Julian numbers and [year, month, date]. private static int toJulian(int year, int month, int date) private static int[] fromJulian(int j) Conversion required for each access of year, month, or date.
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 7 Class Day Implementation, Version 3 Keep year, month, date, and julian number fields. Do conversions between [year, month, date] and julian number only when necessary (lazy conversion). Convert to julian number only when doing date arithmetic. Convert to [year, month, date] only if calling a get * () method. Need two private booleans fields and two private methods to keep [year, month, date] and julian number fields synchronized: private boolean ymdValid private boolean julianValid private void ensureJulian() private void ensureYmd()
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 8 Importance of Encapsulation Three different implementations of the Day class. Each version presents the same public interface. Encapsulation hides the implementation details. Principles: A class should expose as few public fields and public methods as possible. All other fields and methods should be hidden from class users by making them private. Supports reliability and flexibility. _
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 9 Assignment #2 Write the first draft of your Design Specification for the Rock-Paper-Scissors game application. Due Tuesday, September 24 _
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 10 Assignment #2 CRC cards for your classes. You do not need to turn in actual index cards or to draw boxes to represent the cards. For each card, simply give the name of the class and then list the class responsibilities and list the collaborators (dependent classes). _
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 11 Assignment #2 UML class diagrams for your classes. You can optionally put the class diagrams inside UML package diagrams. Show the relationships between classes using the appropriate connectors. At this early design stage, you don’t need to show attributes and methods in the class diagrams, and you don’t need to label associations with attribute names. However, you should show any multiplicity. _
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 12 Assignment #2 UML sequence diagram Show the communication patterns among your objects at run time for key operations of your application. At this early design stage, you should probably not have more than 10 to 12 classes and interfaces. Use a UML drawing tool to create the diagrams and insert the diagrams into your specification. Violet : StarUML:
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 13 Assignment #2 Design tips The way each player’s choice (rock, paper, or scissors) is input will change. Early versions of the program will be command-line-based and later versions will be GUI-based. You will soon be exploring different algorithms for how the computer makes its choice.
SJSU Dept. of Computer Science Fall 2013: September 12 CS 151: Object-Oriented Design © R. Mak 14 Assignment #2 Update your Functional Specification Add and modify requirements or use cases, etc. Turn on “Track Changes” under the “Tools” menu so that the changes to the document are obvious. both documents as attachments to Subject: CS 153 Assignment #2 team name _