Question
How can i modify the linkedStack class so that it will print out my System. out .println(ptStack); Everytime i will print out the output it
How can i modify the linkedStack class so that it will print out my System.out.println(ptStack); Everytime i will print out the output it will say null instead of the painting toString method.
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;
private T[] stack;
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 {
return null;
}
@Override
public int size() {
return count;
}
@Override
public boolean isEmpty() {
return count == 0;
}
public String toString() {
return null;
}
}
public class LinkedDriver {
public static void main(String[] args) {
StackInterface
Painting paint = new Painting("Vincent Van Gogh", "Sculpture" , "Modern");
ptStack.push(paint);
paint = new Painting("Pablo Picasso", "Portrait", "Middle ages");
ptStack.push(paint);
paint = new Painting("Rembrandt", "Landscape", "Renaissance");
ptStack.push(paint);
paint = new Painting("Jackson Pollock", "History", "Modern");
ptStack.push(paint);
System.out.println(ptStack);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started