Object interaction Creating cooperating objects. 28/10/2004Lecture 3: Object Interaction2 Main concepts to be covered Abstraction Modularization Class.

Slides:



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

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Object interaction Creating cooperating objects 3.0.
Programming with Objects Creating Cooperating Objects Week 6.
Object Interaction 1 Creating cooperating objects.
Object interaction Creating cooperating objects 5.0.
Object interaction Creating cooperating objects 5.0.
Object Interaction 2 Creating cooperating objects.
Understanding class definitions Looking inside classes.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Programming with Objects Object Interaction (Part 1) Week 9.
Object Oriented Software Development
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming Fundamentals 2: Interaction/ F II Objectives – –introduce modularization and abstraction – –explain how an object uses other.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COS 260 DAY 5 Tony Gauvin.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Session 7 Methods Strings Constructors this Inheritance.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
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.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Unified Modeling Language (UML)
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Understanding class definitions Exploring source code 6.0.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Objects First with Java Creating cooperating objects
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
About the Presentations
Object INTERACTION CITS1001 week 3.
Java Review: Reference Types
COS 260 DAY 6 Tony Gauvin.
Chapter 6 Methods: A Deeper Look
Java Programming Language
Chapter 4 Writing Classes.
Creating cooperating objects
Creating cooperating objects
Objects First with Java Creating cooperating objects
COS 260 DAY 4 Tony Gauvin.
Objects First with Java Creating cooperating objects
Object Oriented Programming in java
F II 4. Object Interaction Objectives
COS 260 DAY 6 Tony Gauvin.
References Revisted (Ch 5)
Corresponds with Chapter 5
Presentation transcript:

Object interaction Creating cooperating objects

28/10/2004Lecture 3: Object Interaction2 Main concepts to be covered Abstraction Modularization Class and Object Diagrams Call-by-reference and Call-by-value Overloading Internal and External method calls this keyword Debugging

28/10/2004Lecture 3: Object Interaction3 A digital clock

28/10/2004Lecture 3: Object Interaction4 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.

28/10/2004Lecture 3: Object Interaction5 Modularizing the clock display One four-digit display? Or two two-digit displays?

28/10/2004Lecture 3: Object Interaction6 Implementation: NumberDisplay public class NumberDisplay { private int limit; private int value; Constructor and methods omitted. }

28/10/2004Lecture 3: Object Interaction7 Implementation ClockDisplay public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; Constructor and methods omitted. }

28/10/2004Lecture 3: Object Interaction8 Object diagram

28/10/2004Lecture 3: Object Interaction9 Class diagram

28/10/2004Lecture 3: Object Interaction10 Diagrams Class Diagrams Shows the classes of an application and the relationships between them Gives information about the source code Static view of the program Object Diagrams Shows objects and their relationships at one moment in time during the execution of the program Dynamic view of the program

28/10/2004Lecture 3: Object Interaction11 BlueJ and Diagrams

28/10/2004Lecture 3: Object Interaction12 Primitive types vs. object types Java defines two very different kinds of type: primitive types and object types. Primitive types are predefined by Java. Object types originate from classes. Variables and parameters store references to objects. The primitive types are non-object types.

28/10/2004Lecture 3: Object Interaction13 Primitive types vs. object types 32 object type primitive type SomeObject obj; int i;

28/10/2004Lecture 3: Object Interaction14 Primitive types vs. object types 32 SomeObject a; int a; SomeObject b; 32 int b; b = a;

28/10/2004Lecture 3: Object Interaction15 Call-by-reference and Call-by-value There are two ways of passing arguments to methods in many programming languages: call-by-value and call-by-reference. Call-by-value: A copy of the actual parameter is passed to the formal parameter of the called method. Any change made to the formal parameter will have no effect on the actual parameter.

28/10/2004Lecture 3: Object Interaction16 Call-by-reference and Call-by-value Call-by-reference: the caller gives the called method the ability to directly access to the caller’s data and to modify that data if the called method so chooses. Java uses call-by-value for primitive data types and call-by-reference for object types.

28/10/2004Lecture 3: Object Interaction17 Source code: NumberDisplay public class NumberDisplay { private int limit; private int value; public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; }

28/10/2004Lecture 3: Object Interaction18 Source code: NumberDisplay public int getValue() { return value; } public void setValue(int replacementValue) { if((replacementValue >= 0) && (replacementValue < limit)) value = replacementValue; }

28/10/2004Lecture 3: Object Interaction19 Logical Operators && : and, operands are tested, left to right, until conclusion can be reached || : or, operands are tested, left to right, until conclusion can be reached ! : not & : and, both operands are tested | : or, both operands are tested

28/10/2004Lecture 3: Object Interaction20 Source code: NumberDisplay public String getDisplayValue() { if(value < 10) return "0" + value; else return "" + value; } public void increment() { value = (value + 1) % limit; }

28/10/2004Lecture 3: Object Interaction21 String Concatenation Addition: String Concatenation: “Java” + “with BlueJ” -> “Javawith BlueJ” “answer: ” > “answer: 42”

28/10/2004Lecture 3: Object Interaction22 String toString() method String toString() method: Java provides a way of transforming every Object into a String. To tailor this to your own preference write a method toString() returning a String representation of your class/object. public String toString() { return “value: “ + value + “ with limit ” + limit; }

28/10/2004Lecture 3: Object Interaction23 The Modulo Operator % : the modulo operator calculates the remainder of an integer division 27 & 4 -> 3 Division in Java: if both arguments are integers, division will result in an integer. double res = 5 / 2 -> res = 2 double res = 5 / (2.0) or 5 / (2 * 1.0) -> res = 2.5

28/10/2004Lecture 3: Object Interaction24 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); updateDisplay(); }

28/10/2004Lecture 3: Object Interaction25 Objects creating objects 1.new ClassName(parameter-list) It creates a new object of the named class here NumberDisplay this involves creating sufficient memory to store the values of primitive instance variables and references to object instance variables. 2.It executes the constructor of that class public NumberDisplay new NumberDisplay (int rollOverLimit) formal parameter (24) actual parameter

28/10/2004Lecture 3: Object Interaction26 ClockDisplay object diagram

28/10/2004Lecture 3: Object Interaction27 Method Overloading Multiple Constructors of ClockDisplay: new Clockdisplay() new Clockdisplay(hour, minute) It is common for class definitions to contain alternative versions of constuctors or methods that provide various ways of achieving a particular task via their distinctive sets of parameters. This is known as overloading.

28/10/2004Lecture 3: Object Interaction28 Method calling public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); }

28/10/2004Lecture 3: Object Interaction29 Internal method /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }

28/10/2004Lecture 3: Object Interaction30 Method calls internal method calls updateDisplay(); private void updateDisplay() methodName(parameter-list) external method calls minutes.increment(); object.methodName(parameter-list)

28/10/2004Lecture 3: Object Interaction31 Public and Private Methods Public methods: public void increment() can be called externally Private methods private void updateDisplay() can only be called internally used for auxiliary methods

28/10/2004Lecture 3: Object Interaction32 The Mail System

28/10/2004Lecture 3: Object Interaction33 The this Keyword public class MailItem { private String from; private String to; private String message; public MailItem(String from, String to, String message) { this.from = from; this.to = to; this.message = message; }

28/10/2004Lecture 3: Object Interaction34 The this Keyword this.from = from name overloading: the same name is used for two different entities: instance variable and formal parameter. this is used to go out of the scope of the constructor to class level this always refers to the current object. can also used for methods for internal methods calls and access to instance fields Java automatically inserts this updateDisplay -> this.updateDisplay

28/10/2004Lecture 3: Object Interaction35 Debugging Software is hardly ever written without making mistakes or bugs. There are two type of bugs: syntax: can be traced using the compiler logical: tracing the program manually using a debugging tool BlueJ provides a debugger

28/10/2004Lecture 3: Object Interaction36 Debugging

28/10/2004Lecture 3: Object Interaction37 Concepts abstraction modularisation call-by-value call-by-reference logical operators/modulo this class/object diagram primitive types object types object creation overloading internal/external method calls private methods debugging