1. Playing cards are used in many computer games, including versions of such cla
ID: 3747296 • Letter: 1
Question
1. Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. Design a Card class that contains a character data field to hold a suit (s for spades, h for hearts, d for diamonds, or c for clubs) and an integer data field for a value from 1 to 13. (When you learn more about string handling in the chapter Characters, Strings, and the StringBuilder, you can modify the class to hold words for the suits, such as spades or hearts, as well as words for some of the values - for example, ace or kind.) Include get and set methods for each field. Save the class as Card.java.
Write an application that randomly selects two playing cards and displays their values. Simply assign a suit to each of the cards, but generate a random number for each card's value. To fully understand the process, you must learn more about Java classes and methods. However, for now, you can copy the following statements to generate a random number between 1 and 13 and assign it to a variable:
final int CARDS_IN_SUIT = 13;
myValue = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1);
After reading the chapter Making Decisions, you will be able to have the game determine the higher card. For now, just observe how the card values change as you execute the program multiple times. Save the application as PickTwoCards.java.
Explanation / Answer
Card.java
//Designing the Card class
public class Card
{
private String suit; /**suit data member of Card class*/
private String number; /**number data membar of Card class*/
public Card()
{
suit = "Spades"; /**Default Constructor for class Card*/
number = "Jack";
}
/**
* Member function that returns
* number(data member) value.
*/
public String getCardNum()
{
return number;
}
/**
* Member function that returns
* suit(data member) value.
*/
public String getCardSuit()
{
return suit;
}
/**
* Member function that sets the value
* of number(data member) depending upon the
* supplied parameter
*/
public void setCardNum(String number)
{
this.number = number;
}
/**
* Member function that sets the value
* of suit(data member) depending upon the
* supplied parameter
*/
public void setCardSuit(String suit)
{
this.suit = suit;
}
}
PickTwoCards.java
import javax.swing.JOptionPane; //to display a dialog box to the user.
import java.lang.NullPointerException; //for handling NULL pointer.
/**
* generates two random cards from a 52 deck playing set
*/
public class PickTwoCards
{
/**
* first Card object
*/
private Card Card1;
/**
* second Card object
*/
private Card Card2;
/**
* suit field for the first Card object (Card1)
*/
private String suit1;
/**
* suit field for the first Card object (Card2)
*/
private String suit2;
/**
* number field for the first Card object (Card1)
*/
private String number1;
/**
* number field for the first Card object (Card2)
*/
private String number2;
public static void main(String[] args)
{
//reversing order of the next 2 methods in order to trigger the null pointer
PickTwoCards RandomCards = new PickTwoCards();
RandomCards.genTwoCards();
RandomCards.displayCards();
}
/**
* Constructor for the PickTwoCards class
*/
public PickTwoCards()
{
number1 = "One";
suit1 = "Spades";
number2 = "Two";
suit2 = "Diamonds";
}
/**
* Instantiating 2 card objects (Card1 and Card2). Generating a random number
* and random suit for both Card1 and Card2 and sending those values to the
* setting methods in the Card class
*/
public void genTwoCards()
{
Card1 = new Card();
Card2 = new Card();
// generates random int and converts to type String (suit)
String[] suitArray = new String[]{"Clubs","Hearts","Spades","Diamonds"};
int s1 = (int)(Math.random()*suitArray.length);
int s2 = (int)(Math.random()*suitArray.length);
suit1 = suitArray[s1];
suit2 = suitArray[s2];
Card1.setCardSuit(suit1);
Card2.setCardSuit(suit2);
// generates random int and converts to type String (number)
String[] numArray = new String[]{"Ace","Two","Three","Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int r1 = (int)(Math.random()*numArray.length);
int r2 = (int)(Math.random()*numArray.length);
number1 = numArray[r1];
number2 = numArray[r2];
Card1.setCardNum(number1);
Card2.setCardNum(number2);
}
/**
* Displaying the random cards using the JOptionPane method
*/
public void displayCards()
{
try
{
JOptionPane.showMessageDialog(null,"The first card is the " + Card1.getCardNum() + " of " + Card1.getCardSuit());
JOptionPane.showMessageDialog(null,"The second card is the " + Card2.getCardNum() + " of " + Card2.getCardSuit());
}
catch(NullPointerException e)
{
System.err.println(e.getMessage());
}
finally
{
System.out.println("Exception handling");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.