A3: Implementing a Data Abstraction

CPSC 210

Learning Goals

  • Know how to choose an internal data representation (fields) for your data abstraction
  • Know how to implement methods to accomplish the behaviour specified

Test-Driven Dev. (TDD)

Write Tests

Run Tests

FAIL

Implement

PASS

Example

public class Tank {
  // ...
  private int direction;
  // ...
  
  // Construct a tank.
  // effects: places tank at position (x, Y_POS) moving right
  public Tank(int x) {
    this.x = x;
    direction = 1;
  }
  
  // Faces tank to the right
  // modifies: this
  // effects: tank is facing right
  public void faceRight() {
    direction = 1;
  }
  
  // Faces tank to the left
  // modifies: this
  // effects: tank is facing left
  public void faceLeft() {
    direction = -1;
  }
  
  // ...
}

Maybe we could change how direction is implemented?

Lecture Ticket

Lecture Ticket Review

public class Cake {
  //REQUIRES: !isBaked()
  //MODIFIES: this
  //EFFECTS:  changes the status of 
  //the cake to baked
  public void bake() {  // stub  }

  //REQUIRES: isCool()
  //MODIFIES: this
  //EFFECTS:  changes the status of the 
  //cake to decorated
  public void decorate() {  // stub  }

  //REQUIRES: isBaked()
  //MODIFIES: this
  //EFFECTS:  changes the status of 
  //the cake to cool
  public void cool() {  // stub  }

  //EFFECTS:  returns true if 
  //the cake is baked
  public boolean isBaked() { 
    return false;  // stub 
  }

  //EFFECTS: returns true if 
  //the cake is cool
  public boolean isCool() { 
    return false;  // stub 
  }
}

Lecture Lab

          We finally implement it!!!

A3: Implementing a Data Abstraction

The End - Thank You!

Made with Slides.com