Object interaction Creating cooperating objects 5.0.

Slides:



Advertisements
Similar presentations
Understanding class definitions Looking inside classes 5.0.
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Fields, Constructors, Methods
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Improving structure with inheritance
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection.
Objects First with Java A Practical Introduction using BlueJ
String Concatenation (operator overloading) 3.0.
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Object interaction Creating cooperating objects 3.0.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Programming with Objects Creating Cooperating Objects Week 6.
Object Interaction 1 Creating cooperating objects.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Object interaction Creating cooperating objects 5.0.
Object Interaction 2 Creating cooperating objects.
Object interaction Creating cooperating objects. 28/10/2004Lecture 3: Object Interaction2 Main concepts to be covered Abstraction Modularization Class.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Programming with Objects Object Interaction (Part 1) Week 9.
Object Oriented Design: Identifying Objects
Programming Fundamentals 2: Interaction/ F II Objectives – –introduce modularization and abstraction – –explain how an object uses other.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
Classes CS 21a: Introduction to Computing I First Semester,
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
COS 260 DAY 5 Tony Gauvin.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
OOPDA Intro 5.0. Topics Website and Syllabus Rowan VPN and H:drive BlueJ application and projects Programming Style (Appendix J) Javadoc (Appendix I)
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 COS 260 DAY 15 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz Today –Chapter 6 Assignment 4 posted –Due Nov 9 Capstone progress reports are due –Brief.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Objects First with Java Creating cooperating objects
Object INTERACTION CITS1001 week 3.
COS 260 DAY 17 Tony Gauvin.
Objects First with Java
Understanding class definitions
COS 260 DAY 6 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 3 Tony Gauvin.
Creating cooperating objects
Understanding class definitions
Collections and iterators
Creating cooperating objects
Objects First with Java Creating cooperating objects
COS 260 DAY 4 Tony Gauvin.
Objects First with Java Creating cooperating objects
F II 4. Object Interaction Objectives
Classes CS 21a: Introduction to Computing I
COS 260 DAY 6 Tony Gauvin.
Collections and iterators
Presentation transcript:

Object interaction Creating cooperating objects 5.0

2 A digital clock Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

3 Abstraction and modularization Abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem. Modularization is the process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

4 Modularizing the clock display Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling One four-digit display? Or two two-digit displays?

5 Implementation - NumberDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class NumberDisplay { private int limit; private int value; Constructor and methods omitted. }

6 Implementation - ClockDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; Constructor and methods omitted. }

7 Object diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 Class diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 Primitive types vs. object types Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 32 object type primitive type SomeObject obj; int i;

10 Quiz: What is the output? int a; int b; a = 32; b = a; a = a + 1; System.out.println(b); Person a; Person b; a = new Person("Everett"); b = a; a.changeName("Delmar"); System.out.println(b.getName()); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 Primitive types vs. object types Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 32 ObjectType a; int a; ObjectType b; 32 int b; b = a;

12 Source code: NumberDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public void increment() { value = (value + 1) % limit; }

13 The modulo operator The 'division' operator (/), when applied to int operands, returns the result of an integer division. The 'modulo' operator (%) returns the remainder of an integer division. E.g., generally: 17 / 5 gives result 3, remainder 2 In Java: 17 / 5 == 3 17 % 5 == 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 Quiz What is the result of the expression 8 % 3 For integer n >= 0, what are all possible results of: n % 5 Can n be negative? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15 Source code: NumberDisplay Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; }

16 Concepts abstraction modularization classes define types class diagram object diagram object references object types primitive types Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 Objects creating objects Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); … }

18 Objects creating objects Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public NumberDisplay(int rollOverLimit); in class NumberDisplay: formal parameter hours = new NumberDisplay(24); in class ClockDisplay: actual parameter

19 ClockDisplay object diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

20 Method calling Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); }

21 External method call external method calls minutes.increment(); object. methodName ( parameter-list ) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

22 Internal method call internal method calls updateDisplay(); No variable name is required. this –could be used as a reference to the invoking object, but not used for method calls. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

23 Internal method Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }

24 Method calls NB: A method call on another object of the same type would be an external call. ‘Internal’ means ‘this object’. ‘External’ means ‘any other object’, regardless of its type.

25 null null is a special value in Java Object fields are initialized to null by default. You can test for and assign null : private NumberDisplay hours; if(hours != null) {... } hours = null; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

26 The debugger Useful for gaining insights into program behavior … … whether or not there is a program error. Set breakpoints. Examine variables. Step through code.

27 The debugger

28 Concept summary object creation overloading internal/external method calls debugger Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling