Write an immutable class named Card to represent a playing card object. Its priv
ID: 3560062 • Letter: W
Question
Write an immutable class named Card to represent a playing card object. Its private instance variables should be an int rank and a String suit. You should have only one constructor that takes a rank and a suit as arguments. Your constructor should check if the rank and suit are valid! You should have methods that access the instance variables values (i.e. getter methods). Also, since we may want to compare cards, you should write three comparison methods (equals, greaterThan, lessThan as in the lab), following this order: compare by rank first (i.e. 2Explanation / Answer
// Card Class declaration
class Card{
// Variables of Class Card
private int rank;
private String suit;
// Constructor to assign values to member of Card object
Card(int r, String s){
rank = r;
suit = s;
}
// get method for class member 'rank'
int getRank(){
return rank;
}
// get member for class member 'suit'
String getSuit(){
return suit;
}
/*method to check whether the card is equal to the parameter card or not*/
boolean equals(Card c){
if(this.rank == c.rank && this.suit.equalsIgnoreCase(c.suit)) return true;
else return false;
}
/*method to check whether the card has more value to the parameter card or not.*/
boolean greaterThan(Card x){
if(this.rank < x.rank) return true;
if(this.rank > x.rank) return false;
String suits[] = {"spade", "heart", "diamond", "club"};
int t = 0, xx = 0;
for(int i = 0; i < 4; i++){
if(this.suit.equalsIgnoreCase(suits[i])) t = i;
if(x.suit.equalsIgnoreCase(suits[i])) xx = i;
}
if(t > xx) return true;
else return false;
}
/*method to check whether the card has less value to the parameter card or not*/
boolean lessThan(Card x){
if(this.rank < x.rank) return true;
if(this.rank > x.rank) return false;
// String array to store the suit names
String suits[] = {"spade", "heart", "diamond", "club"};
int t = 0, xx = 0;
for(int i = 0; i < 4; i++){
if(this.suit.equalsIgnoreCase(suits[i])) t = i;
if(x.suit.equalsIgnoreCase(suits[i])) xx = i;
}
if(t < xx) return true;
else return false;
}
}
// import package Random to generate random numbers
import java.util.Random;
// Class declaration of CardTester class
public class CardTester{
// main method starts
public static void main(String args[]){
// Create 2 card arrays to use as 'hand', each of 5 Card elements
Card hand1[] = new Card[5];
Card hand2[] = new Card[5];
/* variables to count the number of 'greaterThan of each hand, and to generate random numbers' */
int count1 = 0, count2 = 0, r, s;
String suits[] = {"spade", "heart", "diamond", "club"};
// Random class object to generate random numbers
Random in = new Random();
// loop to generate 2 hands randomly
for(int i = 0; i < 5; i++){
r = 2 + in.nextInt(13);
s = in.nextInt(4);
hand1[i] = new Card(r, suits[s]);
r = 2 + in.nextInt(13);
s = in.nextInt(4);
hand2[i] = new Card(r, suits[s]);
}
// 2 for loops. Each to print a 'hand'
for(int i = 0; i < 5; i++){
System.out.print(hand1[i].getRank() + "," + hand1[i].getSuit() + " ");
}
System.out.println();
for(int i = 0; i < 5; i++){
System.out.print(hand2[i].getRank() + "," + hand2[i].getSuit() + " ");
}
/* Compare each card of hand 1 with each card of hand 2, and store the number of greaterThans of each hand */
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++)
if(hand1[i].greaterThan(hand2[j])) count1++;
for(int j = 0; j < 5; j++)
if(hand1[i].lessThan(hand2[j])) count2++;
}
// Print the number of greaterThans of each hand and decide the better hand
System.out.println(" The number of greaterThan of hand 1: " + count1);
System.out.println("The number of greaterThan of hand 2: " + count2);
if(count1 > count2) System.out.println(" Hand 1 is better.");
else if(count1 < count2) System.out.println(" Hand 2 is better.");
else System.out.println(" Both hands are equal.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.