Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object INTERACTION CITS1001 week 3.

Similar presentations


Presentation on theme: "Object INTERACTION CITS1001 week 3."— Presentation transcript:

1 Object INTERACTION CITS1001 week 3

2 Fundamental concepts Coupling and Cohesion
Internal/external method calls null objects Chaining method calls Class constants Class variables Reading: Chapter 3 of Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

3 Concepts (1) abstraction modularization classes define types
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Concepts (1) abstraction modularization classes define types class diagram object diagram object references object types primitive types

4 Objects First with Java
A digital clock © David J. Barnes and Michael Kölling

5 Abstraction and modularization
Objects First with Java Abstraction and modularization Abstraction is the ability to ignore details of the parts of a problem, to focus attention on its higher levels Modularization is the process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways © David J. Barnes and Michael Kölling

6 Modularizing the clock display
Objects First with Java Modularizing the clock display One 4-digit display? Why? We could implement the clock in one class (as we have learnt so far) but instead we look for subcomponents in this problem we can turn into separate classes. The reason for this is complexity: trivial tasks such as the ticket machine can be solved as a single problem. But as a problem grows larger it becomes increasingly difficult to keep track of all the details at the same time. We use abstraction to divide and conquer large problems. Or two 2-digit displays? © David J. Barnes and Michael Kölling

7 Implementation - NumberDisplay
Objects First with Java Implementation - NumberDisplay public class NumberDisplay { private int limit; private int value; Constructor and methods omitted. } © David J. Barnes and Michael Kölling

8 Implementation - ClockDisplay
Objects First with Java Implementation - ClockDisplay public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; Constructor and methods omitted. } © David J. Barnes and Michael Kölling

9 Objects First with Java
Object diagram Dynamic view at runtime (when the system is running) © David J. Barnes and Michael Kölling

10 Object diagram Objects exist at run-time
The object diagram shows the objects and their relationships at one moment in time during the execution of an application It gives information about objects at runtime and presents the dynamic view of a program

11 Objects First with Java
Class diagram Static view of the system vs object diagram (dynamic view) ClockDisplay depends on NumberDisplay ClockDisplay makes use of NumberDisplay © David J. Barnes and Michael Kölling

12 Classes define types private NumberDisplay hours;
A class name can be used as the type for a variable Variables that have a class as their type can store objects belonging to that class

13 Class diagram Classes exist at compile time
The class diagram shows the classes of an application and the relationships between them It gives information about the source code and presents the static view of a program

14 Primitive types vs. object types
Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Primitive types vs. object types object type SomeObject obj; int i; primitive type 32 © David J. Barnes and Michael Kölling

15 Quiz: What is the output?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Quiz: What is the output? int a; int b; a = 32; b = a; a = a + 1; System.out.println(b); Person a; Person b; a = new Person("Everett"); b = a; a.changeName("Delmar"); System.out.println(b.getName());

16 Primitive types vs. object types
Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Primitive types vs. object types ObjectType a; ObjectType b; b = a; int a; int b; 32 32 © David J. Barnes and Michael Kölling

17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The modulo operator The 'division' operator (/), when applied to int operands, returns the result of an integer division. The 'modulo' operator (%) returns the remainder of an integer division. E.g., generally: / 5 gives result 3, remainder 2 In Java: / 5 == % 5 == 2

18 Quiz 8 % 3 What is the result of the expression
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Quiz What is the result of the expression 8 % 3 For integer n >= 0, what are all possible results of: n % 5 Can n be negative? Show BlueJ code pad to test

19 Source code: NumberDisplay
Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Source code: NumberDisplay public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public void increment() value = (value + 1) % limit; © David J. Barnes and Michael Kölling

20 Concepts (2) abstraction modularization classes define types
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Concepts (2) abstraction modularization classes define types class diagram object diagram object references object types primitive types

21 Objects creating objects
Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects creating objects public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() hours = new NumberDisplay(24); minutes = new NumberDisplay(60); } © David J. Barnes and Michael Kölling

22 Objects creating objects
Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects creating objects hours = new NumberDisplay(24); in class ClockDisplay: actual parameter public NumberDisplay(int rollOverLimit); in class NumberDisplay: formal parameter © David J. Barnes and Michael Kölling

23 ClockDisplay object diagram
Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling ClockDisplay object diagram © David J. Barnes and Michael Kölling

24 Objects First with Java
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method calling public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); © David J. Barnes and Michael Kölling

25 Objects First with Java
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling External method call For calling a method on another object external method calls minutes.increment(); object . methodName ( parameter-list ) where public void increment() { … } If increment() had been a private method we would not have been able to invoke it. © David J. Barnes and Michael Kölling

26 Objects First with Java
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Internal method call For calling a method on our own object internal method calls updateDisplay(); No variable name is required. this could be used as a reference to the invoking object, but not used for method calls. © David J. Barnes and Michael Kölling

27 Internal method (helpers)
Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Internal method (helpers) /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } © David J. Barnes and Michael Kölling

28 Method calls NB: A method call on another object of the same type would be an external call. ‘Internal’ means ‘this object’. ‘External’ means ‘any other object’, regardless of its type.

29 null null is a special Object in Java
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling null null is a special Object in Java All Object variables (of any class) are initially null Variables can be tested for null private NumberDisplay hours; if(hours != null) { //... nothing to show } else { // … display the hours } Variables can be assigned to null - losing the reference to anything they were previously holding. public void forgetHours() { hours = null;

30 Anonymous objects Objects are often created and handed on elsewhere immediately: Lot furtherLot = new Lot(…); lots.add(furtherLot); We don’t really need furtherLot: lots.add(new Lot(…));

31 Chaining method calls Methods often return objects.
We often immediately call a method on the returned object. Bid bid = lot.getHighestBid(); Person bidder = bid.getBidder(); We can use the anonymous object concept and chain method calls: lot.getHighestBid().getBidder()

32 Chaining method calls Each method in the chain is called on the object returned from the previous method call in the chain. String name = lot.getHighestBid().getBidder().getName(); Returns a Bid object from the Lot Returns a Person object from the Bid Returns a String object from the Person

33 Objects First with Java
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Concept summary object creation overloading internal/external method calls debugger © David J. Barnes and Michael Kölling

34 Review (1) Abstraction Modularisation Classes define types
ignore some details to focus attention on a higher level of a problem Modularisation Divide a whole into well defined parts that can be built separately and that interact in well-defined ways Classes define types A class name can be used as the type for a variable. Variables that have a class as their type can store objects of that class.

35 Review (2) Object diagram Object references Primitive type
Shows the objects and their relationships at one moment during the execution of an application Object references Variables of object types store references to objects Primitive type The primitive types of Java are non-object types. The most common primitive types are int, boolean, char, double and long. Object creation Objects can create other objects using the new operator

36 Review (3) Overloading Internal method call External method call
A class can contain more than one constructor or more than one method with the same name. In this case, each must have a distinctive set of parameter types. Internal method call Methods can call other methods of the same class. External method call Methods can call methods of other objects using dot notation


Download ppt "Object INTERACTION CITS1001 week 3."

Similar presentations


Ads by Google