240 likes | 368 Views
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 );
E N D
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 ); void setY ( float y ); void setRadius( float r ); float getX (); float getY (); float getRadius(); float getArea (); }
public class MyCircle implements Circle { float x, y, radius; void setRadius( float newRadius ) { radius = newRadius; } float getArea() { return ( Math.PI * radius * radius ); } }
public class AreaCircle implements Circle { float x, y, radius, area; void setRadius( float newRadius ) { radius = newRadius; area = Math.PI * radius * radius ; } float getArea() { return ( area ); } }
Interface Circle Class MyCircle (fast setRadius) (slow getArea) Class MyCircle (slow setRadius) (fast getArea)
A method that uses Circle public boolean isBigger( Circle A, Circle B) { if ( A.getArea() > B.getArea() ) return(true); else return(false); }
A method that uses MyCircle public boolean isBigger( MyCircle A, MyCircle B) { if ( A.getArea() > B.getArea() ) return(true); else return(false); }
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 );
ADTs: Multiple Implementations ADT Implementation 1 Program that uses the ADT ADT Operations ADT Implementation 2
Changing the radius AreaCircle A = new AreaCircle(); A.setRadius(.5);
The Wall ADT Operations Program that uses the ADT ADT Implementation Textbook, p. 110
public 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 ); } }
In Tutoral 5 …. • Array implementation of List ADT
The Towers of HanoiTextbook pp. 85-91 Source Target Spare
References and Objects Object Storage Object References Diagramming Objects and References Garbage Collection Example: Equality Example: Parameter Passing Example: Resizable Arrays
Arrays: Index and Value 0 1 2 3 4 anArray: 1 3 7 8 9 first: last:
Java Reference Variables Integer intRef = new Integer(5); intRef: Textbook, pp. 153-154
Digramming References Integer intRef = new Integer(5); Integer p = null; intRef: 5 p:
Diagramming References p: q: Integer p, q; p = new Integer(5); p = new Integer(6); q = p; q = new Integer(9); p = null; q = p; Textbook, pp. 155
Garbage Collection p: q: Integer p, q; p = new Integer(5); p = new Integer(6); q = p; q = new Integer(9); P = null; q = p; Textbook, pp. 155
References and Objects Object Storage Object References Diagramming Objects and References Garbage Collection Example: Equality Example: Parameter Passing Example: Resizable Arrays
Equality of References p: q: Integer p, q; p = new Integer(5); q = new Integer(5); if ( p == q ) println(“Equal”); else println(“Not”);
Equality of References p: q: Integer p, q; p = new Integer(5); p = new Integer(5); if ( p.equals(q) ) println(“Equal”); else println(“Not”);