Programming with Objects Object Interaction (Part 1) Week 9
Abstraction & modularisation The modulo operator Programming with Objects CONCEPTS COVERED THIS WEEK
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 Objects ABSTRACTION & MODULARISATION
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
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:
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?
BlueJ Demonstration A look at the NumberDisplay class
Programming with Objects Source Code - NumberDisplay public class NumberDisplay { private int limit; private int value; // Constructor and methods // omitted. }
Programming with Objects Source Code - NumberDisplay public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public int getValue() { return value; }
Programming with Objects Source Code - NumberDisplay public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; }
Programming with Objects Source Code - NumberDisplay public void setValue(int replacementValue) { if((replacementValue >= 0) && (replacementValue < limit)) { value = replacementValue; }
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;
Programming with Objects Source Code - NumberDisplay public void increment() { value = (value + 1) % limit; }
Required Reading Objects First With Java – A Practical Introduction using BlueJ Related reading for this lecture Chapter 3