Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Objects Creating Cooperating Objects Week 6.

Similar presentations


Presentation on theme: "Programming with Objects Creating Cooperating Objects Week 6."— Presentation transcript:

1 Programming with Objects Creating Cooperating Objects Week 6

2 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

3 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

4 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

5 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.

6 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)

7 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

8 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

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 Tire Design:

10 Programming with Objects ABSTRACTION & MODULARISATION A digital clock

11 Programming with Objects ABSTRACTION & MODULARISATION Modularising the clock display One four-digit display? Or two two-digit displays?

12 Programming with Objects Implementation - NumberDisplay public class NumberDisplay { private int limit; private int value; // Constructor and methods // omitted. }

13 Programming with Objects Implementation - ClockDisplay public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; // Constructor and methods // omitted. }

14 Programming with Objects OBJECT DIAGRAM

15 Programming with Objects CLASS DIAGRAM

16 Programming with Objects PRIMITIVE TYPES & OBJECT TYPES 32 object type primitive type SomeClass obj; int i; (‘pointer’ to the actual object)

17 Primitive data types contain values 6 int i = 6 2.5 float x = 2.5f A char grade = ‘A’ The “=“ symbol here means “put into (assign value to) box” Programming with Objects VARIABLES OF PRIMITIVE TYPES

18 Programming with Objects PRIMITIVE TYPES 00100111 byte primitive value age byte age = 39;

19 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

20 Programming with Objects OBJECT TYPES Person object reference value mark Person mark = new Person(); mark.setName(“Mark Campbell”); mark.setJob(“Lecturer”); mark.setAge(39);

21 Programming with Objects PRIMITIVE TYPES & OBJECT TYPES 32 SomeClass a; int a; SomeClass b; 32 int b; b = a; SomeClass Object

22 BlueJ Demonstration A look at object interaction in the ClockDisplay class

23 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;

24 Programming with Objects Source Code - NumberDisplay public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public void increment() { value = (value + 1) % limit; }

25 Programming with Objects Source Code - NumberDisplay public String getDisplayValue() { if(value < 10) return "0" + value; else return "" + value; }

26 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(); }

27 Programming with Objects Source Code (Method Calling) - ClockDisplay public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); }

28 Programming with Objects Source Code (Internal Method) - ClockDisplay /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); }

29 Programming with Objects INTERNAL/EXTERNAL METHOD CALLS Internal Method Calls: updateDisplay();... private void updateDisplay() {... } External Method Calls: minutes.increment();

30 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.

31 Programming with Objects OBJECT DIAGRAM (ClockDisplay)

32 Programming with Objects OBJECTS CREATING OBJECTS hours = new NumberDisplay(24); public NumberDisplay(int rollOverLimit); ClockDisplay: NumberDisplay: formal parameter actual parameter

33 Required Reading Objects First With Java – A Practical Introduction using BlueJ Related reading for this lecture Read Chapter 3 (pp 56 – 75)


Download ppt "Programming with Objects Creating Cooperating Objects Week 6."

Similar presentations


Ads by Google