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

Slides:



Advertisements
Similar presentations
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
1 CS2200 Software Development Lecture 29: Polymorphism I A. O’Riordan, 2008 Based on notes by K. Brown.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
The Clock Example. 1. The Clock Example 2 3 public class Clock { // instance variables private int hr; private int min; private int sec; // constructors:
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Object Oriented Programming Object and Classes Lecture 3 MBY.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
State ● What is state? ABCDE. State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
Linked Data Structures
Modern Programming Tools And Techniques-I
GC211 Data structure Lecture 3 Sara Alhajjam.
The need for Programming Languages
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Lecture 10: More on Methods and Scope
Classes and Objects 2nd Lecture
Introduction to Computer Programming
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
null, true, and false are also reserved.
Introduction to Java Programming
Object Oriented Programming
Simple Classes in Java CSCI 392 Classes – Part 1.
Week 4 Lecture-2 Chapter 6 (Methods).
CS360 Client/Server Programming Using Java
Software Design Lecture : 39.
LCC 6310 Computation as an Expressive Medium
Presentation transcript:

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: 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

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.

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.

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

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.

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

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.

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();

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

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

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

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* min*60 + sec; } public String getClockTime() { int hours = seconds / 3600; int minutes = (seconds % 3600) / 60; int secs = (seconds % 3600) % 60; return hours + ":" + minutes + ":" + secs; }

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; }

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