Jump to content

User talk:Pyrotrees

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Be sure to change Sha to Gua in every case.

12 public class MoveToFront { /** Move all the CDs in the first sequence of slots up to the * front of the sequence. Precondition: stack is empty. */ public static void main (String[ ] args) { Vic chun; // design step 1 chun = new Vic(); String spot; // design step 2 spot = chun.getPosition(); while (chun.seesSlot()) // design step 3 chun.moveOn(); while ( ! spot.equals (chun.getPosition()))// design step 4 { chun.takeCD(); chun.backUp(); } Vic.say ("All CDs are now on the stack."); while (chun.seesSlot() && Vic.stackHasCD())// design step 5 { chun.putCD(); chun.moveOn(); } Vic.say ("The first few slots are now filled."); } //====================== }

13 /** The executor tells whether its last slot is filled. Precondition: seesSlot()

*  is true. */
public boolean lastIsFilled()
{	Vic a;
	a = new Vic();
	String position;
	position = a.getPosition();
	while (seesSlot())
	moveOn();
	backUp();
	boolean answer;
	answer = a.seesCD();
	while (! position.equals (a.getPosition()))
	backUp();
	return answer;
}

14 public class Sha_Ex3_14 extends Vic { // The executor advances until it comes to the first empty slot public void goToFirstEmpty() { while (seesSlot() && seesCD()) moveOn(); }

// The executor advances until it comes to the first filled slot public void goToFirstFilled() { while (seesSlot() && ! seesCD()) moveOn(); } }

15 public class Sha_Ex3_15 extends Vic { // The executor advances until it comes to the first empty slot public void goToFirstEmpty() { while (seesSlot() && seesCD()) moveOn(); }

// The executor advances until it comes to the first filled slot public void goToFirstFilled() { while (seesSlot() && ! seesCD()) moveOn(); }

/** The executor moves one CD at a time to the earliest empty slot that comes

	*before that CD in the sequence. This gets all the CDs to the front of the
	*sequence without having more than one extra CD on the stack at a time. This
	*byOnes method should call on the other two methods in Finder as needed. */

public void byOnes() { String position; while (seesSlot()) { position = getPosition(); goToFirstFilled(); takeCD(); while (! position.equals (getPosition())) backUp(); goToFirstEmpty(); putCD(); moveOn(); } } }

16 /** Main is capitalized when it shouldn't be, and string isn't capitalized when it

 * should be */

17 /** spot equals Bob.getPosition() so nothing can happen during the while statement. */

18 /** Rewrite the hasSomeFilledSlot method in the earlier Listing 3.2 to

 * use this wherever it is allowed. */

public boolean hasSomeFilledSlot() { String spot; // design step 1

   spot = this.getPosition();
   while (this.seesSlot() && ! this.seesCD()) // design step 2
   this.moveOn();
   boolean valueToReturn; // design step 3
   valueToReturn = this.seesSlot();
   while ( ! spot.equals (this.getPosition())) // design step 4
   this.backUp();
   return valueToReturn; // design step 5

} //======================

19 /** Write the public void fillEvenSlots() method described in the

 * Looper class. The first slot filled should be the slot after the current position
 *     (if it exists). Call on fillOddSlots to do most of the work. */

public void fillEvenSlots() { this.moveOn();

   this.fillOddSlots();

} //======================

20 /** Write the seesOddsFilled method described in the Looper class. */ public boolean seesAllFilled() { String spot = this.getPosition(); // design step 1

   while (this.seesSlot() && this.seesCD()) // design step 2
   this.moveOn();
   boolean valueToReturn = ! this.seesSlot(); // design step 3
   this.backUpTo (spot); // design step 4
   return valueToReturn; // design step 5

} //======================

21 /** Write the seesEvensFilled method described in the Looper class. Call on seesOddsFilled to do most of the work. */ public boolean seeEvensFilled() { this.moveOn();

   this.seesOddsFilled();

}

22 /** Write an application program that tests out your solutions to the two preceding exercises by calling each one for the first sequence and printing a message saying what each returned. */ public class Sha_Ex3_22 {

   public static void main (String [ ] args)
   {   
       Looper a = new Looper();
       boolean firstResult = a.seesOddsFilled();
       boolean secondResult = a.seeEvensFilled();
       System.out.println (firstResult);
       System.out.println (secondResult);
   }

}

23 /** Write a Looper method public void bringBack(): The executor removes the CD in its current slot, if any, then brings each CD that is later in the sequence back one slot. Leave the position of the executor unchanged. */ public void bringBack() { String spot = this.getPosition();

   this.takeCD();
   while (this.seesSlot())
   this.moveOn();
   this.backUp();
   while (! this.getPosition() = spot)
   {
       this.takeCD();
       this.backUp();
       this.putCD();
   }

}

24 // public boolean hasNoSlotFilled() // Tell if no slots are filled. // public boolean hasSomeEmptySlot() // Tell if at least one slot is Empty.

25 /** Write a Looper method public void overOrOut(): The executor moves each CD in

 * its sequence of slots (a) to the following slot if the following slot exists and
 * is empty, or (b) to the stack if not. Leave the position of the executor unchanged. */

public void overOrOut() {

   String spot = this.getPosition();
   while (this.seesSlot())
   {   
       this.takeCD();
       this.moveOn();
       if (! seesCD())
       this.putCD();
   }
   this.backUpTo (spot);

}



Note to mods: If Chrisshaffer edits this page, don't revert it because I told him to do so. It's not vandalism

Start a discussion with Pyrotrees

Start a discussion