Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 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;

private T[] stack;

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 {

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 ptStack = new LinkedStack();

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual C# An Introduction to Object-Oriented Programming

Authors: Joyce Farrell

7th edition

978-1337102100

More Books

Students also viewed these Accounting questions

Question

OUTCOME 3 Describe pay equity and strategies for implementing it.

Answered: 1 week ago