Complete StackArray.java code below.
Use StackArrayDemo.java to test your implementation of StackArray.java.
StackArray.java below.
public class StackArray <T> {
public static int CAPACITY = 100;
private final T[] elements;
private int topIndex;
// Constructor
public StackArray() {
// Initialize elements
// Initialize topIndex to -1
}
public T peek() {
// If topIndex is less than zero, return null.
// Otherwise, return element from top of the stack.
}
public T pop() {
// If topIndex is less than zero, return null.
// Otherwise return element from top of the stack, and decrement topIndex
}
public void push(T obj) {
// Push obj into top of the stack
// And, increment topIndex
}
public int getStackSize() {
// Return stack size, i.e., number of elements
}
@Override
public String toString() {
String s = "The stack is: ";
for (int i = topIndex; i >= 0; i--)
s += elements[i] + " ";
return s;
}
}//end class StackArray
Use StackArrayDemo.java below to test your code.
public class StackArrayDemo {
public static void main(String [] args) {
StackArray a = new StackArray();
int score = 0;
if (a.peek() == null)
score += 10;
else
System.out.println("Check peek()");
a.push("Orange");
System.out.println(a);
a.push("Apple");
System.out.println(a);
a.push("Guava");
System.out.println(a);
if (a.peek().equals("Guava"))
score += 10;
else
System.out.println("Check push()");
a.pop();
System.out.println(a);
a.pop();
System.out.println(a);
if (a.peek().equals("Orange"))
score += 10;
else
System.out.println("Check pop()");
a.push("Mango");
System.out.println(a);
if (a.getStackSize() == 2)
score += 10;
else
System.out.println("Check getStackSize()");
System.out.printf("Your score is %d/40\n", score);
}
}
public class StackArray<T> {
public static int CAPACITY = 100;
private final T[] elements;
private int topIndex;
// Constructor
public StackArray() {
elements = (T[]) new Object[CAPACITY];
topIndex = -1;
}
public T peek() {
// If topIndex is less than zero, return null.
// Otherwise, return element from top of the stack.
if (topIndex < 0)
return null;
return elements[topIndex];
}
public T pop() {
// If topIndex is less than zero, return null.
// Otherwise return element from top of the stack, and decrement topIndex
if (topIndex < 0)
return null;
return elements[topIndex--];
}
public void push(T obj) {
// Push obj into top of the stack
// And, increment topIndex
elements[++topIndex] = obj;
}
public int getStackSize() {
return topIndex+1;
}
@Override
public String toString() {
String s = "The stack is: ";
for (int i = topIndex; i >= 0; i--)
s += elements[i] + " ";
return s;
}
}//end class StackArray

Complete StackArray.java code below. Use StackArrayDemo.java to test your implementation of StackArray.java. StackArray.java below. public class...
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException at InfixExpression.execute(InfixExpression.java:98) at InfixExpression.Evaluate(InfixExpression.java:65) at InfixExpression.setWholeExpr(InfixExpression.java:24) at InfixExpression.<init>(InfixExpression.java:17) at Main.testHW1(Main.java:17) at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...
Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...
how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...
how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...
Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...
Now, your objective is to rewrite the same Stack class with a Generic ArrayList and make the entire class support using Generic types. You should be able to create a Stack of any primitive wrapper class, and show us that your Generic Stack implementation (push, pop, search, display, etc.) works with: • Character (Stack) • Integer (Stack) • Float (Stack • Double (Stack) • String (Stack) You can create these Stack objects in main() and perform operations on them to...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...