Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How come the top artist is the bottom object. Shouldnt the top artist be Vincent van gogh ? linkedStack class: public class LinkedStack implements StackInterface

How come the top artist is the bottom object. Shouldnt the top artist be "Vincent van gogh" ? image text in transcribedlinkedStack class:

public class LinkedStack implements StackInterface {

private LinearNode top;

private int count;

private T[] stack;

public LinkedStack() {

top = null;

count = 0;

}

public LinkedStack(T element) {

top = new LinearNode(element);

count = 1;

}

public void push(T element) {

// creating a new node, positioning before current top, setting as new

// top and updating count

LinearNode node = new LinearNode(element);

node.setNext(top);

top = node;

count++;

}

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 (count == 0)

throw new StackException();

//returning top element

return top.getElement();

}

@Override

public int size() {

return count;

}

@Override

public boolean isEmpty() {

return count == 0;

}

public String toString() {

// initializing an empty String variable

String str = "";

// taking reference to head

LinearNode curr = top;

// looping until curr is null

while (curr != null) {

// fetching element and appending to str

str += curr.getElement();

// if there are more node(s) appending a newline to str

if (curr.getNext() != null) {

str += " ";

}

// advancing to next node

curr = curr.getNext();

}

// returning str

return str;

}

package Class; HNM non 5 LinkedDriver (Java Application] /Library/Java/JavaVirtual Machines/jdk-13.0.2.jd 4 elements on the stack (testing toString): Artist: Rembrand Genre: Landscape Era: Renaissance Artist: jackson Pollock Genre: History Era: Modern Artist: Pablo Picasso Genre: Portrait Era: Middle ages Artist: Vincent Van Gogh Genre: Sculpture Era: Modern 40 import Stack.LinkedStack; import Stack. StackInterface; 6 7 public class LinkedDriver { 80 public static void main(String[] args) { 9 StackInterface ptStack = new LinkedStack(); 10 11 ptStack.push(new Painting ("Vincent Van Gogh", "Sculpture" , "Modern")); 12 ptStack.push(new Painting "Pablo Picasso", "Portrait", "Middle ages")); 13 ptStack.push(new Painting("jackson Pollock", "History", "Modern")); 14 ptStack.push(new Painting ("Rembrandt", "Landscape", "Renaissance")); 15 16 17 18 System.out.println(ptStack.size() + " elements on the stack In (testing toString): " 19 + ptStack); 20 System.out.println(); 21 22 System.out.println("Top artists name: " + ptStack.peek().getArtist()); 23 24 25 26 27 } 28 } 29 Top artists name: Rembrand

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions