15-100 Lecture 33 (Wednesday April 13, 2005)

Teaching to the Mastery

So in the mastery exam you have an array collection. You will have to write an insert and a remove based on one of nine different idioms (these idioms can be found on the intro group's homepage). The collection class will have a count of how many places in your array are taken. However, the count of how many of each type of item will not be part of the collection class instead it will be a variable in the items/objects that are being collected. So you can only keep a collection of objects that have getCount and setCount methods.

This doesn't make sense because in the real world an object has no clue how many other objects like it exist. This is a design issue with the exam. However, you have to know how to code with this type of design because it is on the exam.

The Object to be Collected (simplified)

You will have to write this class...filling in the specifics from reading the test driver and the Word document that also come as part of the exam. Remember, the .doc will not contain all of the information you need to sucessfully complete the exam.


class Item{
  private String name;
  private int count;

  private static final int DEFAULT_COUNT = 1;
  
  //the mastery's constructor will contain some type of logic (if statements)
  public Item (String name, int count) {
    this.name = name;
    this.count = count;
  }

  public Item (String name) {
    this.name = name;
    count = DEFAULT_COUNT;
  }

  //the accessors
	
  public int getCount(){
    return count;
  }

  public String getName() {
    return name;
  } 

  // the modifiers
	
  public void changeCountBy(int change) {
    count += change;
  }

  public String toString() {
    return name + ": " + count;
  }

  public boolean equals(Object o){
    Item item = (Item)o;

    if (!this.name.equals(item.name))
      return false;

    if (this.count != item.count)
      return false;

    return true;
  }
}
Collection Class

class CountedCollection {
  private Item[] = items;
  private int count;

  public CountedCollection() {
    items = new Item[1];
    count = 0;
  }

  //not provided
  public CountedCollection(int size) {
    items = new Item[size];
    count = 0;
  }

  private void grow(){
    Item [] biggerArray = new Item[2*items.length];
    for (int index = 0; index < count; index++)
      biggerArray[index] = items[index];

    items = biggerArray;
  }

  private void insertAtEnd(Item item) {
    if (count == items.length)
      grow();

    items[count++] = item;
  }

  //we look for the item in the collection.  If it is there then we increase its
  //count by one.  Other wise we insert the newItem at the end. 
  private void insert (Item newItem){
    location = findLocation(newItem);
    if (location >= 0)
      item[location].changeCountBy(1);
    else
      insertAtEnd(newItem);
  }

  private int findLocation (Item item) {
    for (int i = o; i < count; i++)
      if (items[i].getName().equals(item.getName())
        return i;

    return -1;
  }

  public void remove(Item item, int count) {
    int location = findLocation(item);

    if(location < 0)
      return;

    items[location].changeCountBy(count);

    //if there are still one or more items of that type, we are done...exit the method
		if (items[location].getCount() > 0)
      return;

    //shift each item behind location forward by one (to cover up hole caused by
    //removing the item)
    for (int i = location; i < (count-1); index ++){
      items[index] = items[index+1];
      items[index+1] = null;
    }
    count--;
  }
}