330 likes | 497 Views
Programming with Objects Creating Cooperating Objects Week 6. Programming with Objects. CONCEPTS COVERED THIS WEEK. Abstraction & Modularisation Object Diagrams & Class Diagrams Primitive Types & Object Types Objects creating other Objects Calling methods on Objects . What is OO?.
E N D
Programming with Objects Creating Cooperating Objects Week 6
Programming with Objects CONCEPTS COVERED THIS WEEK • Abstraction & Modularisation • Object Diagrams & Class Diagrams • Primitive Types & Object Types • Objects creating other Objects • Calling methods on Objects
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)
Programming with Objects ABSTRACTION & MODULARISATION • 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.
Object Model ABSTRACTION & MODULARISATION This car is a Complex Object It is an object that is made up of other objects! Wheels, Seats, Chassis, Exhaust, Steering Wheel, etc, etc... And… a wheel itself is also a complex object! Tire, Trim, Hub Cap, etc, etc...
Object Model MODULARISATION Team 3a works on producing the Tire Design: Team 1 works on producing the Engine Design: Team 2 works on producing the Seat Design: Team 3 works on producing the Wheel Design:
Programming with Objects A digital clock ABSTRACTION & MODULARISATION
Programming with Objects Modularising the clock display ABSTRACTION & MODULARISATION One four-digit display? Or two two-digit displays?
Programming with Objects Implementation - NumberDisplay • public class NumberDisplay • { • private int limit; • private int value; • //Constructor andmethods • //omitted. • }
Programming with Objects Implementation - ClockDisplay • public class ClockDisplay • { • private NumberDisplayhours; • private NumberDisplayminutes; • //Constructor andmethods • //omitted. • }
Programming with Objects OBJECT DIAGRAM
Programming with Objects CLASS DIAGRAM
Programming with Objects PRIMITIVE TYPES & OBJECT TYPES SomeClass obj; object type (‘pointer’ to the actual object) int i; 32 primitive type
Programming with Objects VARIABLES OF PRIMITIVE TYPES 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 age 00100111 primitive value byte age = 39; byte PRIMITIVE TYPES
Programming with Objects VARIABLES OF OBJECT TYPES Object types contain references to objects String name = “Mark”; Person mark = new Person( ); name mark “Mark” Person object The “=“ symbol here means “point to the box”
Programming with Objects OBJECT TYPES Person mark = new Person(); Person object reference value mark mark.setName(“Mark Campbell”); mark.setJob(“Lecturer”); mark.setAge(39); Person
Programming with Objects PRIMITIVE TYPES & OBJECT TYPES SomeClass b; SomeClass a; SomeClass Object b = a; int a; int b; 32 32
A look at object interaction in the ClockDisplay class BlueJ Demonstration
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) EXAMPLES: minutes.increment(); hours.getDisplayValue(); minutes.getDisplayValue(); Object identifier No object identifier is required for internal method calls. updateDisplay();
Programming with Objects OBJECT DIAGRAM (ClockDisplay)
Programming with Objects OBJECTS CREATING OBJECTS NumberDisplay: public NumberDisplay(int rollOverLimit); formal parameter ClockDisplay: hours = new NumberDisplay(24); actual parameter
Required ReadingObjects First With Java – A Practical Introduction using BlueJ Related reading for this lecture • Read Chapter 3 (pp 56 – 75)