Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need Help with this Java program, All the files needed to solve this are zippe

ID: 3858297 • Letter: I

Question

I need Help with this Java program, All the files needed to solve this are zipped here:

https://drive.google.com/file/d/0B-4gpQ8Y_EDgbWczWEdmMFhqclU/view?usp=sharing

Please make sure it compiles correctly and all parts are done. thank you

Here is the answer for the #42, i need answer for 46. a,b,c,d please do all parts thank you

File Name: LinkedStack.java

//----------------------------------------------------------------------

// LinkedStack.java         by Dale/Joyce/Weems                Chapter 3

//

// Implements UnboundedStackInterface using a linked list

// to hold the stack elements.

//-----------------------------------------------------------------------

package ch03.stacks;

import support.LLNode;

public class LinkedStack<T> implements UnboundedStackInterface<T>

{

protected LLNode<T> top;   // reference to the top of this stack

   public LinkedStack()

   {

          top = null;

   }

   public void push(T element)

   // Places element at the top of this stack.

   {

          LLNode<T> newNode = new LLNode<T>(element);

          newNode.setLink(top);

          top = newNode;

   }    

   public void pop()

   // Throws StackUnderflowException if this stack is empty,

   // otherwise removes top element from this stack.

   {                 

          if (!isEmpty())

          {

              top = top.getLink();

          }

          else

throw new StackUnderflowException("Pop attempted on an empty stack.");

   }

   public T top()

   // Throws StackUnderflowException if this stack is empty,

   // otherwise returns top element from this stack.

   {                

    if (!isEmpty())

              return top.getInfo();

    else

throw new StackUnderflowException("Top attempted on an empty stack.");

   }

   public boolean isEmpty()

// Returns true if this stack is empty, otherwise returns false.

   {             

          if (top == null)

              return true;

          else

              return false;

   }

     @Override

     //ToDo #42

   public String toString ()

   {

          //check stack

          if(isEmpty())

              //Return

              return "Stack is empty";

    

          //create

String rtStr="";

//Define

          LLNode<T> myNode=top;

          //Loop

          while(myNode!=null)

          {

              //Add

rtStr = rtStr + (myNode.getInfo()).toString() + " ";

//Next element

              myNode=myNode.getLink();

          }

          //Return

          return rtStr;

   }

     //ToDo #46a "walk"

   public int sizeIs()

   {

     return -100;

   }

}

42. Create a toString method for the LinkedStack class. This method should cre- ate and return a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the LinkedStack class and for testing and debugging applications that use the LinkedStack class 46. Suppose we decide to add a new operation to our Stack ADT called size!s, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizels is public int sizelsO a. Write the code for sizeIs for the ArrayStack class. b. Write the code for sizels for the LinkedStack class (do not add any instance variables to the class; each time sizels is called you must "walk" through the stack and count the nodes) c. Suppose you decide to augment the LinkedStack class with an instance variable size that always holds the current size of the stack. Now you can implement the sizels operation by just returning the value of size. Identify all of the methods of LinkedStack that you need to modify to maintain the correct value in the size variable and describe how you would change them. d. Analyze the methods created/changed in parts a, b, and c in terms of Big-o efficiency

Explanation / Answer

import ch03.stacks.*;

import support.*;

public class Driver {

public static void main(String[] args)

{ // #42

System.out

.println("#42 implementing method toString() in LinkedStack class");

UnboundedStackInterface<String> myStack;

myStack = new LinkedStack<String>();

myStack.push("A");

myStack.push("B");

myStack.push("C");

System.out.print("MyStack: ");

System.out.println(myStack);

myStack.pop();

System.out.print("MyStack after pop: ");

System.out.println(myStack);

UnboundedStackInterface<Integer> myIntegerStack;

myIntegerStack = new LinkedStack<Integer>();

myIntegerStack.push(7);

myIntegerStack.push(9);

System.out.print("MyIntegerStack: ");

System.out.println(myIntegerStack);

UnboundedStackInterface<Integer> myEmptyStack;

myEmptyStack = new LinkedStack<Integer>();

System.out.print("MyEmptyStack: ");

System.out.println(myEmptyStack);

// #46

// a.

System.out

.println("#46a.implementing sizeIs() in ArrayStack class and testing in Driver class ");

BoundedStackInterface<String> myStackArray;

myStackArray = new ArrayStack<String>();

System.out.println("myStackArray size is: " + myStackArray.sizeIs());

myStackArray.push("A");

myStackArray.push("B");

myStackArray.push("C");

System.out.println("myStackArray after pushing A, B, C, size is: "

+ myStackArray.sizeIs());

myStackArray.pop();

System.out.print("MyStackArray after pop, ");

System.out.println("size is: " + myStackArray.sizeIs());

System.out

.println("46b.implementing sizeIs() in LinkedStack class and testing in Driver class(walk)");

UnboundedStackInterface<String> myStack2;

myStack2 = new LinkedStack<String>();

System.out.println("Just after creation myStack2 size is: "

+ myStack2.sizeIs());

myStack2.push("D");

myStack2.push("F");

myStack2.push("G");

System.out.print("MyStack2: ");

System.out.println(myStack2);

System.out.println("myStack2 size is: " + myStack2.sizeIs());

myStack2.pop();

System.out.print("MyStack2 after pop: ");

System.out.println(myStack2);

System.out.println("myStack2 size is: " + myStack2.sizeIs());

}

}

//----------------------------------------------------------------------

// ArrayListStack.java by Dale/Joyce/Weems Chapter 3

//

// Implements UnboundedStackInterface using an ArrayList to

// hold the stack elements.

//----------------------------------------------------------------------

package ch03.stacks;

import java.util.*;

public class ArrayListStack<T> implements UnboundedStackInterface<T> {

protected ArrayList<T> stack; // ArrayList that holds stack elements

public ArrayListStack() {

stack = new ArrayList<T>();

}

public void push(T element)

// Places element at the top of this stack.

{

stack.add(element);

}

public void pop()

// Throws StackUnderflowException if this stack is empty,

// otherwise removes top element from this stack.

{

if (!isEmpty()) {

stack.remove(stack.size() - 1);

} else

throw new StackUnderflowException(

"Pop attempted on an empty stack.");

}

public T top()

// Throws StackUnderflowException if this stack is empty,

// otherwise returns top element from this stack.

{

T topOfStack = null;

if (!isEmpty())

topOfStack = stack.get(stack.size() - 1);

else

throw new StackUnderflowException(

"Top attempted on an empty stack.");

return topOfStack;

}

public boolean isEmpty()

// Returns true if this stack is empty, otherwise returns false.

{

return (stack.size() == 0);

}

public int sizeIs() {

return -1;

}

}

//----------------------------------------------------------------

// ArrayStack.java by Dale/Joyce/Weems Chapter 3

//

// Implements BoundedStackInterface using an array to hold the

// stack elements.

//

// Two constructors are provided: one that creates an array of a

// default size and one that allows the calling program to

// specify the size.

//----------------------------------------------------------------

package ch03.stacks;

public class ArrayStack<T> implements BoundedStackInterface<T> {

protected final int DEFCAP = 100; // default capacity

protected T[] stack; // holds stack elements

protected int topIndex = -1; // index of top element in stack

public ArrayStack() {

stack = (T[]) new Object[DEFCAP];

}

public ArrayStack(int maxSize) {

stack = (T[]) new Object[maxSize];

}

public void push(T element)

// Throws StackOverflowException if this stack is full,

// otherwise places element at the top of this stack.

{

if (!isFull()) {

topIndex++;

stack[topIndex] = element;

} else

throw new StackOverflowException("Push attempted on a full stack.");

}

public void pop()

// Throws StackUnderflowException if this stack is empty,

// otherwise removes top element from this stack.

{

if (!isEmpty()) {

stack[topIndex] = null;

topIndex--;

} else

throw new StackUnderflowException(

"Pop attempted on an empty stack.");

}

public T top()

// Throws StackUnderflowException if this stack is empty,

// otherwise returns top element from this stack.

{

T topOfStack = null;

if (!isEmpty())

topOfStack = stack[topIndex];

else

throw new StackUnderflowException(

"Top attempted on an empty stack.");

return topOfStack;

}

public boolean isEmpty()

// Returns true if this stack is empty, otherwise returns false.

{

if (topIndex == -1)

return true;

else

return false;

}

public boolean isFull()

// Returns true if this stack is full, otherwise returns false.

{

if (topIndex == (stack.length - 1))

return true;

else

return false;

}

// ToDo $46b

public int sizeIs() {

return topIndex + 1;

}

}

//package ch03.stacks;

//----------------------------------------------------------------------------

// UnboundedStackInterface.java by Dale/Joyce/Weems Chapter 3

//

// Interface for a class that implements a stack of <T> with no bound

// on the size of the stack. A stack is a last-in, first-out structure.

//----------------------------------------------------------------------------

package ch03.stacks;

public interface UnboundedStackInterface<T> extends StackInterface<T>

{

void push(T element);

// Places element at the top of this stack.

int sizeIs();

}

package ch03.stacks;

public class StackUnderflowException extends RuntimeException

{

public StackUnderflowException()

{

super();

}

public StackUnderflowException(String message)

{

super(message);

}

}

package ch03.stacks;

public class StackOverflowException extends RuntimeException

{

public StackOverflowException()

{

super();

}

public StackOverflowException(String message)

{

super(message);

}

}

//----------------------------------------------------------------------------

// StackInterface.java by Dale/Joyce/Weems Chapter 3

//

// Interface for a class that implements a stack of <T>.

// A stack is a last-in, first-out structure.

//----------------------------------------------------------------------------

package ch03.stacks;

public interface StackInterface<T>

{

void pop() throws StackUnderflowException;

// Throws StackUnderflowException if this stack is empty,

// otherwise removes top element from this stack.

  

T top() throws StackUnderflowException;

// Throws StackUnderflowException if this stack is empty,

// otherwise returns top element from this stack.

  

boolean isEmpty();

// Returns true if this stack is empty, otherwise returns false.

}

//----------------------------------------------------------------------

// LinkedStack.java by Dale/Joyce/Weems Chapter 3

//

// Implements UnboundedStackInterface using a linked list

// to hold the stack elements.

//-----------------------------------------------------------------------

package ch03.stacks;

import support.LLNode;

public class LinkedStack<T> implements UnboundedStackInterface<T> {

protected LLNode<T> top; // reference to the top of this stack

protected int size;

public LinkedStack()

{

top = null;

size = 0;

}

public void push(T element)

// Places element at the top of this stack.

{

LLNode<T> newNode = new LLNode<T>(element);

newNode.setLink(top);

top = newNode;

size++;

}

public void pop()

// Throws StackUnderflowException if this stack is empty,

// otherwise removes top element from this stack.

{

if (!isEmpty()) {

top = top.getLink();

size--;

} else

throw new StackUnderflowException(

"Pop attempted on an empty stack.");

}

public T top()

// Throws StackUnderflowException if this stack is empty,

// otherwise returns top element from this stack.

{

if (!isEmpty())

return top.getInfo();

else

throw new StackUnderflowException(

"Top attempted on an empty stack.");

}

public boolean isEmpty()

// Returns true if this stack is empty, otherwise returns false.

{

if (top == null)

return true;

else

return false;

}

@Override

// ToDo #42

public String toString() {

String result = "";

LLNode<T> current = top;

// check stack

if (isEmpty()) {

// Return

return "Stack is empty";

} else {

while (current != null) {

result += current.getInfo() + ", ";

current = current.getLink();

}

return "List: " + result;

}

}

// ToDo #46b "walk"

// public int sizeIs() {

// if (isEmpty())

// return 0;

// else {

// int count = 0;

// LLNode<T> current = top;

// while (current != null) {

// current = current.getLink();

// count++;

// }

// return count;

//

// }

// }

// ToDo #46c "walk"

public int sizeIs() {

return size;

}

}

//----------------------------------------------------------------------------

// BoundedStackInterface.java by Dale/Joyce/Weems Chapter 3

//

// Interface for a class that implements a stack of <T> with a bound

// on the size of the stack. A stack is a last-in, first-out structure.

//----------------------------------------------------------------------------

package ch03.stacks;

public interface BoundedStackInterface<T> extends StackInterface<T>

{

void push(T element) throws StackOverflowException;

// Throws StackOverflowException if this stack is full,

// otherwise places element at the top of this stack.

boolean isFull();

// Returns true if this stack is full, otherwise returns false.

int sizeIs();

}

//----------------------------------------------------------------------------

// LLNode.java by Dale/Joyce/Weems Chapter 3

//

// Implements <T> nodes for a Linked List.

//----------------------------------------------------------------------------

package support;

public class LLNode<T> {

private LLNode<T> link;

private T info;

public LLNode(T info) {

this.info = info;

link = null;

}

public void setInfo(T info)

// Sets info of this LLNode.

{

this.info = info;

}

public T getInfo()

// Returns info of this LLONode.

{

return info;

}

public void setLink(LLNode<T> link)

// Sets link of this LLNode.

{

this.link = link;

}

public LLNode<T> getLink()

// Returns link of this LLNode.

{

return link;

}

}

OUTPUT:

#42 implementing method toString() in LinkedStack class

MyStack: List: C, B, A,

MyStack after pop: List: B, A,

MyIntegerStack: List: 9, 7,

MyEmptyStack: Stack is empty

#46a.implementing sizeIs() in ArrayStack class and testing in Driver class

myStackArray size is: 0

myStackArray after pushing A, B, C, size is: 3

MyStackArray after pop, size is: 2

46b.implementing sizeIs() in LinkedStack class and testing in Driver class(walk)

Just after creation myStack2 size is: 0

MyStack2: List: G, F, D,

myStack2 size is: 3

MyStack2 after pop: List: F, D,

myStack2 size is: 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote