Question

I need help finishing one of four classes for a TowerOfHanoi project. The whole project should...

I need help finishing one of four classes for a TowerOfHanoi project. The whole project should have a Disk class, a Pole class, a TowerOfHanoi class, and a TowerOfHanoi main method. So far, I have finished the Disk class, and I need help completing the Pole class.

1. I need help figuring out how to create a toString() method within that class that will return a text-based 2D graphic of a pole and any existing disks on it. For example:

                |
                |
             **|**
           ***|***
         ****|****
   =============

I'll post my code with the particular section I'm having trouble with in bold. Here's the my Pole class so far:

public class Pole
{
   private Disk[] disks;
   private int numberOfDisks;
   private int maxNumberOfDisks;
   private int maxDiskSize;
   private char poleChar;
  
   // Constructors:
   public Pole (int aMaxNumberOfDisk, int aMaxDiskSize, char aPoleChar)
   {
       if (aMaxNumberOfDisk < 1)
       {
           maxNumberOfDisks = 1;
       }
       else
       {
           maxNumberOfDisks = aMaxNumberOfDisk;
       }
       numberOfDisks = 0;
       maxDiskSize = aMaxDiskSize;
       poleChar = aPoleChar;
       disks = new Disk[maxNumberOfDisks];
   }
  
   public Pole (int aMaxNumberOfDisk, int aMaxDiskSize)
   {
       numberOfDisks = 0;
       maxNumberOfDisks = aMaxNumberOfDisk;
       maxDiskSize = aMaxDiskSize;
       disks = new Disk[maxNumberOfDisks];
   }
  
   public Pole (int aMaxNumberOfDisk)
   {
       numberOfDisks = 0;
       maxNumberOfDisks = aMaxNumberOfDisk;
       poleChar = '|';
       maxDiskSize = aMaxNumberOfDisk;
       disks = new Disk[maxNumberOfDisks];
   }
// --------------------------------------------------------------------------------------------------
   // Accessors:
   public int getMaxNumberOfDisks()
   {
       return maxNumberOfDisks;
   }
  
   public int getMaxDiskSize()
   {
       return maxDiskSize;
   }
  
   public int getNumberOfDisks()
   {
       return numberOfDisks;
   }
  
   public Disk peekTopDisk()
   {
       for (int i = disks.length - 1; i >= 0; i--)
       {
           if (disks[i] != null)
           {
               return disks[i];
           }
       }
      
       return null;
   }
  
   public String toString() // This is the method I'm having trouble with.
   {
      
   }

  
   // Mutators:
   public boolean addDisk(Disk aDisk)
   {
       if (aDisk.getSize() > maxDiskSize)
       {
           return false;
       }
       for (int i = 0; i < disks.length; i++)
       {
           if (disks[i] == null)
           {
               disks[i] = aDisk;
               numberOfDisks++;
               return true;
           }
       }
       System.out.println("Couldn't add (anymore) disks");
       return false;
   }
  
   public Disk removeDisk()
   {
       Disk myTopDisk = peekTopDisk();
      
       for (int i = (disks.length- 1); i >= 0; i--)
       {
           if (disks[i] != null)
           {
               disks[i] = null;
               numberOfDisks --;
               return myTopDisk;
           }
       }
       return null;
   }
}

Would really appreciate some help so I can move on with this project. Thank you! :)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Although the question mentions nothing clearly about what each function is meant for doing

I have a maintained tower of hanoi program prepared hope this is of any help to solve your problem

#include<iostream>
#define N 4
using namespace std;
class tower
{
int d[N],top;
char c[10];
public:
   tower()       //constructor of tower
   {
       cout<<"Enter name of the tower: "; //takes name of the tower as input
       cin>>c;
       for(int l=0;l<N;l++)
           d[l]=0;
       top=-1;
   }
   tower(int z)       //another constructor(parameterized)
   {
       cout<<"Enter name of the tower: ";
       cin>>c;
       for(int k=0;k<z;k++)
       {
       cout<<"Enter "<<k<<"th element";//taking input values
       cin>>d[k];
       }
       top=z-1;
   }

int pop()       //pop function from tower
{
int x;
if(top==-1)
cout<<"\n Tower Empty";
else
x=d[top--];
return(x);
}
void push(int n)       //push function from tower
{
   bool i=false;
   if(top==N)
   cout<<"Tower full!!";
   else if((n>d[top] )&& (top!=-1))
       cout<<"Large element cannot push\n";
   else
       {
       i=true;
       d[++top]=n;
       }  
}
void show()           //printing the tower
{
   cout<<"\t\t\t\t"<<c<<endl;
   for(int m=top;m>=0;m--)
   {cout<<"\t\t\t";
   for(int n=0;n<d[m];n++)
       cout<<"*";
   cout<<d[m]<<endl;
   }
}
void friend move(int n,tower &s, tower &d, tower &a); //function to move each disk
};  
  
   void move(int n,tower &s,tower &d,tower &a)
   {
       int t;
       if(n>1)
       {
           move(n-1,s,a,d);
           t=s.pop();
           d.push(t);
           move(n-1,a,d,s);
       }
       else
       {
           t=s.pop();
           d.push(t);
       }
   }
int main()       //driver method
{
   int size;
   cout<<"Enter no of disks: ";
   cin>>size;
   tower s(size),d,a;
   cout<<"\n\n\t\t\tInitial state:\n\n";
   s.show();
   a.show();
   d.show();
   move(size,s,d,a);
   cout<<"\n\n\t\t\tFinal state: \n\n\n";
   s.show();
   d.show();
   a.show();
   return 0;

  
}

For any problem refer to the comment section

Thank you

Add a comment
Know the answer?
Add Answer to:
I need help finishing one of four classes for a TowerOfHanoi project. The whole project should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal....

    Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "bcaahttaetersse" 4. String outputDepthFirstSearch(): returns a string represenng a pre order depth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "batcathateersse This is my code so far public class Trie { final TrieNode root; public Trie() { this.root...

  • I need help to write a test case. 1. Go to the ConcertTicketTest.java file and write...

    I need help to write a test case. 1. Go to the ConcertTicketTest.java file and write test cases for the compareTo method you just implemented. /* ConcertTicket.java file */ package SearchingSorting; /** * * @author clatulip */ public class ConcertTicket implements Comparable<ConcertTicket> { private String name; private int price; private char row; private int seat; public ConcertTicket(String name, int price) { this.name = name; this.price = price; // randomly create a row and seat // assumes 60 seats across width...

  • Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add...

    Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add an instance method deleteFront such that … Method deleteFront has no input. Method deleteFront returns … an empty Optional instance if the list is empty an Optional instance whose value is the integer that was deleted from the front of the list, otherwise Hints https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html import java.util.Optional; public class IntegerLinkedList { private IntegerNode head ; private int numberOfItems ; public int getNumberOfItems() { return...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

    Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add​(Event evt) Add an event to the calendar Event get​(int i) Fetch the ith Event added to the calendar Event get​(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

  • please help with program,these are he requirements: I am also getting a "missing return statement" in...

    please help with program,these are he requirements: I am also getting a "missing return statement" in my milestone class Create a new class Milestone which contains at least an event description, a planned completion date, and an actual completion date (which will be unset when the milestone is created). There should be a method to changed the planned completion date as well as a method to designate the Milestone as achieved. Add a method that returns whether the milestone has...

  • I need help fixing my java code for some reason it will not let me run...

    I need help fixing my java code for some reason it will not let me run it can someone pls help me fix it. class LinearProbingHashTable1 { private int keyname; private int valuename; LinearProbingHashTable1(int keyname, int valuename) { this.keyname = keyname; this.valuename = valuename; } public int getKey() { return keyname; } public int getValue() { return valuename; } } class LinearProbingHashTable2 { private final static int SIZE = 128; LinearProbingHashTable2[] table; LinearProbingHashTable2() { table = new LinearProbingHashTable2[SIZE]; for (int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT