Chapter 3: Notes on Basic Object Interaction

Download Report

Transcript Chapter 3: Notes on Basic Object Interaction

Chapter 3
Object Interaction
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



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 subproblems 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 subproblem 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
myDisplay:
ClockDisplay
ClockDisplay
hours
minutes
:NumberDisplay
11
NumberDisplay
:NumberDisplay
03
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 are built-in to Java



Primitive Types
Object Types
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, 2.63
3.1, 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




The expression a && b is


True if a and b are true and false otherwise
The expression a || b is


&& (and)
|| (or)
! (not)
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
X
T
T
F
F
Y
T
F
T
F
X && Y
T
F
F
F
X
T
T
F
F
X
!X
T
F
F
T
Y
T
F
T
F
X || Y
T
T
T
F
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”
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


The new operator does 2 things



new Classname( parameter list );
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.