Advanced Object Oriented Programming Chapter 3 & 4.

Slides:



Advertisements
Similar presentations
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 3 (Horstmann’s Book) Guidelines for Class Design Hwajung Lee.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Class design guidelines. 2 Encapsulation Classes can be implemented many different ways –each has advantages & disadvantages –improvement/revision always.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 3b Standard Input and Output Sample Development.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 3: Guidelines for.
18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Objects and Classes (cont)
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Java Classes Using Java Classes Introduction to UML.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Polymorphism and interfaces Horstmann ch 4. Outline Interface Polymorphism Function object Anonymous class User Interface Action Scope of variables (Large)
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Guidelines for class design Horstmann ch Noter ch.1.1.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Chapter 10 Introduction to Classes
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CS 151: Object-Oriented Design September 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 3 Guidelines for Class Design. Objective of this chapter Have “bottom up point of view” Learn how to write a single class well.  The classes.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Object-Oriented Principles Applications to Programming.
Shlomo Hershkop Advanced Review. Shlomo Hershkop Advanced Review Time classes Time classes Date Classes Date Classes File input/output File.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS 151: Object-Oriented Design September 10 Class Meeting
Lect Chap 3 (cont.) 2/2. Chapter 3 The Importance of Encapsulation Even a simple class can benefit from different implementations Users are.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
1 Using const in C++ Classes In the presence of pointers we must take steps to ensure the integrity of the object Can use const method definitions The.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
CS 151: Object-Oriented Design September 17 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
A High Flying Overview CS139 – Fall 2010 How far we have come.
COMPUTER 2430 Object Oriented Programming and Data Structures I
ITEC324 Principle of CS III
Guidelines for Class Design
Interface types and Polymorphism
Chapter 10 Thinking in Objects
Classes and Objects: Encapsulation
Chapter 10 Thinking in Objects
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
Chapter 10 Thinking in Objects
CMPE 135: Object-Oriented Analysis and Design September 12 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
2.5 Another Java Application: Adding Integers
Chapter 4 Interface Types and Polymorphism Part 1
Classes and Objects 2nd Lecture
Classes and Objects: Encapsulation
CSC 480 Software Engineering
Chapter 9 Thinking in Objects
Classes and Objects Encapsulation
Chapter 9 Thinking in Objects
Week 4 Lecture-2 Chapter 6 (Methods).
CMPE 135: Object-Oriented Analysis and Design February 21 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor:
CMSC 202 Encapsulation Version 9/10.
ITEC324 Principle of CS III
Chapter 4 Interface Types and Polymorphism Part 1
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Advanced Object Oriented Programming Chapter 3 & 4

Motivation Focus of this course: Use object oriented techniques to program reusable modules that can be used (and reused) for building larger applications. Reuse is good: reused software components get tested in many different programs! They become more reliable.

How to Bottom up We will learn how to design libraries of useful classes and objects that can help in many programs. This is difficult, we have to make life easy for programmers that will use our classes!

Topics Encapsulation, Users of a class should be kept unaware of implementation details (implementations might change,users should not need to change their own code!) Contracts, Specify in detail responsabilities of the implementor and the caller to increase reliability and efficiency. (comming later in the course, when we discuss Unit Testing) Qualities, Cohesion, completeness, conveinience, clarity, consistency. Interfaces & Polymorphism Separating the interface of a class from the code that implements it. Many classes can implement the same interface and we can write programs that can work on any of these (polymorphism)

Dates and Calenders- class Day Time in programs boolean after(Date other) This date after other? boolean before(Date other) This date before other? int compareTo(Date other) Which date came before? long getTime() Milliseconds gone since epoch void setTime(long n) Set to milliseconds since epoch

Calenders in the word There are different calendar systems aroud the world. Programs might be required to deal with years, months and days in one or more of them. Calendars in use nowadays are Gregorian, Chinese, Hebrew and Islamic.

Dates and Calenders The (abstract) class java.util.Calendar Other classes will take care of translating back and forth from points in time to calendar descriptions. A lot of functions are already dened in Calendar. This separation of concerns is good design! The class Date can be used for other purposes too!

Designing a Day class Many programs work with days for example March 31, 2010 For the Date class the most useful operation was before, efter What should be the responsabilies of Day class 1)How many days from now to…? 2)What day is 100 days from nu ?

What users of the class expect (Designing the class) public class Day public Day(int aYear, int aMonth, int aDate) public int getYear() public int getMonth() public int getDate() public Day addDays(int n) public int daysFrom(Day other)... d.addDays(n).daysFrom(d) == n d1.addDays(d2.daysFrom(d1)) == d2 As you see, there is one constructor, 3 accessors and 2 ways of using days. But there are no mutators!

Test the Day class public class DayTester { public static void main(String[] args){ Day today = new Day(2001, 2, 3); Day later = today.addDays(999); System.out.println( later.getYear() + "-" + later.getMonth() + "-" + later.getDate()); System.out.println( later.daysFrom( today )); }

Designing classes-a first implementation public class Day { private int year; private int month; private int date; addDays and daysFrom are tedious to implement: 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

Day- first implementation public Day addDays(int n){ Day result = this; while (n > 0){ result = result. nextDay() ; n--; } while (n < 0){ result = result. previousDay() ; n++; } return result; } Private methods that might use other private filds. We do not make them public because for other implementations they might no be even dened!

Day- first implementation public int daysFrom(Day other){ int n = 0; Day d = this; while (d.compareTo(other) > 0){ d = d. previousDay() ; n++; } while (d.compareTo(other) < 0){ d = d. nextDay() ; n--; } return n; } complete implementation in the labb

A second implementaion For greater eciency, use Julian day number Used in astronomy Number of days since Jan. 1, 4713 BCE May 23, 1968 = Julian Day 2,440,000 Greatly simplies date arithmetic

A second implementation public Day(int aYear, int aMonth, int aDate){ julian = toJulian(aYear, aMonth, aDate); } public Day addDays(int n){ return new Day( julian + n); } public int daysFrom(Day other){ return julian - other.julian; } public int getYear(){ return fromJulian(julian)[0]; } private int julian; Arithmetic becomes ecient, but not the constructor and the selectors!

Encapsulation (information hiding) Even a simple class can benefit from different implementations! Hiding the representation and auxiliary methods from users of the class makes the user unaware of the differnt implementations! Imagine having the fields public. The users might have used d.year d.year++ It is not easy to change the user code everywhere if the implementation changes to not have such a eld!

Encapsulation Do not supply a mutator for every accessor! They might lead to inconsistent states! (In our example, not all days are valid!) Having a setDate might lead to Day deadline = new Day(2001, 1, 31); deadline.setMonth(2); // ERROR deadline.setDate(28); or Day deadline = new Day(2001, 2, 28); deadline.setDate(31); // ERROR deadline.setMonth(3);

Sharing mutable references class Employee{... public String getName() return name; public double getSalary() return salary; public Date getHireDate() return hireDate; private String name; private double salary; private Date hireDate; } In a program dealing with employees Employee harry =...; Day d = harry. getHireDate() ;

Sharing mutable references Date is mutable Employee harry =...; Date d = harry.getHireDate(); d. setTime(t) ; // changes Harry's state!!! Cloning For mutable classes like Date do as follows public Date getHireDate() return (Date) hireDate.clone() ;

Qualities of a class interface What criteria should a class interface meet? 1 Cohesion 2 Completeness 3 Convenience 4 Clarity 5 Consistency

Cohesion Class describes a single abstraction Methods should be related to the single abstraction public class Mailbox { public addMessage(Message aMessage) {... } public Message getCurrentMessage() {... } public Message removeCurrentMessage() {... } ! public void processCommand(String command) {... }... }

Completeness Support all operations that are a part of abstraction Why is there no method for calculating how many milliseconds have ellapsed between to dates? (there are methods before, after and getTime) Date start = new Date(); // do some work Date end = new Date() I may be wrong about it !

Convenience A good interface makes all tasks possible... and common tasks simple Bad example Reading from System.in before Java 5.0: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Why doesn't System.in have a readLine method? After all, System.out has println!

Clarity and Consistency Related features should have related 1 names 2 parameters 3 return values 4 behavior Bad example In the class GregorianCalendar the constructor public GregorianCalendar(int year, int month, int dayOfMonth) expects for year and dayOfMonth values from 1 but for month from 0!

A little user interface Displaying an Image Can specify arbitrary image file JOptionPane. showMessageDialog(null, "Hello, World!", "Message", JOptionPane.INFORMATION_MESSAGE, new ImageIcon( "globe.gif” )); Icon

Use Icon interface -What if we don't want to generate an image from file? -Fortunately, can use any class that implements Icon interface type JOptionPane.showMessageDialog( null, "Hello, Mars!", "Message", JOptionPane.INFORMATION_MESSAGE, new MarsIcon(50)) ; The last argument is of type Icon

The Icon interface and create Icon objects public interface Icon{ int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g,int x,int y); }

Icons objects public class MarsIcon implements Icon{ public MarsIcon( int aSize ){size = aSize;} public int getIconWidth() {return size;} public int getIconHeight(){return size;} public void paintIcon (Component c, Graphics g, int x, int y){ Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double planet = new Ellipse2D.Double(x, y, size, size); g2.setColor(Color.RED); g2.fill(planet); } private int size; }

The Icon Interface Type and Implementing Classes showMessageDialog expects a kind of Icon object

Benefit of polymorhism - loose coupling and extensibility- public static void showMessageDialog( , Icon anIcon) There are no objects of type Icon! showMessageDialog doesn't know which icon is passed anIcon belongs to a class that implements Icon and that class defines a getIconWidth method!

The Comparable Interface Type Collections has static sort method: ArrayList a =... Collections.sort(a); Objects in list must implement the Comparable interface type public interface Comparable { int compareTo(T other); }