public class hw334 extends Vic { // will return false if parameter vic has a different number of cds as the current // instance has public boolean sameNumber(Vic par) { // as long as either has at least a slot left while (this.seesSlot() || par.seesSlot()) { // loop through this until we find a cd as long as we have a slot while (!this.seesCD() && this.seesSlot()) { this.moveOn(); } // at this point this either has run out of slots or has found a cd // do the same thing for par while (!par.seesCD() && par.seesSlot()) { par.moveOn(); } /* make sure both of the sequences actually did find a cd, or both did not find a cd.*/ if (par.seesCD() == this.seesCD()) { if (par.seesSlot()) par.moveOn(); if (this.seesSlot()) this.moveOn(); } else return false; // if only one found a cd they aren't equal } // end of the while loop return true; // if we're all the way over here, we KNOW it all worked out } // this is an alternate solution using integers... public boolean sameNumber1(Vic par) { return countcds(this) == countcds(par); } private int countcds(Vic v) { int counter = 0; while (v.seesSlot()) { if (v.seesCD()) counter++; v.moveOn(); } return counter; } }