Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Java - Objects interaction
Description: Java - Understanding class definitions. Notes taken from the 1st year class, programming foundations class lecture, based on lectures from Objects first Java -A practical introduction using BlueJ.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Chapter 3
Object Interaction
Objects cooperating with other objects –
objects calling other objects methods
...

BlueJ Book: Sections 3
...
6
1

Abstraction and Modularisation
• Abstraction is the ability to ignore details of parts
to focus attention on a higher level of a problem
...

• Object-oriented design and development
provides strong support for both Abstraction
and Modularisation
...
Barnes, Michael Kölling

2

A Digital Clock

Range 00:00  23:59
Going to examine the design and implementation of this
...
Barnes, Michael Kölling

Modularising the Clock
Display
One four-digit display?

Or two two-digit
displays?

4
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Barnes, Michael Kölling

5

Implementation - ClockDisplay
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
Constructor and
methods omitted
...
g
...

• Classes define Types
...
Barnes, Michael Kölling

Class NumberDisplay Methods
public NumberDisplay(int rollOverLimit)
public int getValue()
public String getDisplayValue()
public void setValue(int replacementValue)
public void increment()
This is an abstract view of NumberDisplay
...
Barnes, Michael Kölling

7

Object Diagram

Object Diagram shows
objects and their
relationships at one
moment in time during
the execution of a
program (known as a run
time or dynamic view)
Objects First with Java - A Practical Introduction using BlueJ, © David J
...

Objects First with Java - A Practical Introduction using BlueJ, © David J
...

}
value is the current value of number to display
limit is the „limit‟ at which it „rolls over‟ to zero
Objects First with Java - A Practical Introduction using BlueJ, © David J
...

The 'modulo' operator (%) returns the remainder of an
integer division
...
g
...
Barnes, Michael Kölling

11

Source Code: NumberDisplay
public voidsetValue(int replacementValue)
{
if((replacementValue >= 0) &&
(replacementValue < limit))
value = replacementValue;
}
&& is the boolean “and” operator
a && b evaluates to true if both a is true AND b is true
|| is “or”: a || b evaluates to true if either a OR b is true
! is “not”: !a evaluates to true if a is false
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
)
!
* / %
Brackets ‘( …)’ have the
+ strongest precedence
...
Barnes, Michael Kölling

13

Source Code: NumberDisplay
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
Reminder: „+‟ concatenates strings when at least
one of the two operands is a string
What does this method do?
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Only require brackets if more than one statement
...
Barnes, Michael Kölling

15

Key points so far …







Abstraction
Modularisation
Classes defines types
Class Diagram
Object Diagram
Short Circuit Operators: ! * / % + - && ||

Objects First with Java - A Practical Introduction using BlueJ, © David J
...
7 – 3
...
12 – 3
...
Barnes, Michael Kölling

17

Primitive Types vs
...
g
...
3

See Appendix B
P469 (4th ed
...
Barnes, Michael Kölling

18

Primitive Types vs
...

(A „reference‟ is the memory address of the object
...

This is the case before the constructer is called
obj null
e
...
after just: SomeObjectType obj;
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Object Types
int
int
a =
b =

a;
b;
32;
a;

a

32

b

32

ObjType a;
a
ObjType b;
a = new ObjType();
b
b = a;
With shared object references changing one, a or b
above, changes both – see the Quiz on the next slide
...
Barnes, Michael Kölling

20

Quiz: What is the output?
int a;
int b;
a = 32;
b = a;
a = a + 1;
System
...
println(b);
Person a;
Person b;
a = new Person(“Pascali”);
b = a;
a
...
out
...
getName());
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Barnes, Michael Kölling

ClockDisplay Object Diagram

Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Barnes, Michael Kölling

24

Multiple Constructors
There are two alternative ways for creating and initialising a
ClockDisplay object:
new ClockDisplay()
new ClockDisplay(15, 23)
The ClockDisplay class contains two constructors - one with
no parameters, the other with two
...

ClockDisplay()
ClockDisplay(int hour, int minute)
Overloading: Providing more than one constructor or method of the same
name is permissible provided each has a distinctive list of parameter types
(signature)
...
Barnes, Michael Kölling

25

Method Calls
When a method call is encountered:
e
...
minutes
...
g
...
Barnes, Michael Kölling

26

Method Calls
• External method calls - methods can call methods of
other objects using the “dot” notation:
objectName
...
g
...
increment();
• The method is sought in class of object referenced by
minutes i
...
NumberDisplay
• increment() must be public (or protected) in
class NumberDisplay

Objects First with Java - A Practical Introduction using BlueJ, © David J
...
g
...
updateDisplay();
Can make internal method calls to public or private
(or protected) methods
...
Barnes, Michael Kölling

28

Method Calls
In class ClockDisplay:
Which class?
public void timeTick()
{
minutes
...
getValue() == 0) {
// it just rolled over!
hours
...
Barnes, Michael Kölling

29

Internal Methods
Notice „private‟

/**
* Update the internal string that
* represents the display
...
getDisplayValue() + ":" +
minutes
...
8
...
6)
Objects First with Java - A Practical Introduction using BlueJ, © David J
...

• You can assign and test for null:
private NumberDisplay hours;
if(hours == null) {
...
Barnes, Michael Kölling

31

this
• Special keyword in Java
...
updateDisplay();
• Can be used to refer to field of current object, thus
public getAge(int age)
{
this
...
Barnes, Michael Kölling

32

Key Points…
• object references
• primitive types
• classes define types e
...
ClockDisplay, NumberDisplay
• object creation using new and constructors
• overloading – more than one constructor
• internal / external method call
Most important overall concept in Chapter 3 is the way that
objects cooperate
...

We‟ll be using it in the Practical
...
1 – 4
...
Barnes, Michael Kölling

33


Title: Java - Objects interaction
Description: Java - Understanding class definitions. Notes taken from the 1st year class, programming foundations class lecture, based on lectures from Objects first Java -A practical introduction using BlueJ.