Question
Here is the question: Design and implement a class called Card that represents a standard playing card. Each card has a suit and face value.
Here is the question:
Design and implement a class called Card that represents a standard playing card. Each card has a suit and face value. For your design, create a UML Class diagram similar to Figure 5.5 on page 180 of the textbook. Note that you need to include the two constructors in the methods section (i.e., they must be coded).
Hints: Represent the faces/ranks of Ace through King as 1 through 13 and the suits as 1 through 4. You need two constructors: one that receives a face/rank value and suit value as parameters, plus the default constructor where these values are randomly generated.
The face/rank and suit must both have appropriate get_ and set_ methods for the numeric values plus a get_ method for the textual equivalent (e.g., getFace() might return 13 while getFaceText() would return King). Your toString method should return a nice representation of the values like Ace of Spades or Nine of Hearts. Write a test driver that creates five random cards (i.e., uses the default constructor) and outputs them. Then creates five more cards of specific face/rank and suit values (i.e., uses the first constructor) and outputs them. The specific cards should include two with boundary values: for example, face 1, suit 1 and face 13, suit 4. The final card should use invalid face and suit values, such as 15 and 5. You should not need to prompt the user for anything, just hard-code the calls like: Card card6 = new Card(1,1);
Here is my code:
import java.util.Random;
class Card{ //class Card // public final static int HEARTS = 1; // public final static int DIAMONDS = 2; // public final static int CLUBS = 3; // public final static int SPADES = 4; private final static String[] faces={" ", "Ace", "Two", "Three", "Four", "Five" , "Six", "Seven", "Eight", "Nine", "Ten", "Jack" , "Queen", "King"}; private final static String[] suits={" ", "Hearts", "Clubs", "Diamonds", "Spades"};
private int suit; private int face;
// Default constructor public Card(){ Random rand = new Random(); // Assingning random values in the specified range face = rand.nextInt(13) + 1; suit = rand.nextInt(4) + 1; }
// Parameterised constructor public Card(int face, int suit){ // It sets the values only if the values are in the specified range if(face >= 1 && face <= 13) this.face = face; //else //throw new IllegalArgumentException("The face value should be in range [1 13].");
if(suit >= 1 && suit <= 4) this.suit = suit; // else // throw new IllegalArgumentException("The suit value should be in range [1 4]."); }
public int getFace(){ return face; }
// Returns the text of face public String getFaceText(){ return faces[face]; }
public int getSuit(){ return suit; }
public void setFace(int face){ if(face >= 1 && face <= 13) this.face = face; // else // throw new IllegalArgumentException("The face value should be in range [1 13]."); }
public void setSuit(int suit){ if(suit >= 1 && suit <= 4) this.suit = suit; // else // throw new IllegalArgumentException("The suit value should be in range [1 4]."); }
//toString method @Override public String toString(){ return faces[face] + " of " + suits[suit]; } }
public class Assignment4Q2 { public static void main(String[] args) { //creating 5 card objects using default constructor Card card1 = new Card(); Card card2 = new Card(); Card card3 = new Card(); Card card4 = new Card(); Card card5 = new Card(); //printing the toString method of each System.out.println(card1); System.out.println(card2); System.out.println(card3); System.out.println(card4); System.out.println(card5); //creating 5 Card objects using parameterised constructor Card card6 = new Card(1, 1); Card card7 = new Card(13, 4); Card card8 = new Card(11, 2); Card card9 = new Card(8, 3); Card card10 = new Card(15, 5); //printing the toString method of each System.out.println(card6); System.out.println(card7); System.out.println(card8); System.out.println(card9); System.out.println(card10); } }
Please fix any issues in my code or comment if it's all correct. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started