Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
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.
C++ for Engineers and Scientists Third Edition
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.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Programming with Objects Object Interaction (Part 1) Week 9.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Object Oriented Design: Identifying Objects
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Programming Fundamentals 2: Interaction/ F II Objectives – –introduce modularization and abstraction – –explain how an object uses other.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSE 131 Computer Science 1 Module 1: (basics of Java)
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
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.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
COS 260 DAY 5 Tony Gauvin.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
(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,
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Objects First with Java Creating cooperating objects
University of Central Florida COP 3330 Object Oriented Programming
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
Object INTERACTION CITS1001 week 3.
Chapter 6 Methods: A Deeper Look
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
Creating cooperating objects
Creating cooperating objects
Objects First with Java Creating cooperating objects
Chapter 2 Programming Basics.
Objects First with Java Creating cooperating objects
Classes, Objects and Methods
Presentation transcript:

Chapter 3 Object Interaction

 To construct interesting applications it is not enough to build individual objects  Objects must be combined so they cooperate to perform a common goal  We will see an example of this using three objects  The clock example The clock example

The Clock Example  We will build an application to display a simple digital clock  The clock will display 24 hour time  The digits will be separated with a colon

Abstraction and Modularization  A first idea would be to implement the clock as a single class  Instead, we will see if we can identify the key subcomponents and break the problem down to those pieces  We will push down some of the complexity from the project into lower class  This solution to complexity is called abstraction

Abstraction and Modularization  We divide the problem into sub- problems and so on  Until we get to a sub-problem that is simple enough to solve with a single class  Then we treat the solved sub- problem as a building block for our bigger problem  Divide and conquer

Abstraction and Modularization  Divide the problem into separate modules  Once the module is done, use abstraction to ignore the complexity of the module and use it to solve a bigger problem  In OOP, the modules are objects

Clock Modularization  It can be viewed as a display with 4 digits  It can also be viewed as two separate digit displays  Or it can be viewed as an object that can display digits from 0 to a given limit  The value can be incremented and when the limit is reached, the display rolls back to 0

Implementing the Clock Display public class NumberDisplay { private int limit; private int value; … } public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; … }

Classes as Data Types  In the last example of the ClockDisplay class, we see that classes can define data types  The type of the field specifies what kind of values can be stored in the field  If the field is a class, then the field can store objects of that class.

Class Diagrams vs. Object Diagrams :NumberDisplay myDisplay: ClockDisplay hours minutes :NumberDisplay ClockDisplay NumberDisplay

Class Diagram  The class diagram shows a static view  It depicts the view at the time of writing the program We have two classes. The arrow indicates that the one class makes use of the other class We also say that class A depends on class B

Object Diagram  The object view shows the program at runtime  This is also called a dynamic view  The object diagram shows an important feature about how a variable stores an object  The object is not stored directly, rather an object reference is stored

Object Diagrams  BlueJ shows the static view of a project  In order to plan and understand Java programs you will need to be able to construct object diagrams  When we think about what programs do, we will think about the object structures it creates and how those objects interact

Primitive Types and Object Types  Java knows of two very different kind of types Primitive Types Object Types  Primitive types are built-in to Java A complete list is in Appendix B  Object types are defined by classes  Some come standard with Java

Homework  Due next week at the beginning of lab 2.47, 2.49, 2.50, 2.51, 2.52, 2.54, 2.55, 2.56, 2.59, 2.61, , 3.2, 3.3, 3.4

Primitive Types and Object Types  Both kind of types can be used to store values  There are situations where they behave differently  Primitive types are stored directly in a variables  Object types are stored as object references in variables

Operators  Operators in Java come in many different types and uses  We will look at logical, mathematical and string operators to name a few  The ones we see are not all of the operators Java has.

ClockDisplay Source public void setValue(int replacementValue) { if ( replacementValue >= 0) && (replacementValue < limit) value = replacementValue; }  See Appendix D for a complete list of other logical operators

Logical Operators  Three main operators && (and) || (or) ! (not)  The expression a && b is True if a and b are true and false otherwise  The expression a || b is True if either a or b or both are true, and false if they are both false.  The expression !a is True is a is false, and false if a is true

Truth Tables  You can use truth tables to determine the validity of any Boolean expression  You make a table with the operands across the top  The body of the table is filled with the values the operands can take  And the results are determined from the values

Truth Table Example XYX && Y TTT TFF FTF FFF XYX || Y TTT TFT FTT FFF X!X TF FT

String Concatenation  The plus operator (+) has different meanings depending on the type of its operands does what we expect “Java” + “with BlueJ” gives us “Javawith BlueJ” “answer: ” + 12 gives us “answer: 12”

String Concatenation Trick if ( value < 10 ) return “0” + value; else return “” + value;  This is from the get value method, which returns a string  That’s why they concatenate the empty string with the integer value

The Modulo Operator  The modulo operator calculates the remainder from integer division  Example 12 % 5 = 2 public void increment() { value = ( value + 1 ) % limit; }

Objects Creating Objects  Sometimes objects need to create other objects  Here’s how they do that new Classname( parameter list );  The new operator does 2 things It creates a new object of the named class. It executes the constructor of that class  If a constructor takes parameters, you must supply when you call new

Multiple Constructors  You might have noticed that the ClockDisplay class has two ways to create an object  It has two constructors  It is common for classes to offer multiple versions of a method that differs only by its parameters.  This is called overloading

Method Calls  They come in two flavors Internal Method calls External Method calls  Internal method calls are calls within the same class  External method calls are calls to methods of other classes

Internal Method Calls  You call an internal method like this methodName( parameter list );  When a method call is encountered, the matching method is executed  Once the matching method is done, execution returns to the line after the method call  To match, both the method name and parameter list must match.

External Method Calls  In order to make an external method call, an object must have a reference to another object, either through a field, parameter or variable  Then it can do the following to make an external method call objectName. methodName( paramter list)  The execution here works just like an internal method call

The this keyword  Many times in Java we need a way to refer to the object currently executing a method.  The this keyword gives us a reference to that object.  An example of when this might be useful is when you are trying to copy the contents from this object to another object.

Another example of Object Interaction  We will use the debugger tool in BlueJ to look at another example of object interaction  A debugger is a piece of software that will allow you to examine code as it runs.  We will look at the Mail System example.Mail System