Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 5: A blueprint class for Card objects 30 points total; individual-only In this problem, you will create a class called Card that serves as

Problem 5: A blueprint class for Card objects

30 points total; individual-only

In this problem, you will create a class called Card that serves as a blueprint for objects that can be used to represent a single playing card in a card game.

Each Card object will have two characteristics:

a rank (e.g., 5, 8, Ace, or Queen). This will be stored as an integer even when the card is non-numeric card. For example, we will use a rank of 1 for an Ace card.

a suit (e.g., Clubs or Diamonds). This will be stored as a single character. For example, a suit of Clubs will be stored as the character 'C'.

The sections below outline the steps that you should take to implement this class.

The guidelines from Problem 4 also apply here.

Getting started

Download the following file: Card.java

Make sure to put it in your ps3 folder. If your browser doesnt allow you to specify where a file should be saved, try right-clicking on its link above and choosing Save as...or Save link as..., which should produce a dialog box that allows you to choose the correct folder for the file.

In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open the folder that you created for this assignment. (Note: You must open the folder; it is not sufficient to simply open the file.)

The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the name of the file that you downloaded.

Click on the name Card.java, which will open an editor window for that file. You will see that weve given you the beginnings of the class.

Read over the starter code in Card.java before continuing including all of the comments that we have provided.

You will note that we have provided several class constants global variables whose values cannot change. The definition of these variables includes several key components:

the keyword static, which means that the variable belongs to the class as a whole. This distinguishes it from a field, which is a non-static variable that is inside every object of the class. Every object gets its own separate set of the fields, but there is only one copy of a static variable, and it is shared by all objects of the class. In addition, it has class-level scope and can be accessed by any method written in the class.

the keyword final, which is what makes a variable a constant. The value that we assign to a class constant is its final value, and we cannot assign a new value to it somewhere else.

the type declaration, like that of any other variable

the variable name, which we capitalize so that it will be obvious that it is a constant

the initialization of the variable, which we must do here, because we cant change a class constant outside of its definition.

Your tasks

1. Although the rank of a card will be stored as an integer, we will sometimes need a string representation of the rank. For example, when printing a Queen (which has a rank of 12), we will use the string "Q" instead of the integer 12. This is why we have provided you with the RANK_STRINGS array, which is a class constant that contains string representations of all of the card ranks.

As the comments that weve included above the array indicate, if a card has a rank of r, the corresponding rank string can be obtained using the expression RANK_STRINGS[r]. For example, 12 is the numeric rank of a Queen card, so RANK_STRINGS[12] gives us the string "Q".

Your first task is to add a static method called rankNumFor that goes in the reverse direction taking a rank string as its only parameter and returning the corresponding integer rank. In other words, the method should find and return the index of the specified rank string in the RANK_STRINGS array. For example:

rankNumFor("Q") should return 12, because "Q" has an index of 12 in the RANK_STRINGS array.

rankNumFor("A") should return 1, because "A" has an index of 1 in the RANK_STRINGS array.

rankNumFor("10") should return 10, because "10" has an index of 10 in the RANK_STRINGS array.

If the value of the parameter is null or if it doesnt appear in the RANK_STRINGS array, the method should return -1. For example, rankNumFor("B") should return -1.

Important notes:

Because the smallest possible rank is 1, we have put an empty string in position 0 of the RANK_STRINGS array. When processing the array, you should ignore position 0.

Your method should work even if we change the contents of the RANK_STRINGS array. In other words, you should write code that processes any array of String objects that is assigned to the class constant RANK_STRINGS. The only assumption you should make about the array is that element 0 will be an empty string.

This method (unlike most of the others you will write for this problem) is static, because it doesnt need to access the fields of a Card object. Rather, it gets all of the information that it needs from its parameter and from the RANK_STRINGS array.

2. Add another static method called isValidSuit that takes a single-character representation of a cards suit and returns true if that suit is valid (i.e., if it is one of the values in the SUITS array), and false otherwise. For example:

isValidSuit('D') should return true, because 'D' appears in the SUITS array

isValidSuit('B') should return false, because 'B' does not appear in the SUITSarray.

Important: Your method should work even if we change the contents of the SUITS array. In other words, you should write code that processes any array of char values that is assigned to the class constant SUITS, and you should not make any assumptions about the chars found in that array.

Here again, this method is static, because it doesnt need to access the fields of a Card object. Rather, it gets all of the information that it needs from its parameter and the SUITS array.

Testing your static methods Before you proceed with the remaining steps, we highly recommend adding a main method to your Card class that makes test calls to the two static methods that you just implemented. Take whatever steps are needed to ensure that they work correctly.

3. Define the fields Each Card object should encapsulate two pieces of state:

the cards rank (an integer). For numeric cards, the rank is simply the number itself (e.g., 5 cards have a rank of 5). Aces have a rank of 1, Jacks a rank of 11, Queens a rank of 12, and Kings a rank of 13. Ranks less than 1 or greater than 13 will not be allowed.

the cards suit (a single character). The only allowable suits are the ones found in the SUITS array.

For example, here is what a Card object representing an Ace of Hearts would look like in memory:

 +----------------+ | +-----+ | | rank | 1 | | | +-----+ | | +-----+ | | suit | 'H' | | | +-----+ | +----------------+ 

For now, you only need to define the fields. Make sure to:

- use the field names shown above

- prevent them from being directly accessed by client code.

In subsequent sections, you will write constructors that assign values to the fields, and that ensure that only valid values are allowed.

4. implement the constructors In Java, a class can have more than one constructor; this provides clients of the class with different options for creating a new object of the class. All of the constructors must be named after the class, but their parameter lists must differ from each other in some way.

You should add the following two constructors to the Card class:

- a constructor that takes two parameters: an integer specifying the cards rank, and a single character (a char) specifying the cards suit (in that order). It should ensure that only valid values are assigned to the objects fields, as specified in part 3. Attempts to assign invalid values should produce an IllegalArgumentException.

- a constructor that takes a single parameter: a two- or three-character string that specifies the card to be created. For example, the client could pass in the string"KC" for a King of Clubs or the string "10S" for a 10 of Spades.

The last character of the input string represents the cards suit (e.g., 'C' or 'S') and the first one or two characters represent its rank (e.g., K or 10).

This constructor should use the specified string to determine the field values of the new object, and it should ensure that only valid values are assigned to the fields. Attempts to assign invalid values should produce an IllegalArgumentException. In addition, a parameter value of null should produce an IllegalArgumentException.

For example, here is some code that uses the two constructors:

Card c1 = new Card(1, 'H'); // Ace of Hearts Card c2 = new Card(12, 'D'); // Queen of Diamonds Card c3 = new Card("KC"); // King of Clubs Card c4 = new Card("10S"); // 10 of Spades 

Hint: Your constructors should take advantage of the static methods that you wrote for parts 1 and 2.

5. Implement some basic accessor methods Next, define the following initial set of instance methods. (Note that all of these methods are accessor methods. We will not implement any mutator methods, because we assume that a given Card objects rank and suit remain fixed once the object is created. The constructor will take care of assigning the initial values of those fields, and those values will not change.)

Make sure that your methods are non-static, because they need access to the fields in the called object. In addition, none of these methods should take an explicit parameter, because all of the information that they need is inside the called object.

Here are the methods:

getRank, which returns the integer representing the Card objects rank. For example, if c1 is the card shown in the diagram above, c1.getRank() should return 1.

getSuit, which returns the char representing the Card objects suit. For example, if c1 is the card shown in the diagram above, c1.getSuit() should return the char'H'.

isAce, which returns true if the Card is an Ace and false if it is not. To make your code more readable, we encourage you to use the class constant that we have provided for the rank of an Ace. The relevant constant is at the very beginning of the class.

isFaceCard, which returns true if the Card is a face card (Jack, Queen, or King) and and false if it is not. To make your code more readable, we encourage you to use the class constants that we have provided for the ranks of Jack, Queen and King.

getValue, which returns the Card objects value. If the card is a face card, then it should return a value of 10. Otherwise, it should return the cards rank. Hint: Use the isFaceCard() method to check if it is a face card!

Once you have completed your methods for parts 4 and 5, you can test them using the first client program that weve given you. See below for more detail.

6. Define a toString method Write a toString method that returns a String representation of the Card object that can be used when printing it or concatenating it to a String. We will discuss this type of method in lecture, and we provide an example in our Rectangle class. The returned String should consist of the cards rank string followed immediately by its suit. For example, if a Card object represents a 10 of Diamonds, this method should return "10D". If a Card object represents a Queen of Spades, this method should return "QS". This method must make use of the RANK_STRINGS array that we have given you.

7. Define methods for comparing Card objects Finally, define the following two instance methods for comparing Card objects:

sameSuitAs, which takes a Card object as a parameter and determines if it is has the same suit as the called object, returning true if they have the same suit and false if they do not have the same suit. If a value of null is passed in for the parameter, the method should return false.

equals, which takes a Card object as a parameter and determines if it is equivalent to the called object, returning true if it is equivalent and false if it is not equivalent. Two Card objects should be considered equivalent if their rank and suit are the same. If null is passed in for the parameter, the method should return false.

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

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions

Question

explain what is meant by the terms unitarism and pluralism

Answered: 1 week ago