Question: How can i modify the toString in the linkedStack class so that it will print out my System. out .println(ptStack); Everytime i will print out

How can i modify the toString in the linkedStack class so that it will print out my System.out.println(ptStack); Everytime i will print out the output it will just print out one object on the stack instead of printing all of the objects. I will upvoteHow can i modify the toString in the linkedStack class so that

Code:

public class Painting {

private String artist;

private String genre;

private String era;

public Painting() {

}

public Painting(String artist, String genre, String era) {

this.artist = artist;

this.genre = genre;

this.era = era;

}

public String getArtist() { return artist;}

public String getGenre() {return genre;}

public String getEra() { return era;}

//mutators

public void setArtist(String artist) { this.artist = artist;}

public void setGenre(String genre) { this.genre = genre;}

public void setEra(String era) { this.era = era;}

public String toString() {

return("Artist: " + getArtist()

+ " Genre: " + getGenre() + " Era: " + getEra());

}

}

public final class LinearNode {

private T element;

private LinearNode next;

public LinearNode() {

element = null;

next = null;

}

public LinearNode(T element) {

this.element = element;

next = null;

}

public void setElement (T element) {

this.element = element;

}

public void setNext(LinearNode node) {

next = node;

}

public T getElement(){return element;}

public LinearNode getNext(){return next;}

public String toString() {

return " " + element;

}

}

package Exception;

public class StackException extends RuntimeException {

public StackException() {

super("stack is empty");

}

public StackException(String msg) {

super(msg);

}

}

public interface StackInterface {

public void push(T element);

public T pop() throws StackException;

public T peek() throws StackException;

public int size();

public boolean isEmpty();

public String toString();

}

public class LinkedStack implements StackInterface {

private LinearNode top;

private int count;

public LinkedStack() {

top = null;

count = 0;

}

public LinkedStack(T element) {

count = 1;

LinearNode node = new LinearNode(element);

if (count == 0) {

top = node;

}

else {

node.setNext(top);

top = node;

}

count++;

}

public void push(T element) {

LinearNode node = new LinearNode(element);

if (count == 0) {

top = node;

}

}

public T pop() throws StackException {

if (count == 0)

throw new StackException();

T temp = top.getElement();

top = top.getNext();

count--;

return temp;

}

public T peek() throws StackException {

if (isEmpty())

throw new StackException();

return top.getElement();

}

@Override

public int size() {

return count;

}

@Override

public boolean isEmpty() {

return count == 0;

}

public String toString() {

String result = "";

LinearNode current = top;

while (current != null) {

result = result +

(current.getElement()).toString() + " ";

current = current.getNext();

}

return result;

}

}

public class LinkedDriver {

public static void main(String[] args) {

StackInterface ptStack = new LinkedStack();

ptStack.push(new Painting("Vincent Van Gogh", "Sculpture" , "Modern"));

ptStack.push(new Painting("Pablo Picasso", "Portrait", "Middle ages"));

ptStack.push(new Painting("jackson Pollock", "History", "Modern"));

ptStack.push(new Painting("Rembrandt", "Landscape", "Renaissance"));

System.out.println(ptStack);

}

}

Problems Javadoc Declaration Console X LinkedDriver (Java Application] /Library/Java/JavavirtualMachines/jdk-13.0.2. Artist: Rembrandt Genre: Landscape Era: Renaissance

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!