net.pacbell.jfai.toh.domain
Class Pin

java.lang.Object
  extended by net.pacbell.jfai.toh.domain.Pin

public class Pin
extends Object

A pin holds a stack of disks. It has a name, such as 'left'.


Constructor Summary
Pin(String name)
          Creates a named pin.
 
Method Summary
 void add(Disk disk)
          Insert the given disk into the tower at the appropriate position according to the disk size.
 void addTop(Disk disk)
          Put the given disk on top of the existing tower.
 int diskLocation(Disk disk)
          Answer the 1-based position of the given disk in the receiver's tower.
 Stack<Disk> getDisks()
          Gets the disks.
 String getName()
          Gets the name.
 int getNumberOfDisks()
          Gets the total number of disks at this pin.
 boolean isAcceptable(Disk probe)
          Answer if the argument can be placed on top of the existing tower.
 boolean remove(Disk disk)
          If the specified disk is at this pin, then remove it and answer true, else answer false.
 Disk removeTop()
          Remove the topmost disk from the tower and answer it.
 void setName(String name)
          Sets the name.
 Disk top()
          Answer the topmost disk.
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Pin

public Pin(String name)
Creates a named pin.

Parameters:
name - the name, such as 'left'
Method Detail

add

public void add(Disk disk)
Insert the given disk into the tower at the appropriate position according to the disk size. The largest disk of the tower is the first element in the list, and the smallest disk of the tower is the last element.

Parameters:
disk - the disk to be added

addTop

public void addTop(Disk disk)
            throws InvalidMoveException
Put the given disk on top of the existing tower. If this constitutes an invalid move, an Exception is thrown.

Parameters:
disk - The disk to be added
Throws:
InvalidMoveException - if an invalid move is attempted

diskLocation

public int diskLocation(Disk disk)
Answer the 1-based position of the given disk in the receiver's tower. The top of the tower is position 1. If the disk isn't found, answer -1.

Parameters:
disk - a disk
Returns:
position of the given disk - the first position is 1

getDisks

public Stack<Disk> getDisks()
Gets the disks.

Returns:
all the disks

getName

public String getName()
Gets the name.

Returns:
the pin's name, e.g. 'left'

getNumberOfDisks

public int getNumberOfDisks()
Gets the total number of disks at this pin.

Returns:
the total number of disks

isAcceptable

public boolean isAcceptable(Disk probe)
Answer if the argument can be placed on top of the existing tower. Answer true if the stack is empty or if the given disk is smaller than the topmost disk.

Parameters:
probe - A disk
Returns:
can the given disk be placed at the top of the tower?

remove

public boolean remove(Disk disk)
If the specified disk is at this pin, then remove it and answer true, else answer false.

Parameters:
disk - A disk
Returns:
Was the given disk removed?

removeTop

public Disk removeTop()
Remove the topmost disk from the tower and answer it. If there is no disk to remove, an exception is thrown.

Returns:
the removed disk

setName

public void setName(String name)
Sets the name.

Parameters:
name - the name of the pin, e.g. 'left'

top

public Disk top()
Answer the topmost disk. If there is none an exception is thrown.

Returns:
the disk at the top of the tower

toString

public String toString()

Overrides:
toString in class Object
See Also:
Object.toString()