In Java 1 Add the following methods to the LinkedStack class
In Java
1. Add the following methods to the LinkedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayLinkedStack, not by calling the previously defined public methods of the class.
String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method.
int size()—returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method.
void popSome(int count)—removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack.
boolean swapStart()—if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true.
T poptop( )—the “classic” pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack.
2. Use the LinkedStack class to support an application that tracks the status of an online auction. Bidding begins at 1 (dollars, pounds, euros, or whatever) and proceeds in increments of at least 1. If a bid arrives that is less than the current bid, it is discarded. If a bid arrives that is more than the current bid, but less than the maximum bid by the current high bidder, then the current bid for the current high bidder is increased to match it and the new bid is discarded. If a bid arrives that is more than the maximum bid for the current high bidder, then the new bidder becomes the current high bidder, at a bid of one more than the previous high bidder’s maximum. When the auction is over (the end of the input is reached), a history of the actual bids (the ones not discarded), from high bid to low bid, should be displayed. For example:
The bid history for this auction would be
Input/output details can be determined by you or your instructor. In any case, as input proceeds the current status of the auction should be displayed. The final output should include the bid history as described above.
//---------------------------------------------------------------------------// LinkedStack.java by Dale/Joyce/Weems Chapter 2//// Implements StackInterface using a linked list to hold the stack elements.//---------------------------------------------------------------------------
package ch02.stacks;
import support.LLNode;
public class LinkedStack<T> implements StackInterface<T>{
protected LLNode<T> top;
// reference to the top of this stack
public LinkedStack()
{
top = null;
}
| New Bid | Result | High Bidder | High Bid | Maximum Bid |
|---|---|---|---|---|
| 7 John | New high bidder | John | 1 | 7 |
| 5 Hank | High bid increased | John | 5 | 7 |
| 10 Jill | New high bidder | Jill | 8 | 10 |
| 8 Thad | No change | Jill | 8 | 10 |
| 15 Joey | New high bidder | Joey | 11 | 15 |
Solution
//ArrayListDemo.java
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo
{
public static void main(String args[])
{
// Creating an ArrayList
ArrayList al = new ArrayList();
System.out.println(\"Initial size of ArrayList: \" + al.size()); //0
//Add elements to the ArrayList
al.add(\"Red\");
al.add(\"Green\");
al.add(\"Blue\");
al.add(\"Pink\");
al.add(\"Orange\");
System.out.println(\"\ Size of ArrayList after additions: \" + al.size()); //5
//Display the ArrayLiAst
System.out.println(\"\ Contents of ArrayList After add: \" + al);
// Remove elements from ArrayList
al.remove(4);
System.out.println(\"\ Contents of ArrayList after remove index: \" + al);
al.remove(\"Pink\");
System.out.println(\"\ Contents of ArrayList after remove object: \" + al);
System.out.println(\"\ Size of ArrayList after deletions: \" + al.size());
// Display the ArrayList
System.out.println(\"\ Contents of ArrayList after modification: \" + al);
StringBuffer sb = new StringBuffer();
String alElement = (String)(al.get(1));
System.out.println(\"alElement :\"+alElement);
al.add(alElement+\" rose\");
System.out.println(al);
System.out.println(\"al elements through Iterator\");
Iterator alIterator = al.iterator();
while(alIterator.hasNext())
{
String str = (String)alIterator.next();
System.out.println(str);
sb.append(str+\",\");
}
System.out.println(\"Array List values in string buffer :\"+sb);
}
}

