(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this.
package driver;
import exception.StackException;
import stack.*;
public class StackDriver
{
public static void main(String[] args) throws Exception
{
StackInterface<Painting> painting = new LinkedStack <Painting>();
try
{
System.out.println("Peeking at top of player stack.\n" + painting.peek());
}catch (StackException e)
{
System.out.println(e);
System.out.println("Let's double check if it's empty: " + painting.isEmpty());
}
System.out.print("Printing out the size of the stack: " + painting.size());
System.out.println("\nCreating five paintings.");
Painting A = new Painting("Alice", "random" , "romance");
Painting B = new Painting("Byran", "random" , "romance");
Painting C = new Painting("Cathy", "random" , "romance");
Painting D = new Painting("Daniel", "random" , "romance");
Painting E = new Painting("Emily", "random" , "romance");
painting.push(A);
painting.push(B);
painting.push(C);
painting.push(D);
painting.push(E);
System.out.println("Testing the size method:\n" + painting.size());
System.out.println("Testing the peek method:\n" + painting.peek());
System.out.println("Testing the pop method:\n" + painting.pop());
System.out.println("Testing the peek method again to check if pop method workerd:\n"
+ painting.peek());
System.out.println("Testing the size method again:\n" + painting.size());
System.out.println("Testing the isEmpty method again:\n" + painting.isEmpty());
System.out.println("\nTesting stack method peek with Painting class methods:\n" +
painting.peek());
System.out.println("Peek at artist: " + painting.peek().getArtist());
System.out.println("Peek at genre: " + painting.peek().getGenre());
System.out.println("Peek at era: " + painting.peek().getEra());
System.out.println("Printing out the stack:\n" + painting);
System.out.println("Printing out the stack:\n" + painting);
}
}
package stack;
import exception.StackException;
/**
* Represents a linked implementation of a stack.
*
* @author Katareyna Guerra
*/
public class LinkedStack<T> implements StackInterface<T>
{
private int count;
private LinearNode<T> top;
/**
* Creates an empty stack.
*/
public LinkedStack()
{
count = 0;
top = null;
}
//creates the node
public LinkedStack(T element) {
count = 1;
LinearNode<T> node = new LinearNode<T>(element);
top = node;
}
/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed on stack
*/
public void push(T element)
{
LinearNode<T> node = new LinearNode<T>(element);
if(count != 0)
node.setNext(top);
top = node;
count++;
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element from top of stack
* @throws StackException if the stack is empty
*/
public T pop() throws StackException
{
if (isEmpty())
throw new StackException();
T temp = top.getElement();
top = top.getNext();
count--;
return temp;
}
/**
* Returns a reference to the element at the top of this stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws StackException if the stack is empty
*/
public T peek() throws StackException
{
if(isEmpty())
throw new StackException();
return top.getElement();
}
/**
* Returns true if this stack is empty and false otherwise.
* @return true if stack is empty
*/
public boolean isEmpty()
{
return count == 0;
}
/**
* Returns the number of elements in this stack.
* @return number of elements in the stack
*/
public int size()
{
return count;
}
/**
* Returns a string representation of this stack.
* @return string representation of the stack
*/
public String toString()
{
String result = "";
LinearNode<T> temp = top;
while(temp.getNext() != null){
result += temp.getElement();
temp = temp.getNext();
}
return result;
}
}
Ans:
Please rectify the condition inside the while loop of the toString() method.
Below is the updated code for the same:
public String toString(){ String result = ""; LinearNode<T> temp = top; while(temp != null){ result += temp.getElement(); temp = temp.getNext(); } return result; }
// After the above correction your code will be printing the last node as well.
// If you have any query do ask in the comments section.
// If you found the answer helpful do give a Thumbs Up!!
Get Answers For Free
Most questions answered within 1 hours.