Question

Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

package stackdeck: import java.util.Stack: EÇ public class StackDeck { public static void main(Stringargs) { Stack<String> deranks = Q; else if( == 13) ranks = K; else ranks = j+; String card = ; card += ranks; card += suits; deck.push(card);//Sample I/O:

run: Printing the popped cards: Kelubs Qclubs Jclubs 10clubs Deeking the top card: 9clubs Printing the deck: Ahearts D त in D8hearts Shearts 10hearts Jhearts Ohearts Khearts Adiamonds 2 diamonds 3diamonds 4diamonds 5diamonds Ediamonds 7diamonds 8diam

//Hope this helps.

//Give thumbs up. Thank you!

Add a comment
Know the answer?
Add Answer to:
Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a stack class by importing the available java.util.Stack to have the following features: push(x) --...

    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...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    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. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    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...

    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...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    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...

  • 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() =...

    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...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    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...

  • I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create...

    I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create the Javadoc web page (separate Assignment #15b). A. Create your own Stack Class, call it ArrayIntStack.java and write the standard four public Stack methods as listed below (this is the easy part of the assignment). Use at least one private helper method, maybe to ensure capacity of the array. All methods in your ArrayIntStack must have O(constant) run-time. Your ArrayIntStack extends no other Classes...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT