Download presentation
Presentation is loading. Please wait.
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: 1.Obtain statement of problem 2.Sketch sample scenario 3.Work out what objects are involved for one class at a time: 4.Work out how those objects are meant to behave 5.Design the interface for class 6.Define variables 7.Implement methods 8.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
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. 3. what objects are involved? ●Examine the problem statement, and decide what are the most important “entities” – they will be the objects user events a collection of events a unit of a calendar an element of a day
6
Identifying object and methods Ways of Identifying object and methods: Simple (linguistic) approach: 1.Look for nouns (things) in the requirements specification – these are potential objects (candidates) 2.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
Other methods ●Use Cases ●User Stories ●Client/customer interviews; Domain Analysis ●Responsibilities-based – group things that offer common services schedule 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 Note: Keep reuse in mind
11
Time Example The Time class illustrated Time + Time(int, int, int) + getSeconds(): int + getClockTime(): String + getSecondsUntil(Time):int + getTimeUntil(Time):String + isBefore(Time): boolean + isEqual(Time): boolean
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 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); } Note: test every method, under different conditions Note: test every method, under different conditions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.