Create a class call Card.java , it should contain at least 2 variables one is su
ID: 3901958 • Letter: C
Question
Create a class call Card.java, it should contain at least 2 variables one is suit(“Spade” , ”Heart”, “Diamond”, “Club”) and the other one is rank(1~13). Please new 26 card instances using the followings:
// You should know how to use the following code to achieve what you want.
import java.util.*;
Card cards[] = new Card[26];
String suit[] = {“Spade” , ”Heart”, “Diamond”, “Club”};
Random r = new Random[52];
for(int i = 0; i < cards.length; i++){
cards[i] = new Card(r.nextInt(13)+1, suit[r.nextInt(100)%4]);
//You should design the above constructor inside the Card.java
}
After this, please sort the array cards. To compare two cards, first of all you have to compare their ranks. If they have the same rank, you have to then compare their suit (“Spade” > ”Heart” > “Diamond”, > “Club”). If two cards are equal to each other, just put them together. Show the array just sorted
Explanation / Answer
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
Assumption
I am assuming after sorting Spade will come first, then heart, then Diamond and then Club.
Card.java
public class Card implements Comparable<Card> {
// INSTANCE VARIABLES
private String suit; // Suit of a card, it goes between Diamond, Spade, Club
// or Heart
private int rank; // Rank of a card, it goes between 1 till 13 (In order of
// A, 2, 3,..., 10, J, Q, K)
// CONSTRUCTORS
public Card(int rank, String suit) {
this.suit = suit;
this.rank = rank;
}
public Card(Card otherC) {
// Add code
this.rank = otherC.getRank();
this.suit = otherC.getSuit();
}
// OVERRIDED METHODS
@Override
public String toString() {
return "[" + rank + "," + suit + "]";
}
@Override
public boolean equals(Object c2) {
if (!(c2 instanceof Card)) {
throw new RuntimeException("Illegal argument to Card.equals()");
}
Card card2 = (Card) c2;
return ((this.getSuit().equals(card2.getSuit())) && // this.suit same as
// this.getsuit() ??
(this.getRank() == card2.getRank()));
}
// GETTERS AND SETTERS
public String getSuit() {
return suit;
}
public int getRank() {
return rank;
}
public void setSuit(String suit) {
this.suit = suit;
}
public void setRank(int rank) {
this.rank = rank;
}
public int compareTo(Card c) {
if (this.rank > c.rank) {
return 1;
} else if (this.rank < c.rank) {
return -1;
} else {
return c.suit.compareTo(this.suit);
}
}
}
CardMain.java
public class CardMain {
public static void main(String[] args) {
Card cards[] = new Card[26];
String suit[] = { "Spade", "Heart", "Diamond", "Club" };
Random r = new Random();
for (int i = 0; i < cards.length; i++) {
cards[i] = new Card(r.nextInt(13) + 1, suit[r.nextInt(100) % 4]);
// You should design the above constructor inside the Card.java
}
System.out.println("Before sorting: ");
for (int i = 0; i < cards.length; i++) {
System.out.println(cards[i]);
}
sort(cards);
System.out.println(" ");
System.out.println("After sorting: ");
for (int i = 0; i < cards.length; i++) {
System.out.println(cards[i]);
}
}
public static void sort(Card arr[]) {
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j].compareTo(arr[min_idx]) < 0)
min_idx = j;
// Swap the found minimum element with the first
// element
Card temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
Output
Before sorting:
[1,Diamond]
[11,Spade]
[10,Heart]
[9,Diamond]
[5,Diamond]
[11,Heart]
[11,Diamond]
[7,Diamond]
[5,Spade]
[8,Club]
[12,Diamond]
[1,Spade]
[7,Club]
[4,Diamond]
[7,Club]
[1,Club]
[13,Spade]
[13,Club]
[10,Spade]
[9,Heart]
[8,Spade]
[10,Heart]
[12,Spade]
[5,Diamond]
[13,Diamond]
[11,Heart]
After sorting:
[1,Spade]
[1,Diamond]
[1,Club]
[4,Diamond]
[5,Spade]
[5,Diamond]
[5,Diamond]
[7,Diamond]
[7,Club]
[7,Club]
[8,Spade]
[8,Club]
[9,Heart]
[9,Diamond]
[10,Spade]
[10,Heart]
[10,Heart]
[11,Spade]
[11,Heart]
[11,Heart]
[11,Diamond]
[12,Spade]
[12,Diamond]
[13,Spade]
[13,Diamond]
[13,Club]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.