Misplaced Pages

User:Pyrotrees: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 21:26, 13 April 2007 edit204.102.108.32 (talk)No edit summary← Previous edit Revision as of 21:49, 13 April 2007 edit undo204.102.108.32 (talk)No edit summaryNext edit →
Line 1: Line 1:
This is 1-16 This is 1-18
Change Sha_Ex2_(number) to Gua_Ex2_(number) Change Sha_Ex2_(number) to Gua_Ex2_(number)
Copy each into a .java file



public class Sha_Ex2_1 public class Sha_Ex2_1
Line 619: Line 619:
} }
} }
}
}
}

public class Sha_Ex2_17
{
// moves a CD from slot 3 to slot 2 of the
// first sequence and also from slot 2 to slot 3 of the second sequence.
public static void main (String args)
{
SmartVic sue;
sue = new SmartVic();
sue.moveOn();
sue.moveTake();
sue.backPut();
SmartVic sam;
sam = new SmartVic();
if(sam.seesSlot())
{ sam.moveTake();
sam.moveOn();
sam.putCD();
}
}
}

public class Sha_Ex2_18
{
// takes the CD out of the second slot of
// each of the first four sequences
public static void main (String args)
{
SmartVic sue;
sue = new SmartVic();
sue.moveTake();
SmartVic sam;
sam = new SmartVic();
if(sam.seesSlot())
{ sam.moveTake();
}
SmartVic sue2;
sue2 = new SmartVic();
if(sue2.seesSlot())
{ sue2.moveTake();
}
SmartVic sam2;
sam2 = new SmartVic();
if(sam2.seesSlot())
{ sam2.moveTake();
} }
} }

Revision as of 21:49, 13 April 2007

This is 1-18 Change Sha_Ex2_(number) to Gua_Ex2_(number) Copy each into a .java file

public class Sha_Ex2_1 { // moves a CD out of its third slot into its first slot

public static void main (String args) { Vic.reset (args); Vic sue; sue = new Vic(); sue.moveOn(); sue.moveOn(); sue.takeCD(); sue.backUp(); sue.backUp(); sue.putCD(); }

}

public class Sha_Ex2_2 { // creates a Vic object and then puts a CD in each of its first three slots public static void main (String args) { Vic.reset (args); Vic sue; sue = new Vic(); sue.putCD(); sue.moveOn(); sue.putCD(); sue.moveOn(); sue.putCD(); } }

public class Sha_Ex2_3 { // creates a Vic object and then takes a CD // out of its second slot and its fourth slot public static void main (String args) { Vic.reset (args); Vic sue; sue = new Vic(); sue.moveOn(); sue.takeCD(); sue.moveOn(); sue.moveOn(); sue.takeCD(); } }

public class Sha_Ex2_4 { // creates a Vic object and then takes a CD from each // of its second and fourth slots and puts one of them in its fifth slot public static void main (String args) { Vic.reset (args); Vic sue; sue = new Vic(); sue.moveOn(); sue.takeCD(); sue.moveOn(); sue.moveOn(); sue.takeCD(); sue.moveOn(); sue.putCD(); } }

public class Sha_Ex2_5 { // creates a Vic object and then swaps the // CD in its third slot with the CD in its second slot public static void main (String args) { Vic.reset (args); Vic sue; sue = new Vic(); sue.moveOn(); sue.takeCD(); sue.moveOn(); sue.takeCD(); sue.backUp(); sue.putCD(); sue.moveOn(); sue.putCD(); }

}

public class Sha_Ex2_6 { // moves a CD out of its third slot into its first slot

public static void main (String args) { SmartVic.reset (args); SmartVic sue; sue = new SmartVic(); sue.moveOn(); sue.moveTake(); sue.backUp(); sue.backPut(); }

}

public class Sha_Ex2_7 { // creates a SmartVic object and then takes a CD // out of its second slot and its fourth slot public static void main (String args) { SmartVic.reset (args); SmartVic sue; sue = new SmartVic(); sue.moveTake(); sue.moveOn(); sue.moveTake(); } }


Ex2_8 public class SmartVic extends Vic { public void moveTake() { moveOn(); takeCD(); } //====================== public void backPut() { backUp(); putCD(); } //====================== public void movePut() { moveOn(); putCD(); } }

public class Sha_Ex2_9 { // moves a CD from the third slot to the fifth slot public static void main (String args) { SmartVic sue; sue = new SmartVic(); sue.moveOn(); sue.moveTake(); sue.moveOn(); sue.movePut(); } }

public class SmartVic extends Vic { public void moveTake() { moveOn(); takeCD(); } //====================== public void backPut() { backUp(); putCD(); } //====================== public void movePut() { moveOn(); putCD(); } //====================== public void backTake() { backUp(); takeCD(); } }

public class Sha_Ex2_11 { // swaps the CD in the second slot with the CD in the first slot public static void main (String args) { SmartVic sue; sue = new SmartVic(); sue.moveTake(); sue.backPut(); }

}

public class Sha_Ex2_12 { // to move the CD in the first slot into the second, // the CD in the second slot into the third, // and the CD in the third slot into the first. public static void main (String args) { SmartVic sue; sue = new SmartVic(); sue.takeCD(); sue.moveTake(); sue.moveTake(); sue.backUp(); sue.backPut(); sue.moveOn(); sue.movePut(); sue.backPut(); } }

public class Sha_Ex2_13 { // 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.moveOn(); if (sue.seesSlot()) { sue.takeCD(); sue.backPut(); } } //====================== }

public class Sha_Ex2_14 { // creates a Vic and then has it take a CD from each of its fourth and fifth slots public static void main (String args) { Vic sue; sue = new Vic(); // creates a new Vic sue.moveOn(); // moves to the second slot sue.moveOn(); // moves to the third slot sue.moveOn(); // moves to the fourth slot if(sue.seesSlot()) { sue.takeCD(); sue.moveOn(); // moves to the fifth slot if(sue.seesSlot()) { sue.takeCD(); } } } }

Ex2_15

//

// Copy this file in its entirety to a file named Vic.java
// Compile it before trying to compile any program that uses it
// This implementation uses ArrayLists for the sequences of CDs
// It also uses an initializer method to create the tableau before any Vics
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Vic
{
	private static Stack theStack = new Stack();  // where spare CDs are kept
	private ArrayList itsSequence;  // its slots
	private int itsPos;             // 0,1,2,...; 0 is at front
	private int itsID;              // 1 for first Vic, 2 for...
/* QUERY METHODS */
	/** Return the current position as a String value.  */
	public String getPosition()
	{	return itsID + ", " + itsPos;
	}	//======================
	/** Tell whether there is a slot at its position.  */
	public boolean seesSlot()
	{	return itsPos < itsSequence.size();
	}	//======================
	/** Tell whether there is a CD in its current slot.  */
	public boolean seesCD()
	{	if (! seesSlot())
			fail ("Can't see a CD where there is no slot!");
		return itsSequence.get (itsPos) != null;
	}	//======================
	/** Return the CD that is in its current slot.  */
	public String getCD()
	{	if (! seesSlot())
			fail ("There is no slot to get a CD from!");
		return (String) itsSequence.get (itsPos);
	}	//======================
	/** Tell whether the stack has any CDs available. */
	public static boolean stackHasCD()
	{	return ! theStack.isEmpty();
	}	//======================
/* ACTION METHODS */
	/** Move forward to the next slot in the sequence. */
	public void moveOn()
	{	if (! seesSlot())
			fail ("Already at the end of the sequence!");
		itsPos++;
		trace ("moveOn to slot " + (itsPos + 1));
	}	//======================
	/** Back up to the previous slot in the sequence. */
	public void backUp()
	{	if (itsPos == 0)
			fail ("Already at the front of the sequence!");
		itsPos--;
		trace ("backUp to slot " + (itsPos + 1));
	}	//======================
	/** Move a CD from the stack to the current slot.  */
	public void putCD()
	{	if (! seesCD() && stackHasCD())
			itsSequence.set (itsPos, theStack.pop());
		trace ("putCD at slot " + (itsPos + 1));
	}	//======================
	/** Move a CD from the current slot to the stack.  */
	public void takeCD()
	{	if (seesCD())
		{	theStack.push (itsSequence.get (itsPos));
			itsSequence.set (itsPos, null);
		}
		trace ("takeCD at slot " + (itsPos + 1));
	}	//======================
	/** Terminate the program with an appropriate message.  */
	private void fail (String cause)
	{	JOptionPane.showMessageDialog (null, "STOPPING: " + cause
			      + "  (Vic #)" + itsID + ", position =" + itsPos);
		System.exit (0);
	}	//======================
	/** Two convenience methods */
	public void shiftFromSlotToStack()
	{	takeCD();
	}	//======================
	public void shiftFromStackToSlot()
	{	putCD();
	}	//======================
	/** Swaps the CD in the current slot with the CD in the following slot, but it
	moves no CD at all if either CD is missing. */
	public void swapTwo()
	{	if (seesCD())
		{	takeCD();
			moveOn();
			if(seesSlot())
			{	if(seesCD())
				{	takeCD();
					backUp();
					putCD();
					moveOn();
					putCD();
				}
			}
		}
	}
/* METHODS THAT USE THE FRAME */
	private static String vicSay = "Programmable CD Organizer "
	                             + "        mfd by Jones & Co.";
	private static final VicFrame theFrame = new VicFrame();
	//////////////////////////////////
	public static void say (String message)
	{	vicSay = message;
		theFrame.repaint();
	}	//======================
	/** Print a trace of the Vic's action.  */
	private void trace (String message)
	{	System.out.println (message + " for Vic #" + itsID);
		theFrame.repaint();
		pause (500);  // half-a-second between actions
	}	//======================
	/** Pause for the specified number of milliseconds.  */
	private static void pause (int milliseconds)
	{	try
		{	Thread.sleep (milliseconds);
		}
		catch (InterruptedException e)
		{	// never happens
		}
	}	//======================
/* THE INITIALIZER AND CONSTRUCTOR METHODS */
	private static final int MAXSLOTS = 8;
	private static final int MINSLOTS = 3;
	private static final int MAXVICS  = 4;
	private static final Random random = new Random();
	private static int theMaxVics = random.nextInt (MAXVICS) + 1;
	private static ArrayList theSeq = new ArrayList;
	private static int theNumVics = 0;
	private static final Vic theVics = {null, null, null, null};
	//////////////////////////////////
	/** Initialize individual sequences and stacks.  An initializer method
	    is used because these have to exist before any Vics are created. */
	static
	{	for (int k = 0;  k < theMaxVics;  k++)
		{	theSeq = new ArrayList();
			int numSlots = random.nextInt (MAXSLOTS - MINSLOTS + 1)
					                   + MINSLOTS;
			for (int i = 0;  i < numSlots;  i++)
			{	String it = random.nextInt (2) == 0  ?  null 
				            : "" + (char) (i + 'a') + (k + 1);
				theSeq.add (it);
			}
		}
		// start with up to 2 CDs on the stack
		if (random.nextInt (3) > 0)  // 2 times out of 3
		{  theStack.push ("GarthB");
			if (random.nextInt (2) == 0) // 1 time out of 3
				theStack.push ("LyleL");
		}
	}	//======================
	/** Construct a new Vic. */
	public Vic()
	{	super();
		itsSequence =  theNumVics < theMaxVics
				?   theSeq   :  new ArrayList();
		itsPos = 0;
		itsID = theNumVics + 1;
		theVics = this;
		theNumVics++;
		trace ("construction");
	}	//======================
	/** Replace random arrangement by user-specified arrangement. */
	public static void reset (String args)
	{	if (args.length > 0 && theNumVics == 0)
		{	theMaxVics = Math.min (args.length, MAXVICS);
			theSeq = new ArrayList;
			for (int k = 0; k < theMaxVics;  k++)
			{	theSeq = new ArrayList();
				int longest = Math.min (args.length(), MAXSLOTS);
				for (int i = 0;  i < longest;  i++)
				{	String it = args.charAt (i) == '0' ? null 
					          : "" + (char)(i + 'a') + (k + 1);
					theSeq.add (it);
				}
			}
		}
	}	//======================
// THE NESTED FRAME CLASS
	static class VicFrame extends JFrame
	{	private final int SLOT = 75;           // between CD slots
		private final int EDGE = 10;           // leave free at left side
		private final int WIDTH = (MAXSLOTS + 2) * SLOT + 2 * EDGE;
		private final int DIST = 60;           // between CD sequences
		private final int SAY = 45;            // depth of say's output
		private final int TOPSEQ = SAY + DIST; // depth of first seq
		public VicFrame()
		{	addWindowListener (new Closer());
			setSize (WIDTH, TOPSEQ + MAXVICS * DIST + 2 * EDGE);
			setBackground (new Color (255, 252, 224)); // a nice cream
	 		setVisible (true);    // make it visible to user
		}	//======================
		/** Same as for an applet; called by repaint. */
		public void paint (Graphics page)  
		{	// PRINT THE vicSay MESSAGE AT THE TOP
			page.setColor (getBackground());
			page.fillRect (EDGE, EDGE, WIDTH - 2 * EDGE, 
						       TOPSEQ + MAXVICS * DIST);
			page.setColor (Color.white);
			page.fillRect (20, SAY - 20, WIDTH - 40, 20);
			page.setColor (new Color (0, 96, 0));  // a light green
			page.drawString (vicSay, 25, SAY - 5); // message
			// DRAW UP TO FOUR Vic SEQUENCES AND THE STACK
			for (int k = 0;  k < theMaxVics;  k++)
				drawSequence (page, k, TOPSEQ + k * DIST);
			page.setColor (Color.red);
			int y = TOPSEQ + MAXVICS * DIST;
			page.drawString ("stack", EDGE, y);
			page.fillRect (EDGE, y - 25, 40, 5);  // dividing line
			for (int k = 0;  k < theStack.size();  k++)
				page.drawString ((String) theStack.get (k),
					            EDGE, y - 30 - k * 20);
		}	//======================
		/** Called by VicFrame's paint method. */
		private void drawSequence (Graphics page, int index, int y)
		{	page.setColor (Color.red);
			if (theVics != null)
				drawMacMan (page, theVics.itsPos, y - 15);
			page.setColor (Color.blue);
			drawAllCDs (page, y, theSeq); 
		}	//======================
		private void drawAllCDs (Graphics page, int y,
	      	                   ArrayList slots)
		{	int atEnd = slots.size();
			for (int n = 0;  n < atEnd;  n++)
			{	String it = (String) slots.get (n);
				page.drawString (it == null ? "---" : it, 
						           (n + 1) * SLOT + EDGE, y);
			}
			page.drawString ("END", (atEnd + 1) * SLOT + EDGE, y);
		}	//======================
		private void drawMacMan (Graphics page, int pos, int y)
		{	// <x, y> is the lower-left corner of the stick figure
			int x = pos * SLOT + EDGE + 78;
			page.setColor (Color.black);
			page.drawLine (x,     y,      x + 6,  y - 6);  // leg
			page.drawLine (x + 6, y - 6,  x + 12, y);      // leg
			page.drawLine (x + 6, y - 6,  x + 6,  y - 18); // body
			page.drawLine (x,     y - 14, x + 12, y - 14); // arms
			page.drawOval (x + 1, y - 28, 10,     10);     // head
			page.drawLine (x + 4, y - 25, x + 5,  y - 25); // eye
			page.drawLine (x + 7, y - 25, x + 8,  y - 25); // eye
			page.drawLine (x + 3, y - 22, x + 9,  y - 22); // mouth
		}	//======================
	} // end of VicFrame class
	private static class Closer extends java.awt.event.WindowAdapter 
	{	
		public void windowClosing (java.awt.event.WindowEvent e) 
		{	System.exit (0);
		}	//======================
	}     
}     
// 

public class Sha_Ex2_16 { /** creates a Vic and then has it take a CD out of its fifth slot and put it into its seventh slot. */ public static void main (String args) { Vic sue; sue = new Vic(); sue.moveOn(); sue.moveOn(); sue.moveOn(); if(sue.seesSlot()) { sue.moveOn(); if(sue.seesSlot()) { sue.takeCD(); sue.moveOn(); if(sue.seesSlot()) { sue.moveOn(); if(sue.seesSlot()) sue.putCD(); } } } } }

public class Sha_Ex2_17 { // moves a CD from slot 3 to slot 2 of the // first sequence and also from slot 2 to slot 3 of the second sequence. public static void main (String args) { SmartVic sue; sue = new SmartVic(); sue.moveOn(); sue.moveTake(); sue.backPut();

SmartVic sam; sam = new SmartVic(); if(sam.seesSlot()) { sam.moveTake(); sam.moveOn(); sam.putCD(); } } }

public class Sha_Ex2_18 { // takes the CD out of the second slot of // each of the first four sequences public static void main (String args) { SmartVic sue; sue = new SmartVic(); sue.moveTake();

SmartVic sam; sam = new SmartVic(); if(sam.seesSlot()) { sam.moveTake(); }

SmartVic sue2; sue2 = new SmartVic(); if(sue2.seesSlot()) { sue2.moveTake(); }

SmartVic sam2; sam2 = new SmartVic(); if(sam2.seesSlot()) { sam2.moveTake(); } } }