programming with objects object interaction (part 1) week 9

15
Programming with Objects Object Interaction (Part 1) Week 9

Upload: patrick-francis

Post on 25-Dec-2015

219 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects

Object Interaction (Part 1)

Week 9

Page 2: Programming with Objects Object Interaction (Part 1) Week 9

• Abstraction & modularisation

• The modulo operator

Programming with Objects CONCEPTS COVERED THIS WEEK

Page 3: Programming with Objects Object Interaction (Part 1) Week 9

• 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

Page 4: Programming with Objects Object Interaction (Part 1) Week 9

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

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

This car is a Complex Object

And… a wheel itself is also a complex object!

Tyre, Trim, Hub Cap, etc.

ABSTRACTION

Object Model

Page 5: Programming with Objects Object Interaction (Part 1) Week 9

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

Tyre Design:

Page 6: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects ABSTRACTION & MODULARISATION

A digital clock

Page 7: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects ABSTRACTION & MODULARISATION

Modularising the clock display

One four-digit display?

Or two two-digit displays?

Page 8: Programming with Objects Object Interaction (Part 1) Week 9

BlueJ DemonstrationA look at the NumberDisplay class

Page 9: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects Source Code - NumberDisplay

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

// Constructor and methods // omitted.}

Page 10: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects Source Code - NumberDisplay

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

public int getValue(){ return value;}

Page 11: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects Source Code - NumberDisplay

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

Page 12: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects Source Code - NumberDisplay

public void setValue(int replacementValue){ if((replacementValue >= 0) && (replacementValue < limit)) { value = replacementValue; }}

Page 13: Programming with Objects Object Interaction (Part 1) Week 9

Modulo (or modulus or mod) 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;

Page 14: Programming with Objects Object Interaction (Part 1) Week 9

Programming with Objects Source Code - NumberDisplay

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

Page 15: Programming with Objects Object Interaction (Part 1) Week 9

Required ReadingObjects First With Java – A Practical Introduction using

BlueJ

Related reading for this lecture• Chapter 3