CompSci 105 SS 2005 Principles of Computer Science
description
Transcript of CompSci 105 SS 2005 Principles of Computer Science
![Page 1: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/11.jpg)
11
Changing the radius
AreaCircle A = new AreaCircle();
A.setRadius(.5);
![Page 12: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/14.jpg)
14
In Tutoral 5 ….
• Array implementation of List ADT
![Page 15: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/15.jpg)
15
The Towers of HanoiTextbook pp. 85-91
Source Target Spare
![Page 16: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/17.jpg)
17
Arrays: Index and Value
731 98anArray:
first:
last:
0 1 2 3 4
![Page 18: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/18.jpg)
18
Java Reference Variables
Integer intRef = new Integer(5);
intRef:
Textbook, pp. 153-154
![Page 19: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/19.jpg)
19
Digramming References
intRef: 5
p:
Integer intRef = new Integer(5);
Integer p = null;
![Page 20: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062723/56813fec550346895daaeba8/html5/thumbnails/24.jpg)
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: