Question

Implement a class named PlayStack. An instance of this class should have a property cards, which...

  1. Implement a class named PlayStack. An instance of this class should have a property cards, which is a list of ordered cards that behaves like a stack. This property should not be directly accessible from outside the class. An instance of this class should behave like a stack, but it is a special stack as you will see below. You can only add cards from one end.
  2. Implement a method peekValue() which returns the value of the card on top of cards. Raise an Exception "Error: No cards in the playing stack" when appropriate.
  3. Implement a method peekFace() which returns the face of the card on top of cards. Raise an Exception "Error: No cards in the playing stack" when appropriate.
  4. Implement a method named playCard(card), that takes a card and pushes it on top of the cards stack. The card with value 0 can only be added when the cards stack is empty, and the method only accepts valid cards from the Card class (only numbers between 0-9 are accepted cards). Also, an added card can only be pushed on top of a card of directly lower value. For example, a card of value 5 can be added only on a card of value 4; Only a card of value 8 can be pushed on top of a card of value 7. If an error occurs the method should raise an Exception "Error: Card rejected". When the card of value 9 is played and accepted, the stack should be emptied and the method returns a list of the faces of all cards that were in the stack. Otherwise, the method returns an empty list.
  5. Implement the __str__() method to convert a PlayStack instance into a string such that the string represents the cards in the stack from the lowest value on the left to the highest value on the right delimited by two "|", eg. |[0][1][*][3][4][5]|.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans:

# the required class design is as follows:

# OUTPUT FOR THIS SAMPLE RUN!

# ANOTHER SAMPLE RUN:

# CORRESPONDING OUTPUT TO THE SAMPLE RUN:

# REQUIRED PROGRAM:

import random

class PlayStack:
  
def __init__(self):
  
self.cards = list()
  
def peekValue(self):
  
try:
card = self.cards[-1]
return card
except:
return "Error: No cards in the playing stack"
  
def peekFace(self):
  
try:
card = self.cards[-1]
return random.choice(["Heart","Spade","Club","Diamond"])
except:
return "Error: No cards in the playing stack"
  
def playCard(self,card):
  
try:
top = self.cards[-1]
if card == 9:
return self.__str__()
elif card == top + 1 and card > 0 and card < 9:
self.cards.append(card)
return "Appended Successfully"
elif card == 0:
return "Card 0 can only be added on an empty deck!"
else:
return "Error: Card {} rejected".format(card)
except:
if card == 9:
return self.__str__()
else:
self.cards.append(card)
return "Appended Successfully"
  
def __str__(self):
  
if len(self.cards) == 0:
return "[]"
  
else:
s = ""
for i in self.cards:
s += "[" + str(i) + "]"
return "|{}|".format(s)
  
  
# card object of class PlayStack
cards = PlayStack()
# testing the __str__ method on empty deck
print(cards)
# checking peekValue on empty deck
print(cards.peekValue())
# Inserting on empty deck
print(cards.playCard(0))
print(cards.peekValue())
print(cards.peekFace())
print(cards.playCard(1))
print(cards.playCard(2))
print(cards.playCard(3))
print(cards.playCard(5))
print(cards.playCard(9))

  

# PLEASE DO LIKE AND UPVOTE IF THIS WAS HELPFUL!

# THANK YOU SO MUCH IN ADVANCE!

Add a comment
Know the answer?
Add Answer to:
Implement a class named PlayStack. An instance of this class should have a property cards, which...
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
  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

  • Now, create a Deck class that consists of 52 cards (each card is an instance of...

    Now, create a Deck class that consists of 52 cards (each card is an instance of class Card) by filling in the template below. Represent the suit of cards as a string: "Spades", "Diamonds", "Hearts", "clubs" and the rank of cards as a single character or integer: 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", class Deck: def init (self): pass # Your code here. def draw (self): Returns the card at the top of the deck, and...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • A) Please implement a Python script to define a student class with the following attributes (instance...

    A) Please implement a Python script to define a student class with the following attributes (instance attributes): cwid: student’s CWID first_name : student’s first name last_name: student’s last name gender: student’s gender gpa: student’s gpa Please make these attributes as private ones so they have to be accessed via getter/setter methods or property. For this purpose, please define a getter/setter method and property for each of the attributes. Another thing you need to do is to define a constructor that...

  • JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings...

    JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings in the stack c. A constructor to construct an empty stack with a default capacity of 4 d. A constructor to construct an empty stack with a specified capacity e. A method empty() that returns true if the stack is empty f. A method push(String value) that stores value...

  • IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card...

    IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that...

  • IN JAVA PLEASE Lab 28.1 Add a class named Purchase with: an instance (String) variable for...

    IN JAVA PLEASE Lab 28.1 Add a class named Purchase with: an instance (String) variable for the customer's name an instance (double) variable for the customer's balance a member method public String toString() that returns the object's instance variable data as a single String; e.g., Cathy has a balance of $100.00 Lab 28.2 Add a main class named Store; . Then, add code that: declares and creates an ArrayList of Purchase objects named purchases (make sure to add import java.util.ArrayList...

  • C# Only, implement (source code) a class called Counter. It should have one private instance variable...

    C# Only, implement (source code) a class called Counter. It should have one private instance variable representing the value of the counter. It should have two instance methods: increment() which adds on to the counter value and getValue() which returns the current value. After creating the Counter class, create a program that simulates tossing a coin 100 times using two Counter objects (Head and Tails) to track the number of heads and tails.

  • The class "Deck" is defined for the initial deck of 52 cards. A simple list is...

    The class "Deck" is defined for the initial deck of 52 cards. A simple list is used to store 52 poker cards. Consider the Deck class given as below: class Deck: def __init__(self, full=True): self.cards = [] cardlist = ['AS', 'AH', 'AC', 'AD', '2S', '2H', '2C', '2D', '3S', '3H', '3C', '3D', '4S', '4H', '4C', '4D', '5S', '5H', '5C', '5D', '6S', '6H', '6C', '6D', '7S', '7H','7C', '7D', '8S', '8H', '8C', '8D', '9S', '9H', '9C', '9D', 'TS', 'TH', 'TC', 'TD', 'JS', 'JH',...

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