COS 260 DAY 5 Tony Gauvin.

Slides:



Advertisements
Similar presentations
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Advertisements

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.
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 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.
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.
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.
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
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.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Classes CS 21a: Introduction to Computing I First Semester,
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.
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 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.
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.
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.
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.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Objects First with Java Creating cooperating objects
Object INTERACTION CITS1001 week 3.
COS 260 DAY 17 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
Objects First with Java
Understanding class definitions
COS 260 DAY 6 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 3 Tony Gauvin.
Creating cooperating objects
COS 260 DAY 16 Tony Gauvin.
Understanding class definitions
Collections and iterators
COS 260 DAY 23 Tony Gauvin.
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
COS 260 DAY 23 Tony Gauvin.
Classes CS 21a: Introduction to Computing I
COS 260 DAY 6 Tony Gauvin.
Collections and iterators
Presentation transcript:

COS 260 DAY 5 Tony Gauvin

Agenda Questions? 2nd Mini quiz Assignment 1 Due Next Class Password “squareRootChess” 6 questions and 1 extra credit Assignment 1 Due Next Class Assignment 2 will be posted soon Begin Object Interaction

Objects First with Java Creating cooperating objects Object interaction Creating cooperating objects 5.0 © David J. Barnes and Michael Kölling

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

Source: http://www. autoevolution Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

How to design a car? Divide and Conquer Every engineering team has their part(s) Wheels & tires Engines & transmissions Interiors Electronics Body and Paint Have to agree on standard interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Abstraction and modularization Objects First with Java 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 © David J. Barnes and Michael Kölling

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

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

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

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

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

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

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

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

Number Display behaviors 00 (or some other value)  limit  00 Possible methods increment setValue hitLimit Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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 what is largest passible remainder Some times called “clock math” Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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

Concepts abstraction modularization classes define new 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

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

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

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

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

Objects First with Java 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 © David J. Barnes and Michael Kölling

Objects First with Java 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 © David J. Barnes and Michael Kölling

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

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.

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

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

The debugger

Objects First with Java 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 © David J. Barnes and Michael Kölling