CompSci 105 SS 2005 Principles of Computer Science

Post on 08-Jan-2016

26 views 0 download

description

CompSci 105 SS 2005 Principles of Computer Science. Lecture 10: References and Objects. Lecturer: Santokh Singh. Abstract Data Types. ADT Operations. Program that uses the ADT. ADT Implementation. Textbook, p. 110. Java Interface. public interface Circle { void setX ( float x ); - PowerPoint PPT Presentation

Transcript of CompSci 105 SS 2005 Principles of Computer Science

1CompSci 105 SS 2005

Principles of Computer Science

Lecture 10: References and Objects

Lecturer: Santokh Singh

2

Abstract Data Types

Program that uses the ADT

ADTImplementation

AD

T O

perations

Textbook, p. 110

3

Java Interfacepublic interface Circle {

void setX ( float x );

void setY ( float y );

void setRadius( float r );

float getX ();

float getY ();

float getRadius();

float getArea ();

}

4

public class MyCircle implements Circle

{

float x, y, radius;

void setRadius( float newRadius ) {

radius = newRadius;

}

float getArea() {

return ( Math.PI * radius * radius );

}

}

5public class AreaCircle implements Circle

{

float x, y, radius, area;

void setRadius( float newRadius ) {

radius = newRadius;

area = Math.PI * radius * radius ;

}

float getArea() {

return ( area );

}

}

6

Interface Circle

Class MyCircle(fast setRadius)(slow getArea)

Class MyCircle(slow setRadius)

(fast getArea)

7

A method that uses Circle

public boolean isBigger( Circle A,

Circle B)

{

if ( A.getArea() > B.getArea() )

return(true);

else

return(false);

}

8

A method that uses MyCircle

public boolean isBigger( MyCircle A,

MyCircle B)

{

if ( A.getArea() > B.getArea() )

return(true);

else

return(false);

}

9

Creating Circles

Circle A = new MyCircle();

Circle B = new AreaCircle();

MyCircle C = new MyCircle();

AreaCircle D = new AreaCircle();

boolean x = isBigger( A, B );

boolean y = isBigger( C, D );

10

ADTs: Multiple Implementations

Program that uses the ADT

ADTImplementation

1

AD

T O

perations ADTImplementation

2

11

Changing the radius

AreaCircle A = new AreaCircle();

A.setRadius(.5);

12

The Wall

Program that uses the ADT

ADTImplementation

AD

T O

perations

Textbook, p. 110

13public Class AreaCircle implements Circle

{

private float x, y, radius, area;

public void setRadius( float newR ) {

radius = newRadius;

area = Math.PI * radius * radius );

}

public void getArea() {

return ( area );

}

}

14

In Tutoral 5 ….

• Array implementation of List ADT

15

The Towers of HanoiTextbook pp. 85-91

Source Target Spare

16

References and Objects

Object Storage

Object References

Diagramming Objects and References

Garbage Collection

Example: Equality

Example: Parameter Passing

Example: Resizable Arrays

17

Arrays: Index and Value

731 98anArray:

first:

last:

0 1 2 3 4

18

Java Reference Variables

Integer intRef = new Integer(5);

intRef:

Textbook, pp. 153-154

19

Digramming References

intRef: 5

p:

Integer intRef = new Integer(5);

Integer p = null;

20

Diagramming References

Integer p, q;

p = new Integer(5);

p = new Integer(6);

q = p;

q = new Integer(9);

p = null;

q = p;

p: q:

Textbook, pp. 155

21

Garbage Collection

Integer p, q;

p = new Integer(5);

p = new Integer(6);

q = p;

q = new Integer(9);

P = null;

q = p;

p: q:

Textbook, pp. 155

22

References and Objects

Object Storage

Object References

Diagramming Objects and References

Garbage Collection

Example: Equality

Example: Parameter Passing

Example: Resizable Arrays

23

Equality of References

Integer p, q;

p = new Integer(5);

q = new Integer(5);

if ( p == q )

println(“Equal”);

else

println(“Not”);

p: q:

24

Equality of References

Integer p, q;

p = new Integer(5);

p = new Integer(5);

if ( p.equals(q) )

println(“Equal”);

else

println(“Not”);

p: q: