1 / 33

Programming with Objects Creating Cooperating Objects Week 6

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

chance
Télécharger la présentation

Programming with Objects Creating Cooperating Objects Week 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Objects Creating Cooperating Objects Week 6

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

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

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

  9. 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:

  10. Programming with Objects A digital clock ABSTRACTION & MODULARISATION

  11. Programming with Objects Modularising the clock display ABSTRACTION & MODULARISATION 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 andmethods • //omitted. • }

  13. Programming with Objects Implementation - ClockDisplay • public class ClockDisplay • { • private NumberDisplayhours; • private NumberDisplayminutes; • //Constructor andmethods • //omitted. • }

  14. Programming with Objects OBJECT DIAGRAM

  15. Programming with Objects CLASS DIAGRAM

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

  17. 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”

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

  19. 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”

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

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

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

  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) EXAMPLES: minutes.increment(); hours.getDisplayValue(); minutes.getDisplayValue(); Object identifier No object identifier is required for internal method calls. updateDisplay();

  31. Programming with Objects OBJECT DIAGRAM (ClockDisplay)

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

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

More Related