Objective

The purpose of this project is to give you experience with Stacks and using them. In this project, you'll create a stack of 52 cards in a random order, and print them out.

Instructions

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.

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.

Sample Runs

Sample Program Run (note there is no user input)

Cards in Arraylist:
Ace of Clubs
Ace of Diamonds
Ace of Hearts
Ace of Spades
2 Clubs
2 Diamonds
(...this should print like this all the way to the King of Spades).

Cards in Stack:
10 Hearts
Jack of Diamonds
3 Spades
8 Clubs
10 Clubs
Ace of hearts
(...these should randomly print like this, and iterate through all 52 cards in stack.).
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.