Top Banner
Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self- referential class declarations
21

Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 1

Reasoning with Objects

Object references and self-referential class declarations

Page 2: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 2

Data Types

A data type may be described in terms of a set of values and a set of operations involving those values. e.g. For integer, the values are {0, 1, -1, 2, -2, 3, -3, …} and the operations are plus, minus, times, divide,…

In Java we have two distinct kinds of data type: primitive types and object types. They differ in the way which they are defined, the form of operations, the form of data values.

Page 3: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 3

Two Kinds Of Data Type

Primitive Types e.g. int, double, boolean.

1. Predefined part of the core language.

2. Considered fundamental concepts (building blocks).

Object Types e.g. String, Book, Student, Circle,…

1. User (i.e. programmer) defined through class declaration.

2. Class declarations are the means by which new concepts are added to the vocabulary.

Page 4: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 4

Two Kinds of Operation

Primitive Types• Operations on primitive data values are predefined. e.g.

+, -, *, / for int. • Primitive types don’t have methods.

Object Types• Operations on objects are user defined through method

declaration. • A method declaration describes how to represent a

certain operation on the type being defined.• A method call performs the desired operation on the

target object.

Page 5: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 5

Two Kinds of Operation (II)

Practical distinctions in the syntax (or form) of expressions:

e.g. performing addition on integers,

int z = x + 3;

e.g. performing a substring operation on a String,

String b = a.substring(1, 3);

General form of method call:

<object reference>.<method name>(<actual parameters>)

Page 6: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 6

Complications

String is a class but the concept is partially defined in the core language. Not such a neat divide.

e.g. String concatenation,

String s = “fred”+” jim”;

Page 7: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 7

Two Kinds of Data Value

Another name for object type is reference type because the values are references. e.g.

String name = “Stephan”;

int i = 3;

Representation of storage in computer memory:

:String

‘S’, ‘t’, ‘e’, ‘p’, ‘h’, ‘a’, ‘n’

name

3 i

Page 8: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 8

Two Kinds of Storage (II)

Final example,

String name1 = “Stephan”;

String name2 = name1;

:String

‘S’, ‘t’, ‘e’, ‘p’, ‘h’, ‘a’, ‘n’

name1

name2

Representation of storage in computer memory:

Page 9: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 9

Exercises: Understanding Object References

Consider Classes A, B, C in appendices.

Points1. Declaring a variable to be of an object type does

not automatically create an object.2. The execution of a method may involve the

creation and use of other objects.3. References to objects may be passed as

parameters, stored in variables, returned as method results.

Page 10: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 10

Exercise One

Draw an object diagram that depicts the outcome of executing the following fragment:

//…

B vB = new B();

C vC = new C();

A vA = new A();

vA.setOne(vB);

vA.setTwo(vC);

//…

Page 11: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 11

Exercise Two

Draw an object diagram that depicts the outcome of executing the following fragment:

//…

B vB = new B();

A vA = new A();

vA.setOne(vB);

vB.setTwo(new C());

//…

Page 12: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 12

Exercise Three

Draw an object diagram that depicts the outcome of executing the following fragment:

//…

C vC = new C();

A vA = new A();

B vB = new B(null, vC);

vC.setTwo(vB);

//…

Page 13: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 13

Exercise Four

Draw an object diagram that depicts the outcome of executing the following fragment:

//…

A vA = new A();

B vB = new B(vA, vC);

C vC = new C(new B());

vA.setTwo(vB.getTwo());

//…

Page 14: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 14

Challenge: Family Tree

Let’s say we want to represent a family tree, we choose to design a Person class, but here’s the data we want to store:

• Name• Date of birth• Date of death• Father• Mother

How can we do this? Many possibilities….

Page 15: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 15

Exercises: Self-referential Classes

Consider classes D and E in the appendices.

Points

1. An object can contain within its fields references to other objects of the same type.

2. An operation on one object may involve using and manipulating other objects of the same type.

Page 16: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 16

Exercise One

Draw an object diagram that depicts the outcome of executing the following fragment:

//…

D dOne = new D();

D dTwo = new D();

D dThree = new D();

D dFour = new D();

dTwo.setOther(dOne);

dThree.setOther(dOne);

dFour.setOther(dOne);

//…

Page 17: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 17

Exercise Two

Draw an object diagram that depicts the outcome of executing the following fragment:

//…

D dOne = new D(1);

D dTwo = new D(2);

dTwo.setOther(dOne);

D dThree = new D(dTwo, 3);

dOne.setOther(dThree, 5);

//…

Page 18: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 18

Exercise Three

Draw an object diagram that depicts the outcome of executing the following fragment:

//…E eOne = new E(1);E eTwo = new E(5);E eThree = new E(3);E eFour = new E(7);eOne.setFields(null, eTwo, eThree, null);eTwo.setFields(eOne, null, eFour, null);eThree.setFields(null,eFour, null, eOne);eFour.setFields(eThree, null, null, eTwo);//…

Page 19: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 19

Exercise Four

Draw an object diagram that depicts the outcome of executing the following fragment:

//…

D dOne = new D(1);

D dTwo = new D(dTwo, 5);

D dThree = new D(dTwo, 3);

D dFour = new D(dThree, 7);

dFour.printValue();

//…

Page 20: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 20

Challenge: CyberPet

Consider the CyberPet class in the appendicies

It’s possible for a pair of CyberPets to produce offspring. A CyberPet created in this way has energy and health levels inherited from its parents. It gains the health level of the less healthy parent and the energy level of the more energetic parent.

Write a “mate” instance method that accepts a CyberPet reference as a parameter and returns a new CyberPet with the required characteristics.

Page 21: Copyright Department of Computer Science University of Durham 1 Reasoning with Objects Object references and self-referential class declarations.

Copyright Department of Computer ScienceUniversity of Durham 21

Summary

There are two kinds of data type in java: primitive types and object types. The latter is complicated by the inclusion of the concept of reference. The data value that is stored in a variable defined to be an object type is in fact a reference to an object.

Through reference it is possible to build object structures that reflect real world structures.

It is possible and desirable for an object of a certain type to refer to other objects of the same type.