JAVA Lab
Create a class called ArrayBasedStack.
Declare the following variables:
• data: references an array storing elements in the list
• topOfStack: an int value representing the location of the stack
top in the array
• INITIAL_CAPACITY: the default capacity of the stack
public class ArrayBasedStack <E> {
private E[] data;
private int topOfStack;
private static final int INITIAL_CAPACITY = 5; }
Add a constructor that will initialize the stack with a
user-defined initial capacity. The top of the stack will be
assigned a value of -1 to indicate that the stack is currently
empty.
public ArrayBasedStack(int capacity) {
data = (E[]) new Object[capacity];
____________ = -1;
}
Another constructor takes no parameters and sets the capacity of
the data array to the default value:
public ArrayBasedStack() {
this(INITIAL_CAPACITY);
}
Add a method that will expand the array when necessary. The method
is very similar to that in the ArrayBasedList activity. To reduce
the amount of code to 1 line, you could also use the copyOf method
in java.util.Arrays.
private void expand(int amount) {
E[] temp = (E[]) new Object[data.length + amount];
for (int i = 0; i <= topOfStack; i++) {
temp[i] = data[i];
}
data = temp;
}
Add a method called push that will add an element to the top of the
stack:
public void push(E newElement) {
}
If the array is full, increase the length of the array by the
following amount (approximately 65% of its current size):
if (topOfStack == data.length - 1) {
expand((data.length * 3) / 2 + 1);
}
• Increase the top of the stack to point to the new element and
insert the new element into the stack:
topOfStack++;
data[topOfStack] = newElement;
Create a new ArrayBasedStack in interactions and open a viewer on
the stack object. Answer the following questions and use the viewer
to verify your answers:
• What will be the value of topOfStack after each line of code is
executed?
• How many times will the internal array expand? What will its size
be after each expansion?
ArrayBasedStack stack = new ArrayBasedStack(3);
stack.push(3);
stack.push(6);
stack.push(4);
stack.push(8);
The isEmpty method will return whether the stack has elements. If
the top of the stack is less than 0 then there are no elements
currently in the stack:
public boolean isEmpty() {
return __________ < 0;
}
The pop method will remove the last element that was added to the
stack.
public E pop() {
}
• If stack is empty, throw an EmptyStackException. Import the
EmptyStackException class which is in the java.util package.
if (isEmpty()) {
// stack underflow
throw new EmptyStackException();
}
• If there are elements, get the element at the end of the stack
(index is topOfStack) and decrement the index of the top of the
stack. Set the previous top of the stack to null.
E returnObj = data[__________];
data[topOfStack] = null;
• Decrement the index of the top of the stack and return the
previous top.
topOfStack--;
return __________;
Run the following in interactions with a viewer open on the stack
object. Answer the following questions and then use the viewer to
verify your answers:
• What will the stack look like after invoking the push operation
on String objects "1st", "2nd", and "3rd"?
• What would a pop operation return at this point? What would a
second pop operation return?
ArrayBasedStack stack = new ArrayBasedStack(3);
stack.isEmpty()
true stack.push("1st");
stack.isEmpty()
false
stack.push("2nd");
stack.push("3rd");
stack.pop()
3rd
stack.pop()
2nd
stack.push("4th");
stack.pop()
4th
stack.pop()
1st
stack.isEmpty()
true
If you had used a linked list implementation of the stack, how
would your stack differ from the array implementation?
Add a method called peek that will return the top of the stack but
will not remove the element from the stack.
public E peek() {
}
• If the stack is empty, throw an exception.
if (isEmpty()) {
throw new EmptyStackException();
}
• Otherwise, return the element at the top of the stack.
return data[__________];
Test your class as follows in the interactions pane. Answer the
following questions and verify your answers using the viewer:
• At what point is the internal array expanded, if at all?
• What are the contents of the stack before the push operation is
performed on object "o"?
ArrayBasedStack stack = new ArrayBasedStack(3);
stack.isEmpty()
true
stack.push("b");
stack.push("e");
stack.peek()
e
stack.pop()
e
stack.peek()
b
stack.pop()
b
stack.push("o");
stack.pop()
o
stack.isEmpty()
true
stack.pop()
Exception in evaluation thread java.util.EmptyStackException
Deliverables:
Implement a stack using a linked implementation. First, implement
the SinglyLinkedNode class from the activity on singly linked list.
Use the following hints to create your stack and use the same
operations in the interactions pane from the array-based version to
test your stack's functionality.
import java.util.EmptyStackException;
public class LinkedStack <E>{
private SinglyLinkedNode <E> topNode = null;
public void push(E newElement) {
// create a node that holds the new element
// set the next node reference of the new node to the current
top
// set the top node to reference the new element
}
public boolean isEmpty() {
// empty if the topNode is null
}
public E pop() {
// throw an exception if stack is empty
// store data from current top node (type E)
// set the top node to reference the next node in the stack
// return the stored data
}
public E peek() {
// throw an exception if stack is empty
// return the data from the top node
}
}
Submit both the ArrayBasedStack class and the LinkedStack class
implementations.
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...
Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list. This is only supposed to use THAT method, calling a normal Stack will give me a zero. I do have the conversion to postfix, but there may be error in there. But the main problem currently is the evaluation of postfix. I keep getting an error that I made for an empty stack, which I will include. For testing it is only supposed to...
Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix) { LinkedStack stack2 = new LinkedStack(); int val1; int val2; int result; for(int i = 0; i < postFix.length(); i++) { char m = postFix.charAt(i); if(Character.isDigit(m)) { stack2.push(m); } else { ...
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...
In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...
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...
Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...
What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...
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...
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...
I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...