Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program Specifications : We have already used the stack ADT to design a simple game. Since ADTs are not concerned about HOW the inner workings

Program Specifications: We have already used the stack ADT to design a simple game. Since ADTs are not concerned about HOW the inner workings are designed and implemented, we should be able to use the same exact code for the game we have already programmed even if we re-declare all the decks of cards to use a different implementation of the stack ADT that uses linked nodes instead.

The first part of this program is to implement the stack ADT using linked nodes.

The second part is identical to the previous assignment. Program a simple game that shuffles a deck of 52 cards and hands them out to two players randomly (26 each). Write a simulation of the card game, where, at each turn, both players take the card from the top of their pile and the player with the higher card wins that round. The winner of that round takes both cards and adds it to a stack of won cards.

In essence, there are four stacks. Each player has one stack of cards they are playing with and one stack of cards they have won. There are 26 rounds, since each player has a full stack of cards. At the end of the game, the player with the most cards in their won pile wins.

Notes:

The deck of cards can simply hold an integer between 0 and 51. The higher number wins. Dont worry about implementing suits like spades, hearts, etc.

To Do:

In LinkedStack.java Implement push(), pop(), peek(), and toString() methods.

LinkedStack.java

public class LinkedStack { Node head; public void push(T item) { if (head == null) { // The stack is empty Node newNode = new Node(item, null); head = newNode; } else { // Stack is not empty, create a new node at the top of the stack // The new item's next link goes to the "old" head Node newNode = new Node(item, head); // Now we can re-assign the link to the new head head = newNode; } } public T pop() { // TODO: Write code to add pop functionality return null; } public T peek() { // TODO: Write code to add peek functionality return null; } public int length() { Node n = head; int length = 0; while (n != null) { length++; n = n.next; } return length; } @Override public String toString() { // TODO: Write code to add toString functionality return ""; } // Nested class Node private class Node { T data; Node next; // Constructor for inner class Node Node(T data, Node next) { this.data = data; this.next = next; } // Setters & Getters for inner class Node public T getData() { return data; } public void setData(T data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

Explain what a patent is and describe different types of patents.

Answered: 1 week ago

Question

10. What is meant by a feed rate?

Answered: 1 week ago