Objects First with Java Creating cooperating objects

Slides:



Advertisements
Similar presentations
Fields, Constructors, Methods
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Object interaction Creating cooperating objects 3.0.
Programming with Objects Creating Cooperating Objects Week 6.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Object Interaction 1 Creating cooperating objects.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Object interaction Creating cooperating objects 5.0.
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.
Programming with Objects Object Interaction (Part 1) Week 9.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
COS 260 DAY 5 Tony Gauvin.
Understanding class definitions
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Understanding class definitions Exploring source code 6.0.
for-each PROS CONS easy to use access to ALL items one-by-one
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
More about inheritance
Chapter 3 Assignment Statement
Object INTERACTION CITS1001 week 3.
Understanding class definitions
COS 260 DAY 6 Tony Gauvin.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to C++ Programming
Variables ICS2O.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 3 Tony Gauvin.
IFS410 Advanced Analysis and Design
Creating cooperating objects
Building Java Programs
Python Primer 1: Types and Operators
Understanding class definitions
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.
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
COS 260 DAY 6 Tony Gauvin.
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

Objects First with Java A digital clock © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and 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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © 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? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Modeling a two-digit display We call the class NumberDisplay Two integer fields: The current value The limit for the value The current value is incremented until it reaches its limit It rolls over to zero at this point © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Implementation - NumberDisplay Objects First with Java Implementation - NumberDisplay public class NumberDisplay { private int limit; private int value; public NumberDisplay(int limit) this.limit = limit; value = 0; } ... © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © 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. } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Class diagram (static view) Objects First with Java Class diagram (static view) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Object diagram (dynamic view) Objects First with Java Object diagram (dynamic view) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © 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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © 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()); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Java operators © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Tests equality …. not identity!! Java relational operators boolean result Tests equality …. not identity!! © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Java logical operators BINARY && and || or ^ exclusive or UNARY ! not © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Logic operators for boolean values Operands && || ^ T T T T F T F F T T F T F T T F F F F F Which are examples of short-circuit operators? © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Logic operators for boolean values Operands && || ^ T T T T F T F F T T F T F T T F F F F F © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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 For example, generally: 17 / 5 gives result 3, remainder 2 In Java: 17 / 5 == 3 17 % 5 == 2 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java The modulo operation as an expression Dividend / Divisor = Quotient RRemainder 21 / 5 = 4 R1 Thus, the modulo operation(%) is expressed as: a % b == a – ((a / b) * b) 21 % 5 == 21 – ((21 / 5) * 5) == 21 – ((4) * 5) == 21 – 20 == 1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and 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? ………… YES!! What are all the possible results of: -n % 5 Is this possible? n % 0 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Quiz What is the result of the expression 8 % 3 == 2 For integer n >= 0, what are all possible results of: n % 5 0, 1, 2, 3, 4 Can n be negative? ………… YES!! What are all the possible results of: -n % 5 -4, -3, -2, -1, -0 Is this possible? ………… NO!! n % 0 since n/0 is undefined © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

increment method public void increment() { value = value + 1; if(value == limit) { // Keep the value within the limit. value = 0; } } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Alternative increment method public void increment() { value = (value + 1) % limit; } Check that you understand how the rollover works in this version. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Source code: NumberDisplay public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public void increment() value = (value + 1) % limit; * value is between 0 --> (limit - 1) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Source code: NumberDisplay Objects First with Java Source code: NumberDisplay public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Source code: setValue() public void setValue(int replacementValue) { if((replacementValue >= 0) && (replacementValue < limit)) value = replacementValue; } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Classes as types Data can be classified under many different types; e.g. integer, boolean, floating-point. In addition, every class is a unique data type; e.g. String, TicketMachine, NumberDisplay. Data types, therefore, can be composites and not simply values. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Concepts abstraction modularization classes define types class diagram object diagram object references object types primitive types © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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); … } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © 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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

ClockDisplay object diagram Objects First with Java ClockDisplay object diagram © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Object interaction Two objects interact when one object calls a method on another The interaction is usually all in one direction (client, server) The client object can ask the server object to do something The client object can ask for data from the server object © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Object interaction Two NumberDisplay objects store data on behalf of a ClockDisplay object The ClockDisplay is the client object The NumberDisplay objects are the server objects The client calls methods in the server objects © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Method calling ‘client’ method public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); ‘server’ methods internal/self method call © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java External method calls General form of external method call: object . methodName ( params ) Examples: hours.increment() minutes.getValue() © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java Internal method calls No variable name is required for internal method calls: updateDisplay(); Internal methods often have private visibility to prevent them from being called from outside their defining class Method is found in this same invoking class/object where the call is made © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © 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(); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Method calls Internal means this object External means any other object, regardless of its type NOTE: A method call on another object of the same type would also be an external call © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The this keyword Used to distinguish parameters and fields of the same name this could also be used as a reference to the invoking object instead of method calls public ClockDisplay(int limit) { this.limit = limit; value = 0; } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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; © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

null vs. void null Means undefined or no memory address is being pointed to Used in code to represent no object reference exists void Means empty or no data type Used in place of the return type for a method when no value is being returned © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The debugger Useful for gaining insights into program behavior … … whether or not there is a program error Set breakpoints Examine variables Step through code © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The debugger © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Errors Syntax Logic Runtime * Errors in the code text itself * Found when compiling with unrecognizable text * Fix by editing code Logic * Errors in the behavior of the program * Found when running with unexpected results * Fix by debugging and observing states Runtime * Errors which prohibit program from completing * Found when executing the program * Fix by tracing, debugging, observing and editing © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Objects First with Java Concept summary object creation overloading internal/external method calls debugger © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling