Homework Problem: You are creating a card game where you will ask the user to se
ID: 3830834 • Letter: H
Question
Homework
Problem:
You are creating a card game where you will ask the user to select a suit where
Suit> 1=Hearts, 2=Diamonds, 3=Clubs, 4=Spades
The objective is for you to fill the users hand with 5 unique cards of the suit that they selected.
To make this happen you will need to create a class called Hand
Hand will consist of the following:
An instance variable: suit
An instance variable: card
A constructor method that sets suit = 4 and card = 13
Code that imports java.util.Random and creates a Random object for use in the methods below
A drawSuit method that returns a randomly generated integer from 1 to 4 using the suit instance variable to generate the range for your random integer
A drawCard method that returns a randomly generated integer from 2 to 14 using the card instance variable to generate the range for your random integer
(2=2, 3=3,….10=10, 11=jack,12=queen,13=king,14=ace)
Explanation / Answer
below is your code: -
Hand.java
//Note That I have overridden equal and hashcode here because I need to insert distinct cards only and to compare
//two cards, equal and hashcode is overridden
import java.util.Random;
public class Hand {
private int suit;
private int card;
Hand() {
this.suit = 4;
this.card = 13;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
public int getCard() {
return card;
}
public void setCard(int card) {
this.card = card;
}
public int drawSuit() {
Random rand = new Random();
int suitSelected = rand.nextInt(4 - 1 + 1) + 1; // selecting random
// from 1 to 4
return suitSelected;
}
public int drawCard() {
Random rand = new Random();
int cardSelected = rand.nextInt(14 - 2 + 1) + 2; // selecting random
// from 2 to 14
return cardSelected;
}
public void printCardDetails() {
String cardS = "", suitS = "";
for (int i = 2; i <= 14; i++) {
if (this.card == i) {
if (i == 11) {
cardS = "jack";
} else if (i == 12) {
cardS = "queen";
} else if (i == 13) {
cardS = "king";
} else if (i == 14) {
cardS = "ace";
} else {
cardS = i + "";
}
}
}
for (int i = 1; i <= 4; i++) {
if (this.suit == i) {
if (i == 1) {
suitS = "Hearts";
} else if (i == 2) {
suitS = "Diamonds";
} else if (i == 3) {
suitS = "Clubs";
} else if (i == 4) {
suitS = "Spades";
}
}
}
System.out.println("Card: "+cardS+" of "+suitS);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + card;
result = prime * result + suit;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Hand other = (Hand) obj;
if (card != other.card)
return false;
if (suit != other.suit)
return false;
return true;
}
}
Driver.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please select the suit: 1=Hearts, 2=Diamonds, 3=Clubs, 4=Spades.:");
int suitSelected = sc.nextInt();
while (suitSelected <= 1 && suitSelected >= 4) { // to check if suit selected is valid.
suitSelected = sc.nextInt();
}
sc.close();
ArrayList<Hand> hand = new ArrayList<>();
Hand h;
int cardSelected;
for (int i = 0; i < 5; i++) {
h = new Hand();
cardSelected =h.drawCard();
h.setCard(cardSelected);
h.setSuit(suitSelected);
while(hand.contains(h)) { // to insert distinct card always, check if card is already present.
cardSelected =h.drawCard();
h.setCard(cardSelected);
h.setSuit(suitSelected);
}
hand.add(h);
}
System.out.println("Current Hand:");
showHands(hand);// print current hand details.
}
//method to print the current hand.
public static void showHands(ArrayList<Hand> hand) {
Iterator<Hand> i = hand.iterator();
Hand h;
while(i.hasNext()) {// If hand has still a card
h = i.next();
h.printCardDetails();
}
}
}
Sample Output
1.
Please select the suit: 1=Hearts, 2=Diamonds, 3=Clubs, 4=Spades.:
4
Current Hand:
Card: queen of Spades
Card: king of Spades
Card: 5 of Spades
Card: 2 of Spades
Card: 8 of Spades
2.
Please select the suit: 1=Hearts, 2=Diamonds, 3=Clubs, 4=Spades.:
2
Current Hand:
Card: 8 of Diamonds
Card: 2 of Diamonds
Card: jack of Diamonds
Card: 3 of Diamonds
Card: 5 of Diamonds
3.
Please select the suit: 1=Hearts, 2=Diamonds, 3=Clubs, 4=Spades.:
1
Current Hand:
Card: 4 of Hearts
Card: 5 of Hearts
Card: queen of Hearts
Card: 7 of Hearts
Card: 6 of Hearts
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.