copyright © 2002, systems and computer engineering, carleton university. 94.204-04a-javareview.ppt...

55
Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit 4(a) Java Review

Upload: sharon-mccormick

Post on 16-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

1

94.204* Object-Oriented Software Development

Unit 4(a)

Java Review

Page 2: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

2

Java Review

• In this unit, we shall briefly review the Java syntax that you are expected to know already :

– Object Construction

– Object References

• Aliasing

• .. As Method Parameters

– Overloading

– Arrays

Page 3: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

3

Object Construction

• This statement declares variable c of class (type) Complex:

Complex c;

• c is NOT a Complex object

• Instead, variable c can hold a reference to a Complex object

Page 4: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

4

Object Construction

• Here’s how we create a Complex object::

c = new Complex();

• new creates a new instance of class Complex

• The object initializes its state to its default value by invoking its default constructor

• new returns a reference to the object, which is assigned to c

Page 5: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

5

Object Construction

• Often, we combine these two statements:

Complex c = new Complex();

• Important: students sometimes think that every time they declare a variable of class type, they must use this form (i.e., they think its illegal to declare a variable of class type without initializing it to refer to a new instance of the class)

• This is not true!

Page 6: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

6

Object Construction

• The statement

c = new Complex(5.2);

creates a new Complex object that represents 5.2 + 0.0i

• The statement

c = new Complex(-8.5, 9.1);

creates a new Complex object that represents -8.5 + 9.1i

Page 7: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

7

Object References

• Consider the declaration

Complex c;• c is NOT an object of class Complex• c is a variable whose value is a reference

to a Complex object (an instance of class Complex)

• Aside: for brevity, we often use phrases like “object c” when we really mean “the object to which c refers”. This is o.k., as long as we remember that, in Java, all variables of class type store references to objects

Page 8: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

8

Object References

• Suppose the declaration:

Complex c;reserves a word of memory at address 12795130

• This word is initialized to the null reference, null– when the value of a variable of class

type is null, it does not refer to any object

c12795130

represents null

Page 9: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

9

Object References

• Suppose new allocates a new object at address 21985314

c = new Complex();• This address is returned by new and stored

in c– N.B. a reference is not necessarily a 32-

bit address, but to simplify explanations, we will assume it is

Page 10: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

10

Object References

c12795130

• c now refers to the object

21985314

219853140.0

0.0

real

imag

Page 11: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

11

Object References

• We’ll often use this notation when we want to distinguish between variables of class type and objects referred to by those variables

Variable containing anobject reference

An object

A reference to an object

Page 12: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

12

Object References

• Example: variable c, after it has been assigned the reference to the new Complex object

c

Page 13: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

13

c3 = c1.plus(c2);

4.9 + 0.0i

5.1 - 8.6i

c1

c2

Complex number c1: give me a new complex number that contains the sumof your value and the value of complexnumber c2

Page 14: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

14

Aliasing

• Consider:

Complex c = new Complex();

Complex c1;

c1 = c;• The = operator assigns the value of c (an

object reference) to c1• c1 and c are now aliased to (i.e., they

refer to) the same object

Page 15: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

15

Aliasing

• Before c1 = c;

is executed:

c

c1

Page 16: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

16

Aliasing

• After c1 = c;

is executed:

c

c1

Page 17: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

17

Another Aliasing Example

Complex c1 = new Complex(5.0, 3.0);

Complex c2 = new Complex(-1.0, 2.0);

c1

c2

5.0

3.0

-1.0

2.0

Page 18: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

18

Another Aliasing Example

• The = operator does not copy objects• c1 = c2;

does NOT do this:

c1

c2

-1.0

2.0

-1.0

2.0

Page 19: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

19

Another Aliasing Example

• After

c1 = c2;

is executed, c1 and c2 refer to the same object:

c1

c2

5.0

3.0

-1.0

2.0

Page 20: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

20

Another Aliasing Example

• What happens to the object that represents 5.0 + 3.0i?

• No variables refer to that object, so it can no longer receive messages

• The Java run-time system will garbage-collect the object (reclaim the memory allocated to the object for reuse)

Page 21: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

21

Object References as Method Parameters

• Parameter c of plus() is declared as Complex

public Complex plus(Complex c)

{

Complex sum =

new Complex(real + c.real,

imag + c.imag);

return sum;

}

Page 22: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

22

Object References as Method Parameters

• The argument of the plus() message is a reference to a Complex object:

Complex c1 = new Complex(5.1, -8.6);

Complex c2 = new Complex(4.9);

Complex c3;

// c3 = c1 + c2

c3 = c1.plus(c2);

Page 23: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

23

Object References as Method Parameters

• Some books say that Java passes objects by reference, other books say that it passes objects by value

• Which is it?

• When the plus() message is sent to the object referred to by c1:

c3 = c1.plus(c2);

the value stored in argument c2 is passed by value to formal parameter c

• Java ALWAYS passes arguments to methods by value

Page 24: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

24

Object References as Method Parameters

• The value stored in c2 is a reference to an object

• Therefore, formal parameter c is aliased to the object referred to by c2 when the plus() method is invoked

4.9 0.0

c2 c

Page 25: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

25

Object References as Method Parameters

• In other words, when a method parameter has class type, the argument (a reference to an object) is passed by value to the parameter

• Because the value is a reference to an object, that’s why some books say that Java objects are passed by reference to methods

Page 26: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

26

Tracing the Execution of plus()

c3 = c1.plus(c2);

• After the plus() message is sent to c1, but before

Complex sum = new Complex(...);

is executed...

Page 27: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

27

Tracing the Execution of plus()

c2

c

c1

c3

Page 28: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

28

c2

c

sum

c1

c3

Tracing the Execution of plus()

• After Complex sum = new Complex(...);is executed...

Page 29: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

29

Tracing the Execution of plus()

• After plus() returns, the object reference it returns is assigned to c3:

c3 = c1.plus(c2);

• Variable sum and parameter c “disappear”

Page 30: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

30

Tracing the Execution of plus()

c2

c1

c3

Page 31: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

31

A Note About Instance Variable Visibility

• Notice that plus() directly accesses the real and imag variables

– of the Complex object that receives the message

– of the Complex object referenced by parameter c

• Students sometimes think that a method can access the private variables of the object that received the message, but cannot access the private variables of other instances of the same class

• This is not true

Page 32: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

32

A Note About Instance Variable Visibility

• A method defined in one class can access the private variables of any instance of the same class

• A method defined in one class cannot access the private variables of objects that are instances of other classes

Page 33: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

33

Overloading Methods

• Overloading permits several methods to have the same name– They must be distinguished by

different argument lists– Having only a different return type

will not be sufficient - compile error• When invoking an overloaded method,

Java determines which method to invoke based on the method’s signature

• signature == the number of parameters in the method’s parameter list, and their types

Page 34: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

34

Classes Can Have Several Constructors

• We have already seen that class Complex has 3 constructors:

public Complex() {...}public Complex(double rp) {...}

public Complex(double rp, double ip) {...}

Complex x = new Complex();– invokes the 0-argument (default)

constructorComplex y = new Complex(-7.3);

– invokes the 1-argument constructorComplex z = new Complex(1.2, 3.7);– invokes the 2-argument constructor

Page 35: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

35

Overloading Complex’s plus() method

• Overloading isn't limited to constructors - methods can be overloaded

• Suppose we want to be able to add a real number to a complex number

• We could create a new complex number from the real number (i.e., use the 1-argument constructor), then pass that complex number to plus()

• Instead, why not have a second plus() method, one that accepts a real number argument?

Page 36: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

36

Overloading Complex’s plus() method

/*** Return the sum of this

* Complex number and its

* argument.

** @param r A real number

* to be added to this

* complex number.* @return A new Complex

* number equal to the sum

* this Complex number

* and r.*/

Page 37: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

37

Overloading Complex’s plus() method

public Complex plus(double r)

{

// r is same as r + 0.0i Complex sum = new

Complex( real + r,

imag); return sum;

}• This plus() method and the one we saw

earlier both have 1 parameter, but the types are different

Page 38: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

38

Overloading Complex’s plus() method

• Overloaded methods have the same method name but different signatures

• When a message is sent to an object, the Java interpreter checks the number & types of the arguments and the type of the return value, and looks for a method with a compatible signature

– e.g., when the plus() message is sent to a Complex object Java determines which plus() method to invoke based on whether the argument is of type Complex or type double

Page 39: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

39

Overloading Methods continued

• Overloading eliminates the need for multiple names for essentially the same function - error prone and tedious:

int SquareInt (int val)

{

return val * val;

}

float SquareFloat (float val) { return val * val;}

Page 40: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

40

Overloading Methods continued

• Multiple names are eliminated

int Square (int val)

{

return val * val;

}float Square (float val){ return val*val; }

• Different return types WILL NOT suffice

int DoX() {…}

float DoX {…} // Wrong

Page 41: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

41

Java Arrays

• A Java array is a container which holds a contiguous collection of elements of the same type– An array is a special type of object - it

has no class• Handled directly by compiler• Is a pseudo-primitive type

• There are 2 types of arrays– Array of primitive values– Array of references to objects

Array of primitive Array of references to values objects

• Just like C/C++ they are indexed from 0

2355...78

primVal [0]primVal [1]

primVal [n - 1]

...

ref [0]ref [1]

ref [n - 1]

obj0

obj1

obj

Page 42: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

42

Arrays of Primitive Values

• An array must be declared and space must be allocated for it

// Declare a reference to an array of integers

int [] intArray; // No memory allocated yet!

// Allocate space for 4 int elements

intArray = new int [4]; // Note the []

intArray

intArray [0]intArray [1]intArray [2]

intArray [3]

intArray

Page 43: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

43

Arrays of Primitive Values continued

intArray [0] = 25;

intArray [1] = 43;

intArray [2] = 57;

intArray [3] = 79;

• We can declare and allocate space for the primitive values in one stepint [] intArray = new int [4];

...

• Declare, allocate and initialize in 1 stepint [] intArray = {25, 43, 57, 79};

– Convenient - don't have to think about array length

25435779

intArray [0]intArray [1]intArray [2]

intArray [3]

intArray

Page 44: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

44

Arrays of Objects

• Arrays of objects require an additional step (to allocate memory for the objects) // First, declare a reference to an array of

// references to PairClass objects (just like// declaring an array of primitive type)

PairClass [] pairArray; // No mem allocated yet!

// Allocate space for 4 references to PairClass objs

pairArray = new PairClass [4];

pairArray [0]pairArray [1]pairArray [2]

pairArray [3]

pairArray

pairArray

Array of 4 uninitialized references to PairClass objects

Page 45: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

45

Here's the Extra Step..

// Allocate space for the PairClass objects// ..assume a PairClass (int x, int y) constructor..pairArray [0] = new PairClass (5,9);// … Repeat for elements 1, 2 and 3…

• We can also declare and allocate space for the references to objects in 1 stepPairClass [] pairArray = new PairClass [4];...

• Declare, allocate array of references and allocate space for objects in 1 stepPairClass [] pairArray =

{ new PairClass (5,9), new PairClass (9,4)

new PairClass (6,3), new PairClass (2,1) };

pairArray [0]pairArray [1]pairArray [2]

pairArray [3]

pairArray

5,9 PairClass obj9,4 PairClass obj6,3 PairClass obj2,1 PairClass obj

Page 46: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

46

Array length Instance Variable

• A useful public instance variable for array objects

– length is the length of the array object

• pairArray.length is equal to 4for (int i = 0; i < pairArray.length; i++)

{pairArray [i] = new PairClass ();

pairArray [i].setA (0);

pairArray [i].setB (i);

}

• This is another example of encapsulation

– The array object knows how big it is

– We don't need to maintain some constant outside the array object that has to be passed to every method that operates on arrays of that type

Page 47: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

47

length and final Variables

• The length instance variable is final– It can be assigned only once (i.e. when

the array is instantiated)– This is true of all final variables

final int someInt = 3; // Proper way

...is the same asfinal int someInt; // Don't do this!someInt = 3; // Legal - but a bad idea!… // Now, attempt to reassign the value// The compiler will flag this as an errorsomeInt = 4; // Compiler error

• See Ch 11.8.3 for details about multi-dimensional arrays

Page 48: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

48

Are You Ready ?

• In the next series of slides, poor code is presented.

– For the Complex class already introduced, we want a method that will allow us to add two complex numbers and store the sum.

• If you can figure out the problems in the code by yourself, you are ready.

• If you can follow the discussion, prepare yourself for some work.

• If you can’t follow the discussion, you are not ready for 94.204

Page 49: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

49

A Poor Version of plus()

public void plus(Complex c, Complex sum)

{

sum.setReal(real + c.real);

sum.setImag(imag + c.imag);

}

• This version has two parameters

– a reference to the object that is to be added to the object that receives the message

– a reference to the object that will contain the sum

Page 50: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

50

A Poor Version of plus()

• Code showing how to invoke the method:

Complex c1 = new Complex(5.1, -8.6);

Complex c2 = new Complex(4.9);

Complex c3 = new Complex();

// c3 = c1 + c2

c1.plus(c2, c3);

• Explain how this code works.

• Identify the precondition that programmers must meet before invoking this method.

Page 51: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

51

A Poor Version of plus()

• Notice that the client must instantiate the Complex object (referred to by c3) where the result will be stored, before sum is aliased to the same object as c3

• Sending the setReal() and setImag() messages to sum changes the state of the object referred to by c3

• This is not obvious from the statement that sends the plus() message to c1

Page 52: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

52

Side Effects

• If a method changes the state of an object that is passed to it as an argument (e.g., by sending the object a setter message), this is known as a side-effect of the method invocation

• This must be well-documented near the code that invokes the method

• In general, it is better for a method to create a new object and return a reference to it (instead of changing the state of the objects passed as method arguments)

Page 53: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

53

Why Doesn’t This Code Work?

• This version of plus() creates the object that holds the result:

public void plus( Complex c,

Complex sum)

{

sum = new Complex

(real + c.real,

imag+ c.imag);

}

Page 54: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

54

Why Doesn’t This Code Work?

Complex c1 = new Complex(5.1, -8.6);

Complex c2 = new Complex(4.9);

Complex c3;

// c3 = c1 + c2

c1.plus(c2, c3);

• So why doesn’t c3 have the correct result when plus() returns? (Hint: trace the execution using box-oval diagrams)

Page 55: Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt 1 94.204* Object-Oriented Software Development Unit

Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-04a-JavaReview.ppt

55

The Good Version

public Complex plus(Complex c)

{

Complex sum =

new Complex( real + c.real,

imag + c.imag );

return sum;

}

Complex c1 = new Complex( 5.1, -8.6);

Complex c2 = new Complex( 4.9 );

Complex c3;

// c3 = c1 + c2

C3 = c1.plus( c2 );

• Explain why this version is “good code”.