Object Interaction 2 Creating cooperating objects.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Fields, Constructors, Methods
Object Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
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
More sophisticated behaviour Using library classes to implement some more advanced functionality.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Grouping Objects 3 Iterators, collections and the while loop.
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 Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Grouping Objects 1 Introduction to Collections.
Programming with Objects Creating Cooperating Objects Week 6.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
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 Creating cooperating objects 5.0.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Understanding class definitions Looking inside classes.
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.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Object Oriented Design: Identifying Objects
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
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.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
COS 260 DAY 5 Tony Gauvin.
Understanding class definitions
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
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.
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.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically assess the.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Objects First with Java Creating cooperating objects
Object INTERACTION CITS1001 week 3.
Understanding class definitions
COS 260 DAY 6 Tony Gauvin.
Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
Creating cooperating objects
Objects First with Java Creating cooperating objects
Objects First with Java Creating cooperating objects
F II 4. Object Interaction Objectives
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 6 Tony Gauvin.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Object Interaction 2 Creating cooperating objects

Four components: 3 assignments (“coursework”) 1 programming test Assignment weights: Assignment 1: 20% Assignment 2: 40% Assignment 3: 40% If programming test is passed: final mark = coursework mark If programming test is failed: final mark = (coursework mark * 0.37) Module assessment

S:\proj\co320_ca13\assignment-1\ S:\proj\co320_ca13\assignment-1\ Assignment submission: drop folder

Modularising the clock display One four-digit display? Or two two-digit displays? And a bit of glue … : Reminder

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Class Diagram This means that class ClockDisplay contains a reference (either through a field, method parameter or method local variable) to an object that is a NumberDisplay.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Object Diagram ClockDisplay actually contains two references (each is a field) to a NumberDisplay. 24, 11 60, 3

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class NumberDisplay { private int limit; private int limit; private int value; private int value; public NumberDisplay (int rollOver) { limit = rollOver; value = 0; -- start at 0 } public NumberDisplay (int rollOver) { limit = rollOver; value = 0; -- start at 0 }... methods omitted... methods omitted} Objects containing Numbers

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Object Diagram ClockDisplay actually contains two references (each is a field) to a NumberDisplay. 24, 11 60, 3

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects referencing Objects public class ClockDisplay { private NumberDisplay hours; private NumberDisplay hours; private NumberDisplay minutes; private NumberDisplay minutes;... other fields... other fields public ClockDisplay() public ClockDisplay() {... initialise all the fields... initialise all the fields }... more stuff... more stuff}

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class ClockDisplay { private NumberDisplay hours; private NumberDisplay hours; private NumberDisplay minutes; private NumberDisplay minutes;... other fields... other fields public ClockDisplay() public ClockDisplay() { hours = new NumberDisplay(24); hours = new NumberDisplay(24); minutes = new NumberDisplay(60); minutes = new NumberDisplay(60);... initialise the other fields... initialise the other fields } Objects referencing Objects

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public NumberDisplay (int rollOver) {... } hours = new NumberDisplay (24); Code in class ClockDisplay: A constructor in class NumberDisplay: formal parameter supplied argument Objects referencing Objects

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

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

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

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

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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Calls objectName.methodName (argument, list) Calling a method on another object: methodName (argument, list) Calling a method on our own object: this.methodName (argument, list) … or:

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Call Examples minutes.increment(); Calling a method on another object: public void increment() {...} where: must be a public method in the class to which the minutes object belongs. Note: if increment() had been a private method, we would not have been able to invoke it!

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Call Examples updateDisplay (); Calling a method on our own object: private void updateDisplay() {...} where: must be a method in the same class as this code. Note: we can invoke both public and private methods from our own class.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Call Examples this.updateDisplay (); Calling a method on our own object: private void updateDisplay() {...} where: must be a method in the same class as this code. optionally The name of the object on which class code executes is always this (from the perspective of that class code).

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The null Object null is a special Object in Java. All Object variables (of any class ) are initially null. Variables can be tested to see if they are null. private NumberDisplay hours; public void makeHoursVisible() { if (hours == null) {... nothing to show } else {... display it } }... display it } }

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The null Object null is a special Object in Java. All Object variables (of any class ) are initially null. Variables can be tested to see if they are null. private NumberDisplay hours; public void forgetHours() { hours = null; } Variables can also be assigned to null – losing the reference to anything they were previously holding.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Concepts modularity objects referencing other objects constructors and methods internal / external method calls the null object