Top Banner
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 10: References and Objects Lecturer: Santokh Singh
24

CompSci 105 SS 2005 Principles of Computer Science

Jan 08, 2016

Download

Documents

melita

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
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: CompSci 105 SS 2005 Principles of Computer Science

1CompSci 105 SS 2005

Principles of Computer Science

Lecture 10: References and Objects

Lecturer: Santokh Singh

Page 2: CompSci 105 SS 2005 Principles of Computer Science

2

Abstract Data Types

Program that uses the ADT

ADTImplementation

AD

T O

perations

Textbook, p. 110

Page 3: CompSci 105 SS 2005 Principles of Computer Science

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 ();

}

Page 4: CompSci 105 SS 2005 Principles of Computer Science

4

public class MyCircle implements Circle

{

float x, y, radius;

void setRadius( float newRadius ) {

radius = newRadius;

}

float getArea() {

return ( Math.PI * radius * radius );

}

}

Page 5: CompSci 105 SS 2005 Principles of Computer Science

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 );

}

}

Page 6: CompSci 105 SS 2005 Principles of Computer Science

6

Interface Circle

Class MyCircle(fast setRadius)(slow getArea)

Class MyCircle(slow setRadius)

(fast getArea)

Page 7: CompSci 105 SS 2005 Principles of Computer Science

7

A method that uses Circle

public boolean isBigger( Circle A,

Circle B)

{

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

return(true);

else

return(false);

}

Page 8: CompSci 105 SS 2005 Principles of Computer Science

8

A method that uses MyCircle

public boolean isBigger( MyCircle A,

MyCircle B)

{

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

return(true);

else

return(false);

}

Page 9: CompSci 105 SS 2005 Principles of Computer Science

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 );

Page 10: CompSci 105 SS 2005 Principles of Computer Science

10

ADTs: Multiple Implementations

Program that uses the ADT

ADTImplementation

1

AD

T O

perations ADTImplementation

2

Page 11: CompSci 105 SS 2005 Principles of Computer Science

11

Changing the radius

AreaCircle A = new AreaCircle();

A.setRadius(.5);

Page 12: CompSci 105 SS 2005 Principles of Computer Science

12

The Wall

Program that uses the ADT

ADTImplementation

AD

T O

perations

Textbook, p. 110

Page 13: CompSci 105 SS 2005 Principles of Computer Science

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 );

}

}

Page 14: CompSci 105 SS 2005 Principles of Computer Science

14

In Tutoral 5 ….

• Array implementation of List ADT

Page 15: CompSci 105 SS 2005 Principles of Computer Science

15

The Towers of HanoiTextbook pp. 85-91

Source Target Spare

Page 16: CompSci 105 SS 2005 Principles of Computer Science

16

References and Objects

Object Storage

Object References

Diagramming Objects and References

Garbage Collection

Example: Equality

Example: Parameter Passing

Example: Resizable Arrays

Page 17: CompSci 105 SS 2005 Principles of Computer Science

17

Arrays: Index and Value

731 98anArray:

first:

last:

0 1 2 3 4

Page 18: CompSci 105 SS 2005 Principles of Computer Science

18

Java Reference Variables

Integer intRef = new Integer(5);

intRef:

Textbook, pp. 153-154

Page 19: CompSci 105 SS 2005 Principles of Computer Science

19

Digramming References

intRef: 5

p:

Integer intRef = new Integer(5);

Integer p = null;

Page 20: CompSci 105 SS 2005 Principles of Computer Science

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

Page 21: CompSci 105 SS 2005 Principles of Computer Science

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

Page 22: CompSci 105 SS 2005 Principles of Computer Science

22

References and Objects

Object Storage

Object References

Diagramming Objects and References

Garbage Collection

Example: Equality

Example: Parameter Passing

Example: Resizable Arrays

Page 23: CompSci 105 SS 2005 Principles of Computer Science

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:

Page 24: CompSci 105 SS 2005 Principles of Computer Science

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: