×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Implement Stacks DS in Java Assignment Solution

July 15, 2024
Dr. Ethan Taylor
Dr. Ethan
🇬🇧 United Kingdom
Java
Dr. Ethan Taylor holds a Ph.D. in Computer Science from the University of Oxford. With over 850 assignments completed, he brings extensive expertise in data structures, algorithms, and programming languages. Ethan specializes in array manipulation, dynamic memory allocation, and algorithm optimization, providing students with comprehensive guidance and practical solutions for their Array class interface homework.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Ensure you understand the dataset thoroughly before starting your machine learning assignment. Visualize the data, check for missing values, and identify patterns or anomalies to guide your model-building process effectively.
News
In 2024, universities have introduced new programming courses focusing on cybersecurity, machine learning, and artificial intelligence to better prepare students for modern IT careers.

Instructions

Objective

Write a java assignment program to implement stacks DS.

Requirements and Specifications

For this project, you will create two classes:

  • Stack.java
  • Main.java

Stack.Java is to be implemented like we did together on 3/24 and 3/26. You will create a private array in Stack that holds any values given to the stack. However, this time you will make it an array of Strings. Your Stack class must have the following: a Push() function, a Pop() function, and an IsEmpty() function. Push will return nothing, instead adding a String to the array. Pop() will remove and return the "top-most" String element in the stack, which should be the last item added to the array. IsEmpty() will return a bool for whether the array is empty or not. No System.out.print messages of any kind are allowed in the Stack class!

In Main.java, your goal is to create a stack of String objects and print them out. Create an array of Strings such that each element in the array is the name of a card. Create an ArrayList using this array. An example for this has been provided for your convenience:

Project_5_Sample_Code1.txtPreview the document

Notice how the elements are added into the array in an ordered fashion.

Create an empty Stack that can hold 52 elements. Using the java.util.Random package, use the Random function to randomly add elements from the ArrayList to the stack. As you add an element to the stack, remove it from the ArrayList. This way when the Stack is full, the ArrayList will be empty.

From here, Loop through and print all values from the stack. Use IsEmpty to check whether the Stack is empty. Use the return value from Pop() to get the top most value in order to print each value.

Goals

  • Get experience using Stacks.
  • Get experience creating custom classes.
  • Get experience with loops.
  • Experience with the java.util.Random class.

Source Code

/** * This is a stack data structure. It stores elements in a first-in-last-out * or last-in-first-out fashion. * * @author Yourname */ public class Stack { /** * An array that holds the elements of the stack. The last available * non-empty index is the top of the stack. */ private String[] elements; /** * Keep track the size of the stack and at the same time the reference to * the top index of the stack. */ private int size; /** * Create a stack with an initial capacity. * * @param capacity Starting capacity of the stack */ public Stack(int capacity) { elements = new String[capacity]; size = 0; } /** * Put an element on top of the stack. If the stack is full, then the * storage will be expanded. * * @param element Element to add on top of stack */ public void push(String element) { if (size >= elements.length) { // Increase the size if it's out of space String[] temp = new String[elements.length * 2]; System.arraycopy(elements, 0, temp, 0, elements.length); elements = temp; } // Finally, put it on top of stack and update the size. elements[size++] = element; } /** * Remove the top element. * * @return Top element or null if the stack is empty */ public String pop() { // Nothing to pop if the stack is empty if (isEmpty()) { return null; } // Remove the top of stack to return and update the size accordingly. return elements[--size]; } /** * Check if stack is empty. * * @return true if stack is empty, otherwise false */ public boolean isEmpty() { return size == 0; } }

Related Samples

Explore our Java assignment samples for clear, step-by-step solutions to coding challenges. Whether you're a beginner or advanced programmer, our examples will help enhance your understanding and improve your Java skills.