public class MoveOne { // Take a CD from the third slot; put it in the second slot.
public static void main (String[ ] args) { Vic sue; // 1 sue = new Vic(); // 2
sue.moveOn(); // 3 move to the second slot sue.moveOn(); // 4 move to the third slot sue.takeCD(); // 5 take CD from slot 3, put on stack
sue.backUp(); // 6 move back to the second slot sue.putCD(); // 7 put CD in slot 2, taken from stack } //====================== }
public class SmartVic extends Vic { public void moveTake() { moveOn(); takeCD(); } //======================
public void backPut() { backUp(); putCD(); } //====================== }
public class BringThreeBack { // Move the CDs in slots 2, 3, and 4 back to slots 1, 2, 3, // respectively. Presumes a reset with at least 4 slots.
public static void main (String[ ] args) { Vic.reset (args); // 1 SmartVic sue; // 2 sue = new SmartVic(); // 3 sue.moveTake(); // 4 move to slot 2 and take CD sue.backPut(); // 5 back to slot 1 and put CD there
sue.moveOn(); // 6 sue.moveTake(); // 7 move to slot 3 and take CD sue.backPut(); // 8 back to slot 2 and put CD there
sue.moveOn(); // 9 sue.moveTake(); // 10 move to slot 4 and take CD sue.backPut(); // 11 back to slot 3 and put CD there } //====================== }
public class TwoToFour { // If a CD is in the second slot, take it out and then // put it in the fourth slot if possible (robustly).
public static void main (String[ ] args) { Vic.reset (args); // 1 Vic sue; // 2 sue = new Vic(); // 3 sue.moveOn(); // 4
if (sue.seesCD()) // 5 if sue sees a CD in slot 2, { sue.takeCD(); // 6 then sue takes that CD sue.moveOn(); // 7 sue.moveOn(); // 8 if (sue.seesSlot()) // 9 if sue has a slot number 4, sue.putCD(); // 10 then sue puts the CD there } // 11 } //====================== }
public class ThreeSequences { // For each of the first three sequences, move a CD from the // third slot to the fifth slot, insofar as possible.
public static void main (String[ ] args) { Vic.reset (args); Shifter one; one = new Shifter(); // design step 1 one.shiftThreeToFive(); // design step 2
Shifter two; two = new Shifter(); // design step 3 if (two.seesSlot()) // design step 4 { two.shiftThreeToFive(); // design step 4a Shifter three; three = new Shifter(); // design step 4b if (three.seesSlot()) // design step 4c three.shiftThreeToFive(); } } //====================== }
public class Shifter extends Vic { // take the CD from slot 3 (if any) and put it in slot 5.
public void shiftThreeToFive() { moveOn(); moveOn(); // now in slot 3 if (seesCD()) { takeCD(); moveOn(); // now in slot 4 if (seesSlot()) { moveOn(); // now in slot 5 if (seesSlot()) putCD(); } } } //====================== }
public class FillThreeApp { /** Put a CD in each of the first three slots. * Stop as soon as you run out of CDs on the stack. */
public static void main (String[ ] args) { Vic.say ("This program fills the first three slots."); Vic.reset (args); // 2 if (Vic.stackHasCD()) // 3 { Vic sue; // 4 sue = new Vic(); // 5 sue.putCD(); // 6 in slot 1 if (Vic.stackHasCD()) // 7 { sue.moveOn(); // 8 to slot 2 sue.putCD(); // 9 if (Vic.stackHasCD()) // 10 { sue.moveOn(); // 11 to slot 3 sue.putCD(); // 12 } // 13 } // 14 } // 15 Vic.say ("All done!"); // 16 } //====================== }
public class TwoVics { /** Take the CD in slot 1 if there is one, otherwise put a * CD there; repeat for the second sequence, if any. */
public static void main (String[ ] args) { Vic first; // 1 first = new Vic(); // 2 if (first.seesCD()) // 3 first.takeCD(); // 4 else // 5 first.putCD(); // 6
Vic second; // 7 second = new Vic(); // 8 if (second.seesSlot()) // 9 { if (second.seesCD()) // 10 second.takeCD(); // 11 else // 12 second.putCD(); // 13 } // 14 } //====================== }
public class Checker extends Vic { /** Tell whether the stack has at least two CDs. * Precondition: the executor is not at the end * of its slots and the current slot is empty. */
public boolean hasTwoOnStack() { if ( ! stackHasCD()) // 1 return false; // 2 putCD(); // 3 if (stackHasCD()) // 4 { takeCD(); // 5 return true; // 6 } // 7 else // 8 { takeCD(); // 9 return false; // 10 } // 11 } //======================
/** Tell whether the executor's current slot and the one * after it exist and have no CD. No precondition. */
public boolean seesTwoEmpty() { if ( ! seesSlot()) // 12 return false; // 13 if (seesCD()) // 14 return false; // 15 moveOn(); // 16 if (seesSlot()) // 17 { if ( ! seesCD()) // 18 { backUp(); // 19 return true; // 20 } // 21 } // 22 backUp(); // 23 return false; // 24 } //====================== }
public class CheckerRewritten extends Vic // improved version { /** Tell whether the stack has at least two CDs. * Precondition: the executor is not at the end * of its slots and the current slot is empty. */
public boolean hasTwoOnStack() { if ( ! stackHasCD()) // 1 return false; // 2 putCD(); // 3 modify state boolean result; // 4 result = stackHasCD(); // 5 takeCD(); // 6 restore state return result; // 7 } //======================
/** Tell whether the executor's current slot and the * one after it exist and have no CD. No precondition. */
public boolean seesTwoEmpty() { if ( ! seesSlot() || seesCD()) return false; moveOn(); boolean valueToReturn; valueToReturn = seesSlot() && ! seesCD(); backUp(); return valueToReturn; } //====================== }
|