Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack and use its classes and methods. Note that java.uitl.stack uses peek instead of top (as we did in class). Look up the documentation for java.uitl.stack to see what methods it contains and how to call them. Create a Stack of cards that will hold strings. Push new items to the stack (Use "Qclubs" for Queen of clubs and so on) After your 'deck' has been created, print it out. Pop some (a few) items from the Stack Get an item at the top of the stack without removing it Print out the item you just got Print out the Stack as it currently exists.
CODE:
package stackdeck;
import java.util.Stack;
public class StackDeck {
public static void main(String[] args) {
Stack<String> deck = new Stack<>();
String suits = null, ranks = null;
//creating deck
for(int i=0 ; i<4 ; i++){
if(i == 0)
suits = "hearts";
else if(i == 1)
suits = "diamonds";
else if (i==2)
suits = "spades";
else
suits = "clubs";
for(int j=1 ; j<14 ; j++){
if(j == 1)
ranks = "A";
else if (j == 11)
ranks = "J";
else if(j == 12)
ranks = "Q";
else if(j == 13)
ranks = "K";
else
ranks = j+"";
String card = "";
card += ranks;
card += suits;
deck.push(card);
}
}
System.out.println("Printing the popped cards:");
System.out.println(deck.pop());
System.out.println(deck.pop());
System.out.println(deck.pop());
System.out.println(deck.pop());
System.out.println("\nPeeking the top card:");
System.out.println(deck.peek());
System.out.println("\nPrinting the deck:");
printDeck(deck);
}
static void printDeck(Stack<String> deck)
{
// If stack is empty then return
if (deck.isEmpty())
return;
String card = deck.peek();
// Pop the top element of the stack
deck.pop();
// Recursively call the function printDeck
printDeck(deck);
//print the popped card
System.out.print(card + "\n");
// Push the same card back onto the stack
deck.push(card);
}
}
//Screenshot of Code:
//refer this to know indentations.

//Sample I/O:


//Hope this helps.
//Give thumbs up. Thank you!
Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack...
Design a stack class by importing the available java.util.Stack to have the following features: push(x) -- push element x onto stack, where x is anywhere between Integer.MIN_VALUE and Integer.MAX_VALUE. pop() -- remove the element on top of the stack. top() -- get the top element. getMax() -- retrieve the max element in the stack in constant time (i.e., O(1)). Your code should have the following shape and form, all in one .java file. Note the styling and documentation API already...
Java has a class for a stack. It is java.util.Stack. Let us make use of this class. Step 1: Create a project. Name the project MyStk. This will result in the creation of the main class called MyStk if you are using Netbeans IDE with relevant default settings unchanged. If comments are excluded you should see the following in the code editor after creating the project. package mystk; public class MyStk { public static void main(String[ ] args){ } }...
For java Review # 6: Stacks 1) Create a stack manually and automatically 2) PUSH (add a new node to the TOP of the stack) 3) POP (remove the top node from the TOP of the stack) 4) PEEK
Write a program that uses a stack to reverse its inputs. Your
stack must be generic and you must demonstrate that it accepts both
String and Integer types. Your stack must implement the following
methods:
push,
pop,
isEmpty (returns true if the stack is empty and false
otherwise), and
size (returns an integer value for the number of items in the
stack).
You may use either an ArrayList or a LinkedList to implement
your stack. Also, your pop method must...
Java: Code a class encapsulating a stack of clothes using an array. A clothing item has the following attributes: names, color, and whether it can be washed at high temperature. Limit your stack to 20 clothing items. In addition to creating your push, pop, and peek methods; create two additional methods: a method that return all of the clothing items of a given color; a method that returns how many clothing items in the stack can be washed at high...
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...
JAVA JAVA 1- Create two arrays one for the rank of the playing cards and one for the suit. 2- Create an array of 52 strings that contain a deck of playing cards. The first 13 items are the cards, whose suit is clubs, the next 13 diamonds, the next 13 hearts and finally the spades. In this step, the array and the order of the cards should be printed. 3- Use the shuffling algorithm you have learnt in the...
template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...
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...