Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I\'ve created a CharacterStack.java because for my assignment we are not allo

ID: 3761295 • Letter: S

Question

So I've created a CharacterStack.java because for my assignment we are not allowed to use generics.

I have posted the code so that you can view my methods here

http://paste.ofcode.org/EbX75z7rp7KGNc4eYBU96m

(Please correct anything here that you see wrong I'm fairly new)

What I'm trying to do is lets say a user inputs "MXI"

So the stack will enter [ M | X | I ]

Now I want to pop these 3 values and store them in a single string called variable1 if that's possible.

But I also don't want these variables store so that when it prints it is IXM, I need it to print as MXI because I need to later convert the entirety of variable1 from Roman Numerals to Integers

or if it's possible is there anyway to add the letters into the stack backwards? So that the stack will read [ I | X | M ] ?

Can someone please help me with this I've hit a stump.

Explanation / Answer

I had a look at your code and you can use queue in this case.

It will be better as stack is LIFO and the order of pusing and poping is different.

Queue is FIFO based so order will be maintained.

if you want to use the same code written then we can write a new function which can reverse the contents of the stack in java.

this java can be used to reverse the stack.

public void insertback(String item)
{
if (topOfStack >= 0)
push(item);
else
{
int temp = pop();
insertback(item);
push(temp);
}
}  
   public void reverse()
{
if (topOfStack >= 0)
{
int temp = pop();
reverse();
insertback(temp);
}
}
  

I will prefer to use queues in this case.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote