chapter 3 object interaction. to construct interesting applications it is not enough to build...

31
Chapter 3 Object Interaction

Upload: henry-gaines

Post on 18-Jan-2016

227 views

Category:

Documents


2 download

TRANSCRIPT

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

Chapter 3

Object Interaction

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

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

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

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

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

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

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

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

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

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

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

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

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

Implementing the Clock Display

public class NumberDisplay{

private int limit;private int value;…

}public class ClockDisplay{

private NumberDisplay hours;private NumberDisplay minutes;…

}

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

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.

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

Class Diagrams vs. Object Diagrams

:NumberDisplay

myDisplay: ClockDisplay

hours

minutes

:NumberDisplay

03

11

ClockDisplay

NumberDisplay

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

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

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

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

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

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

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

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

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

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, 2.63 3.1, 3.2, 3.3, 3.4

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

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

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

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.

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

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

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

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

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

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

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

Truth Table Example

X Y X && Y

T T T

T F F

F T F

F F F

X Y X || Y

T T T

T F T

F T T

F F F

X !X

T F

F T

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

String Concatenation

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

BlueJ” “answer: ” + 12 gives us “answer: 12”

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

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

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

The Modulo Operator

The modulo operator calculates the remainder from integer division

Example 12 % 5 = 2

public void increment()

{

value = ( value + 1 ) % limit;

}

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

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

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

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

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

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

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

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.

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

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

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

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.

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

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.