
Implementing your own version of a stack. You will be using the List provided below (List.java) to create this stack.
Use the StackAssignment.zip (as shown below). Please look at javadoc comments in StackInterface.java to understand what each method should be doing; DO NOT modify StackInterface.java. You have been provided with the class header for the stack in Stack.java, you WILL be modifying Stack.java to implement your stack. I have also included StackTester.java to help test your stack; DO NOT modify StackTester.java.
You will need to create a new exception class "EmptyCollectionException" that extends "RuntimeException". Please read the javadoc comment for the pop() method in StackInterface.java for details.
Testing: Use the provided StackTester.java to test your stack. Do NOT modify StackTester.java. When running StackTester.java, your should see the following output:
List.java
public class List<T> {
private T[] items = (T[]) new Object[10];
private int size = 0;
public void add(T a) {
if (items.length == this.size) {
this.resize();
}
this.items[size] = a;
this.size++;
}
public void add(int index, T a) {
if (index < this.size && index >= 0) {
if (items.length == this.size) {
this.resize();
}
for (int i = this.size; i > index; i--) {
this.items[i] = this.items[i - 1];
}
this.items[index] = a;
this.size++;
}
}
public void remove(int index) {
if (index < size && index >= 0) {
for (int i = index; i < this.size - 1; i++) {
this.items[i] = this.items[i + 1];
}
size--;
}
}
public void remove(T a) {
for (int i = 0; i < this.size; i++) {
if (this.items[i] == a) {
this.remove(i);
break;
}
}
}
public void set(int index, T a) {
if (index < size && index >= 0) {
this.items[index] = a;
}
}
public T get(int index) {
if (index < this.size && index >= 0) {
return this.items[index];
}
return null;
}
private void resize() {
T[] newItems = (T[]) new Object[this.items.length * 2];
for (int i = 0; i < this.items.length; i++) {
newItems[i] = this.items[i];
}
this.items = newItems;
}
public int size() {
return this.size;
}
}
StackInterface.java (DO NOT MODIFY)
public interface StackInterface<T extends Comparable<T>> {
/**
* Adds an element to the top of the stack.
* @param element the element to add to the stack
*/
void push(T element);
/**
* Removes and returns an element from the top of the stack.
* If the stack is empty, throw EmptyCollectionException
* @return the element from top of the stack.
*/
T pop() throws RuntimeException;
/**
* Returns an element from the top of the stack or null if the stack is empty.
* @return the element from top of the stack.
*/
T peek();
/**
* Returns true if the stack is empty else false.
* @return true if the stack is empty.
*/
boolean isEmpty();
/**
* Returns the number of elements in the stack.
* @return size of the stack
*/
int size();
}
StackTester.java (DO NOT MODIFY)
public class StackTester {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(24);
System.out.println("\nThe stack has " + stack.size() + " elements.");
System.out.println("\nThe contents of the stack are: " + getStackContents(stack));
System.out.println("\nThe stack has " + stack.size() + " elements.");
stack.push(50);
stack.push(80);
stack.push(24);
stack.push(95);
stack.push(98);
System.out.println("\nCalling stack.peek() returned: " + stack.peek());
System.out.println("\nThe stack has " + stack.size() + " elements.");
stack.push(28);
System.out.println("\nCalling stack.pop() returned: " + stack.pop());
System.out.println("\nThe contents of the stack are: " + getStackContents(stack) + "\n");
}
private static String getStackContents(Stack<Integer> stack) {
var result = "";
while (stack.peek() != null) {
result += stack.pop();
if (stack.peek() != null) {
result += ", ";
}
}
return result;
}
}
Stack.java
public class Stack<T extends Comparable<T>> implements StackInterface<T> {
private int size = 0;
private List<T> list = new List<T>();
// Implement your Stack here..
}


Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

- Description: In this project, you are required to make a java code to build a blockchain including multiple blocks. In each block, it contains a list of transactions, previous hash - the digital signature of the previous block, and the hash of the current block which is based on the list of transactions and the previous hash. If anyone would change anything with the previous block, the digital signature of the current block will change. When this changes, the digital signature of the next block will change. Detailed Requirements: 1. Define your block class which contains three private members and four public methods. a. Members: previousHash (int), transactions (String[]), and blockHash (int); b. Methods: i. Block( int previousHash, String[] transactions){} ii. getPreviousHash(){} iii. getTransaction(){} iv. getBlockHash(){} 2. Using ArrayList to design your Blockchain. 3. Your blockchain must contain genesis block and make it to be the first block. 4. You can add a block to the end of…arrow_forwardHi, would you be able to assist me with problem in java code. ......Four new students have arrived at Hogwarts School of Witchcraft and Wizardry. The students will join one of the four houses, Gryffindor, Hufflepuff, Ravenclaw, and Slytherin. The sorting hat has gone missing so in the meantime students will provide a list of the houses they wish to join and the Houses will rank the students in the order they wish to choose. You have been tasked with creating a program to determine a stable match between the students and Houses. The students will get to select their house of choice one at a time. If a student selects a house that is already chosen but rates higher on the list for that house, they are paired to that house and the student who is now un paired will need to select a new house form their ranked list. The program will need each student's name and list of house preferences. Additionally, it will need each house’s name and ranked list of students. How you implement that…arrow_forwardCreate a computer programme to arrange objects in a stack so that the smallest ones are on top. Although you are allowed to utilise an extra temporary stack, you are not allowed to transfer the components into another data structure (such an array). The following operations are supported by the stack: push, pop, peek, and is Empty.arrow_forward
- Write a java GUI program stacking Student objects of a class. Your program should have buttons for push, pop, and peek. Create a student class with these fields: name (type: String), age (type : int), major ( type: String), gpa (type: double) and gender (type:char). Use Java Stack class for implementation and use the GUI for user interaction. At run time books stored in stack should follow the last in First out (LIFO) of a stack.arrow_forwardOCaml Code: The goal of this project is to understand and build an interpreter for a small, OCaml-like, stackbased bytecode language. Make sure that the code compiles correctly and provide the code with the screenshot of the output. Make sure to have the following methods below: -Push integers, strings, and names on the stack -Push booleans -Pushing an error literal or unit literal will push :error: or :unit:onto the stack, respectively -Command pop removes the top value from the stack -The command add refers to integer addition. Since this is a binary operator, it consumes the toptwo values in the stack, calculates the sum and pushes the result back to the stack - Command sub refers to integer subtraction -Command mul refers to integer multiplication -Command div refers to integer division -Command rem refers to the remainder of integer division -Command neg is to calculate the negation of an integer -Command swap interchanges the top two elements in the stack, meaning that the…arrow_forwardYou will create two programs. The first one will use the data structure Stack and the other program will use the data structure Queue. Keep in mind that you should already know from your video and free textbook that Java uses a LinkedList integration for Queue. Stack Program Create a deck of cards using an array (Array size 15). Each card is an object. So you will have to create a Card class that has a value (1 - 10, Jack, Queen, King, Ace) and suit (clubs, diamonds, heart, spade). You will create a stack and randomly pick a card from the deck to put be pushed onto the stack. You will repeat this 5 times. Then you will take cards off the top of the stack (pop) and reveal the values of the cards in the output. As a challenge, you may have the user guess the value and suit of the card at the bottom of the stack. Queue Program There is a new concert coming to town. This concert is popular and has a long line. The line uses the data structure Queue. The people in the line are objects…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





