/* * Laura Toma * csci 210 */ //a class to represent Pegs in the Hanoi puzzle. A peg knows its name, //its size and the array of elements representing the sizes of the //disks on the peg. One can add and delete the last element of a peg. public class Peg { int n; //the current size String name; //the name int[] data; //an array of integers representing the sizes of disks. public Peg(int n, String name) { //allocate data data = new int[n]; this.name = name; this.n = 0; //no elements yet } public String getName() {return name;} public int getSize() {return n;} public boolean isEmpty() { return (getSize() == 0); } public int getLast() { if (getSize()==0) { //peg is empty System.out.println("cannot getLast"); System.exit(1); } return data[getSize()-1]; } //add value k as the last element in this peg public void addLast(int k) { if (n == data.length) { //peg is full System.out.println("addLast: peg " + name + " is full "); System.exit(1); } //else data[n] = k; n++; } //delete the last value in this peg public int deleteLast() { if (n ==0) { System.out.println("deleteLast: peg " + name + " is empty"); System.exit(1); } int last = data[n-1]; n--; return last; } //print the contents of this peg public void print() { System.out.print(getName()+ ": "); for (int i=0; i