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 upvote
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
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
next = node;
}
public T getElement(){return element;}
public LinearNode
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
private LinearNode
private int count;
public LinkedStack() {
top = null;
count = 0;
}
public LinkedStack(T element) {
count = 1;
LinearNode
if (count == 0) {
top = node;
}
else {
node.setNext(top);
top = node;
}
count++;
}
public void push(T element) {
LinearNode
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.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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
