Course Answers

BUSINESS
COMPUTER SCIENCE
OTHER COURSES
My Profile

Solved: The goal of this assignment is to create a re-usable Card and Deck class

By |

CSE205 – Assignment 2

Cards & Deck – 50pts

Topics:

• Arrays

• Classes

• Methods

• Loops and Conditionals

Description

The goal of this assignment is to create a re-usable Card and Deck class. This assignment will lead into Assignment 3 where you will build more classes to flesh out a card game.

Use the following Guidelines:

• Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).

• Keep identifiers to a reasonably short length.

• User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other

identifiers (variables, methods, objects).

• Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and

loops. Be consistent with the number of spaces or tabs that you use to indent.

• Use white space to make your program more readable.

Important Note:

All files in submitted assignments must begin with the descriptive comment block. To avoid losing trivial points, make sure this comment header is included in every assignment you submit, and that it is updated accordingly from assignment to assignment.

Your header should identify you as the author and have a short description of the class contained in the file and what it does in your software.

June 2021

Programming Assignment:

Instructions:

You are eventually going to create a multi-class system to play a card game. For this assignment you will lay the groundwork by creating the Card and Deck classes and demonstrating them working.

Required Classes:

• Card – Standard 52-Card Deck cards

• Deck – Deck of cards with deck behaviors

• Assn2 Class (Main) – Demonstrates the cards and deck functionality

Specifications:

Part 1 – Build your Card Class

Start by building your basic Card Class

Card:

• Properties

o Value:int

o suit:char

o faceup:boolean

• Methods

o Constructor

? Card(value:int, suit:char)

o Getters for value and suit

? You can’t change a card after its built, so no setters

o Getter/Setter for FaceUp

? You can flip it over

o toString()

? Override it to print your card nicely

Part 2 – Build your Deck Class

Deck:

• Properties

o cards:Card[]

o cardCount:int

? Keeps track of the number of valid cards in the array

o maxSize:int

? Helps initialize the array

• Methods

o Constructors

? Deck()

• Default – fill with standard deck of 52

o Hint make a private method to help you fill it

? Deck(size:int)

• Set the maxSize to value passed in

• Initialize array … but don’t fill it

• Use this constructor to make an empty deck to fill via the Add() method

o Shuffle():void

? Shuffle the array

• Hint: remember Card object references

• Hint: Do the names Fisher and Yates ring a bell? Google might know …

o Draw:Card

? Reduce the card count, return a Card object reference

• Hint: last card in the array is easiest

• Hint2: think about setting the Card reference in the array

to null – not necessary, but cleaner

o AddToTop(newCard:Card )

? Take a Card reference add it to the array at index last valid index if able to

o AddToBottom(newCard:Card)

? Take a Card reference add it to the array at the 0 index if able

to

o PrintDeck

? Method to output your array all nice like …

? Hint: card.toString()

Both descriptions are outlining basic requirements, you can add more to these classes as needed. You can also rename the methods if you are clear in your commenting that the method represents a requirement.

REMEMBER: Proper Isolation of Responsibility!

Answer Preview

    public void shuffle() {
        Random rand = new Random();
        for (int i = cardCount - 1; i > 0; i--) {
            int j = rand.nextInt(i + 1);
            Card temp = cards[j];
            cards[j] = cards[i];
            cards[i] = temp;
        }
    }

Get Answer Now


Buy (USD $85.00)