University of Toronto - Fall 2000
Department of Computer Science
Week 6 - Information Hiding
Original Coordinate class
public class Coordinate {
public double x;
public double y;
// Make me to represent location (x,y).
public Coordinate (double x, double y) {
this.x = x;
this.y = y;
}
// Move me by (dx,dy).
public void move (double dx, double dy) {
this.x = this.x + dx;
this.y = this.y + dy;
}
// Rotate me around the origin by d degrees CCW.
public void rotate (double d) {
// Ugh: Math.sin and other ugly calls needed
}
// Move me 's' units away from the origin.
public void stretch (double s) {
// Ditto ugh!
}
}
Modified Coordinate class
public class Coordinate {
public double r; // distance from origin
public double theta; // radians CCW from x axis
// Make me to represent location (x,y).
public Coordinate (double x, double y) {
// This is now Ughish, but that's okay since
// it doesn't need to be blazingly fast in
// this particular application.
}
// Move me by (dx,dy).
public void move (double dx, double dy) {
// This is now Ughish too, but again that's
// okay because of our application.
}
// Rotate me around the origin by d degrees CCW.
public void rotate (double d) {
theta = theta + d;
}
// Move me a factor of s away from the origin.
public void stretch (double s) {
r = r + s;
}
}