programming with objects creating cooperating objects week 6

33
Programming with Objects Creating Cooperating Objects Week 6

Post on 21-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Programming with Objects

Creating Cooperating Objects

Week 6

• Abstraction & Modularisation

• Object Diagrams & Class Diagrams

• Primitive Types & Object Types

• Objects creating other Objects

• Calling methods on Objects

Programming with Objects CONCEPTS COVERED THIS WEEK

What is OO?

• Mirrors the real world

• Classes:– ‘Types’ or blueprints– ‘Encapsulating’ data and actions (methods)

• encapsulation

– Some parts hidden from the outside world• data hiding

Objects

• Objects:– instances (or variables) of a class– once ‘instantiated’ (created) exist until they

are destroyed– communicate with each other and the

outside world by messages

Classes

• A class is a blueprint or template.

• We can then create as many objects of the class as we need

• Think of each of these objects as having their own set of data and methods.

Class / Object Relationships

• ASSOCIATION -one object uses another object. Key word: uses.– (a car uses a car park.)

•  AGGREGATION/COMPOSITION -one object may be made up of other objects. Key word: has a.– (e.g. CAR has a STEERING WHEEL, ENGINE..)

•  INHERITANCE -an object inherits the properties of another. Key word: is a– (HATCHBACK is a type of CAR, its superclass)

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

• Modularisation is the process of dividing a whole into a set of well-defined parts, where each part can be built and tested separately, and where each part only interacts with other parts in well-defined ways.

Programming with ObjectsABSTRACTION & MODULARISATION

It is an object that is made up of other objects!

Wheels, Seats, Chassis, Exhaust, Steering Wheel, etc, etc...

This car is a Complex Object

And… a wheel itself is also a complex object!

Tire, Trim, Hub Cap, etc, etc...

ABSTRACTION & MODULARISATION

Object Model

MODULARISATION

Object Model

Team 1 works on producing the Engine Design:

Team 2 works on producing the Seat Design:

Team 3 works on producing the Wheel Design:

Team 3a works on producing the

Tire Design:

Programming with Objects ABSTRACTION & MODULARISATION

A digital clock

Programming with Objects ABSTRACTION & MODULARISATION

Modularising the clock display

One four-digit display?

Or two two-digit displays?

Programming with Objects Implementation - NumberDisplay

public class NumberDisplay{ private int limit; private int value;

// Constructor and methods // omitted.}

Programming with Objects Implementation - ClockDisplay

public class ClockDisplay{ private NumberDisplay hours; private NumberDisplay minutes;

// Constructor and methods // omitted.}

Programming with Objects OBJECT DIAGRAM

Programming with Objects CLASS DIAGRAM

Programming with Objects PRIMITIVE TYPES & OBJECT TYPES

32

object type

primitive type

SomeClass obj;

int i;

(‘pointer’ to the actual object)

Primitive data types contain values

6int i = 6

2.5float x = 2.5f

Achar grade = ‘A’

The “=“ symbol here means “put into (assign value to) box”

Programming with Objects VARIABLES OF PRIMITIVE TYPES

Programming with Objects PRIMITIVE TYPES

00100111

byte

primitive valueage

byte age = 39;

Object types contain references to objects

“Mark”

String name = “Mark”;

name

Person object

Person mark = new Person( );

mark

The “=“ symbol here means “point to the box”

Programming with Objects VARIABLES OF OBJECT TYPES

Programming with Objects OBJECT TYPES

Person

Person object

reference value

mark

Person mark = new Person();

mark.setName(“Mark Campbell”);mark.setJob(“Lecturer”);mark.setAge(39);

Programming with Objects PRIMITIVE TYPES & OBJECT TYPES

32

SomeClass a;

int a;

SomeClass b;

32

int b;

b = a;

SomeClassObject

BlueJ DemonstrationA look at object interaction in the ClockDisplay

class

Modulo (or modulus) operator

• The % operator gives the remainder of a division

• result = 10 % 3;

• assigns a value of 1 to result

• result = 15 % 6;

• result = 19 % 11;

Programming with Objects Source Code - NumberDisplay

public NumberDisplay(int rollOverLimit){ limit = rollOverLimit; value = 0;}

public void increment(){ value = (value + 1) % limit;}

Programming with Objects Source Code - NumberDisplay

public String getDisplayValue(){ if(value < 10) return "0" + value; else return "" + value;}

Programming with Objects Source Code (Object Creation) - ClockDisplay

public class ClockDisplay{ private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); updateDisplay(); }}

Programming with Objects Source Code (Method Calling) - ClockDisplay

public void timeTick(){ minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay();}

Programming with Objects Source Code (Internal Method) - ClockDisplay

/** * Update the internal string that * represents the display. */private void updateDisplay(){ displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue();}

Programming with Objects INTERNAL/EXTERNAL METHOD CALLS

Internal Method Calls:

updateDisplay(); ...

private void updateDisplay(){ ... }

External Method Calls:

minutes.increment();

Programming with Objects INTERNAL/EXTERNAL METHOD CALLS

object.methodName(parameter-list)

minutes.increment();

EXAMPLES:

hours.getDisplayValue();

minutes.getDisplayValue();

updateDisplay();

Object identifier

No object identifier is required for internal method calls.

Programming with Objects OBJECT DIAGRAM (ClockDisplay)

Programming with Objects OBJECTS CREATING OBJECTS

hours = new NumberDisplay(24);

public NumberDisplay(int rollOverLimit);

ClockDisplay:

NumberDisplay:

formal parameter

actual parameter

Required ReadingObjects First With Java – A Practical Introduction using

BlueJ

Related reading for this lecture• Read Chapter 3 (pp 56 – 75)