1 / 24

CompSci 105 SS 2005 Principles of Computer Science

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

melita
Download Presentation

CompSci 105 SS 2005 Principles of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Abstract Data Types ADT Operations Program that uses the ADT ADT Implementation Textbook, p. 110

  3. 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 (); }

  4. public class MyCircle implements Circle { float x, y, radius; void setRadius( float newRadius ) { radius = newRadius; } float getArea() { return ( Math.PI * radius * radius ); } }

  5. 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 ); } }

  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 ADT Implementation 1 Program that uses the ADT ADT Operations ADT Implementation 2

  11. Changing the radius AreaCircle A = new AreaCircle(); A.setRadius(.5);

  12. The Wall ADT Operations Program that uses the ADT ADT Implementation Textbook, p. 110

  13. 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 ); } }

  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 0 1 2 3 4 anArray: 1 3 7 8 9 first: last:

  18. Java Reference Variables Integer intRef = new Integer(5); intRef: Textbook, pp. 153-154

  19. Digramming References Integer intRef = new Integer(5); Integer p = null; intRef: 5 p:

  20. 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

  21. 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

  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 p: q: Integer p, q; p = new Integer(5); q = new Integer(5); if ( p == q ) println(“Equal”); else println(“Not”);

  24. 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”);

More Related