Question: ======================================== Here is the code I have written for Disk Class public class Disk implements Comparable { private int size; // 1-9: restricts disk movement,

 ======================================== Here is the code I have written for Disk Classpublic class Disk implements Comparable { private int size; // 1-9: restrictsdisk movement, and used for drawing /** * Constructs a new immutable

========================================

Here is the code I have written for Disk Class

public class Disk implements Comparable {

private int size; // 1-9: restricts disk movement, and used for drawing

/**

* Constructs a new immutable disk object with the specified size.

* @param size is used for drawing and comparing against other disks.

* @throws IllegalArgumentException when size is not between 1 and 9.

*/

public Disk(int size) throws IllegalArgumentException {

this.size = size;

}

/**

* Compares one disk to another to determine which is larger, and therefore

* which can be moved on top of the other.

* @param other is a reference to the disk we are comparing this one to.

* @return a positive number when this.size > other.size,

* a negative number when this.size

* zero when this.size == other.size, or other is null.

*/

@Override

public int compareTo(Disk other) {

if (size > other.size) {

// move the disk to upper

return 1;

}

else if (size

return -1;

}

else if ( size == other.size) {

return 0;

}

return 0;

}

/**

* The string representation of this disk object includes its integer size

* surrounded by size-1 equals characters (=) on each side, and enclosed

* within angle brackets (). For example:

* size 1: ""

* size 2: ""

* size 3: ""

* @return the string representation of this disk object based on its size.

*/

@Override

public String toString() {

String a = "" ;

if (size == 1) {

System.out.println("");

}

else if (size > 1) {

for (int i = 0; i

a = a + "=";

}

}

//System.out.println(a);

String rslt = "";

//System.out.println(rslt);

//return rslt;

return rslt;

}

}

========================================

THE ROD 2. As described in the wikipedia page, the Tower of Hanoi puzzle makes use of three rods. Add a new class called Rod which implement the Comparable interface to your project. This Rod class will implement many of the common StackADT operations, and will make use of a array-based stack implementation (as discussed in lecture). This class must contain exactly the private fields and publicly accessible constructor/methods shown in the following 1 private int numberOfDisks; // tracks the number of disks on this rod 2 private Disk[] disks; // stores references to the disks on this rod // index 0: bottom, index disCNumber-1: top 4 6Constructs a new rod that can hold a maximum of maxHeight Disks. The 7numberOfDisks new Disks will be created with sizes between 1 and 8numberOfDisks Cinclusive), and arranged from largest Con bottom) to the smallest Con top) on this Rod 10 eparam maxHeight is the capacity or max number of Disks a rod can hold 11 *param numberOfDiscs is the initial number of Disks created on this rod 12 * 13 public RodCint maxHeight, int numberOfDisks) 14 15** 16 Adds one new Disk to the top of this rod 17param disk is a reference to the Disk being added to this rod 18 *ethrows IllegalStateException when this rod is already full to capacity 19* 20 public void push(Disk disk) throws IllegalStateException 21 24ereturn a reference to the Disk that is being removed 26 28 23Removes and returns one Disk from the top of this rod 25ethrows NoSuchElementException when this rod is empty 27 public Disk pop) throws NoSuchElementException return null; 1 29** 30Returns (without removing) one Disk from the top of this rod 31*ereturn a reference to the Disk that is being returned 32ethrows NoSuchElementException when this rod is empty 34 public Disk peek) throws NoSuchElementException return null; 35 36** 37 *Indicates whether this rod is currently holding zero Disks 38 @return true when there are no Disks on this rod 39 40 public boolean isEmpty) return false; 41 42** 43Indicates whether this rod is currently full to its capacity with disks THE ROD 2. As described in the wikipedia page, the Tower of Hanoi puzzle makes use of three rods. Add a new class called Rod which implement the Comparable interface to your project. This Rod class will implement many of the common StackADT operations, and will make use of a array-based stack implementation (as discussed in lecture). This class must contain exactly the private fields and publicly accessible constructor/methods shown in the following 1 private int numberOfDisks; // tracks the number of disks on this rod 2 private Disk[] disks; // stores references to the disks on this rod // index 0: bottom, index disCNumber-1: top 4 6Constructs a new rod that can hold a maximum of maxHeight Disks. The 7numberOfDisks new Disks will be created with sizes between 1 and 8numberOfDisks Cinclusive), and arranged from largest Con bottom) to the smallest Con top) on this Rod 10 eparam maxHeight is the capacity or max number of Disks a rod can hold 11 *param numberOfDiscs is the initial number of Disks created on this rod 12 * 13 public RodCint maxHeight, int numberOfDisks) 14 15** 16 Adds one new Disk to the top of this rod 17param disk is a reference to the Disk being added to this rod 18 *ethrows IllegalStateException when this rod is already full to capacity 19* 20 public void push(Disk disk) throws IllegalStateException 21 24ereturn a reference to the Disk that is being removed 26 28 23Removes and returns one Disk from the top of this rod 25ethrows NoSuchElementException when this rod is empty 27 public Disk pop) throws NoSuchElementException return null; 1 29** 30Returns (without removing) one Disk from the top of this rod 31*ereturn a reference to the Disk that is being returned 32ethrows NoSuchElementException when this rod is empty 34 public Disk peek) throws NoSuchElementException return null; 35 36** 37 *Indicates whether this rod is currently holding zero Disks 38 @return true when there are no Disks on this rod 39 40 public boolean isEmpty) return false; 41 42** 43Indicates whether this rod is currently full to its capacity with disks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!