1 / 15

CS2200 Software Development

CS2200 Software Development. Class Design Example: The Time Class A. O’Riordan, 2008 (parts adapted from lecture notes by K. Brown). Simple Class Design Process. For small problems: Obtain statement of problem Sketch sample scenario Work out what objects are involved

branxton
Download Presentation

CS2200 Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2200 Software Development • Class Design Example: The Time Class • A. O’Riordan, 2008 • (parts adapted from lecture notes by K. Brown)

  2. Simple Class Design Process For small problems: • Obtain statement of problem • Sketch sample scenario • Work out what objects are involved for one class at a time: • Work out how those objects are meant to behave • Design the interface for class • Define variables • Implement methods • Test class from Arnow and Weiss, Introduction to Programming using Java, Addison Wesley, 1999

  3. 1. A statement of the problem I have a busy schedule, with many appointments and meetings to arrange each day. I want a program which can tell me • the time of any appointment, • how long I have to wait until an appointment, and • which of two appointments comes first.

  4. 2. A sample scenario My program obtains the current time. It displays the current time to me. It obtains an appointment. It tells me the time of that appointment and how long I have to wait, in seconds. It obtains another appointment. It tells me which of the two appointments come first, and how long I have to wait between them.

  5. a collection of events events user a unit of a calendar an element of a day 3. what objects are involved? • Examine the problem statement, and decide what are the most important “entities” – they will be the objects I have a busy schedule, with many appointments and meetings to arrange each day. I want a program which can tell me the time of any appointment, tell me how long I have to wait until an appointment, and tell which of two appointments comes first. I have a busy schedule, with many appointments and meetings to arrange each day. I want a program which can tell me the time of any appointment, tell me how long I have to wait until an appointment, and tell which of two appointments comes first.

  6. Identifying object and methods • Ways of Identifying object and methods: • Simple (linguistic) approach: • Look for nouns (things) in the requirements specification – these are potential objects (candidates) • Look for verbs (actions) in the requirements specification – these are potential methods • Example: • Candidate Objects: User, Event, Schedule, Time, Day • For Time, we can compare times, get the duration until an event, etc.

  7. schedule Other methods • Use Cases • User Stories • Client/customer interviews; Domain Analysis • Responsibilities-based – group things that offer common services User

  8. 4. work out how the objects are meant to behave • What an object can do or what can be done to an object. • We focus on the “Time" object: • each Time object should be created from a statement of the hour, the minutes and the seconds • each Time object should be able to format itself as a (useful, readable) String† • each Time object should be able to state whether it is the same as, before or after another Time object • each object should be able to state the number of seconds between it and another Time object †Here we are ascribing human qualitites to the objects, e.g. “Object, draw thyself”. This is called anthropomorphism. And is sometimes used in OOD.

  9. 5. Determine the interface • what should a Time object look like to other objects? Time now = new Time(14, 8, 25); Time app = new Time(16, 0, 0); JOptionPane.showMessageDialog(null, "Time now: " + now.getClockTime() + "\nAppointment: " + app.getClockTime() + "\nSeconds to wait: " + now.getSecondsUntil(app) + "\nHours to wait: " + now.getTimeUntil(app)); Time other = new Time(15, 55, 0); Time first; if (app.isBefore(other) == true) first = app; else first = other; JOptionPane.showMessageDialog("First appointment is at: " + first.getClockTime();

  10. The interface for Time • the interface only shows the public methods (and any public data) public Time(int, int, int); //hour, minute, sec public int getSeconds(); public String getClockTime(); public int getSecondsUntil(Time); public String getTimeUntil(Time); public boolean isBefore(Time); public boolean isEqual(Time); Note: Keep reuse in mind

  11. Time + Time(int, int, int) + getSeconds(): int + getClockTime(): String + getSecondsUntil(Time):int + getTimeUntil(Time):String + isBefore(Time): boolean + isEqual(Time): boolean Time Example • The Time class illustrated

  12. 6. Define the variables • sometimes we will want to report hours, minutes and seconds; other times we will want to handle total number of seconds • internal representation does not have to be the same as the interface • for internal computation, seconds is easier - we don't have to do complex conditional comparisons //instance variables private final int seconds; //secs since midnight

  13. 7. Implement the methods public class Time { //instance variables private final int seconds; // seconds since midnight //Constructor public Time(int hour, int min, int sec) { seconds = hour*3600 + min*60 + sec; } public String getClockTime() { int hours = seconds / 3600; int minutes = (seconds % 3600) / 60; int secs = (seconds % 3600) % 60; return hours + ":" + minutes + ":" + secs; }

  14. public int getSeconds() { return seconds; } public int getSecondsUntil(Time other) { return other.getSeconds() - seconds; } public String getTimeUntil(Time other) { int interval = other.getSeconds() - seconds; return (interval/3600) + ":" + (interval%3600)/60 + ":" + (interval%3600)%60; } public boolean isBefore(Time other) { if (seconds < other.getSeconds()) return true; else return false; } public boolean isEquals(Time other) { if (seconds == other.getSeconds()) return true; else return false; } }

  15. 8. Testing Note: test every method, under different conditions public static void main(String[] args ) { Time now = new Time(14, 8, 25); Time app = new Time(16, 0, 0); JOptionPane.showMessageDialog(null, "Time now: " + now.getClockTime() + "\nAppointment: " + app.getClockTime() + "\nSeconds to wait: " + now.getSecondsUntil(app) + "\nHours to wait: " + now.getTimeUntil(app)); Time other = new Time(15, 55, 0); Time first; if (app.isBefore(other) == true) first = app; else first = other; JOptionPane.showMessageDialog(null, "First appointment is at: " + first.getClockTime()); System.exit(0); }

More Related