can this code be converted to using generic ? dont change the output just conver
ID: 3798630 • Letter: C
Question
can this code be converted to using generic ?
dont change the output just convert to GENERIC
im guessing you must use array list or list , but the output is fine
public class CardsDeck {
private static String[] SUITS = {"Diamonds", "Hearts", "Clubs", "Spades"};
private static String[] RANKS = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
public CardsDeck() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
String[] deck = new String[RANKS.length * SUITS.length];
for (int i = 0; i < RANKS.length; i++){
for (int j = 0; j < SUITS.length; j++){
deck[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
}
}
int n = deck.length;
for (int i = 0; i < n; i++) {
int r = (int) (Math.random() * n);
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
int index = 1;
for (int i = 0; i < RANKS.length; i++){
for (int j = 0; j < SUITS.length; j++){
System.out.println(index++ +". "+deck[SUITS.length*i + j]);
}
}
findMinAndMax(deck,0,false,false);
}
public static void findMinAndMax(String[] deck, int index, boolean minFound, boolean maxFound){
if(deck[index].equalsIgnoreCase("King of Spades")){
System.out.println("highest card is in position number: "+(index+1));
maxFound = true;
}
else if(deck[index].equalsIgnoreCase("Ace of Diamonds")){
System.out.println("highest card is in position number: "+(index+1));
minFound = true;
}
if(minFound && maxFound){
return;
}
else{
findMinAndMax(deck,index+1,minFound,maxFound);
}
}
}
Output1:
1. 7 of Diamonds
2. 10 of Spades
3. 6 of Diamonds
4. 4 of Hearts
5. 10 of Clubs
6. 3 of Hearts
7. 9 of Spades
8. 4 of Clubs
9. Ace of Hearts
10. Ace of Clubs
11. 3 of Clubs
12. 7 of Hearts
13. Ace of Diamonds
14. 2 of Hearts
15. 5 of Clubs
16. 2 of Spades
17. King of Hearts
18. 10 of Diamonds
19. 9 of Diamonds
20. 4 of Spades
21. 10 of Hearts
22. Queen of Diamonds
23. 3 of Spades
24. 5 of Hearts
25. Queen of Spades
26. 5 of Diamonds
27. 3 of Diamonds
28. 6 of Spades
29. Ace of Spades
30. 8 of Diamonds
31. Jack of Spades
32. 6 of Clubs
33. 2 of Clubs
34. 4 of Diamonds
35. Jack of Diamonds
36. 7 of Spades
37. 8 of Spades
38. 8 of Clubs
39. King of Spades
40. 6 of Hearts
41. Queen of Hearts
42. 7 of Clubs
43. 9 of Hearts
44. King of Diamonds
45. Jack of Hearts
46. 9 of Clubs
47. 8 of Hearts
48. King of Clubs
49. 2 of Diamonds
50. 5 of Spades
51. Jack of Clubs
52. Queen of Clubs
highest card is in position number: 13
lowest card is in position number: 39
Explanation / Answer
A)
public class CardsDeck {
private static String[] SUITS = {"Diamonds", "Hearts", "Clubs", "Spades"};
private static String[] RANKS = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
List list = new ArrayList();
list.add("Hearts");
list.add("Diamonds");
list.add("Clubs");
list.add("Spades");
String s = (String) list.get(0);
public CardsDeck() {
}
public static void main(String[] args) {
String[] deck = new String[RANKS.length * SUITS.length];
for (int i = 0; i < RANKS.length; i++){
for (int j = 0; j < SUITS.length; j++){
deck[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
}
}
int n = deck.length;
for (int i = 0; i < n; i++) {
int r = (int) (Math.random() * n);
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
int index = 1;
for (int i = 0; i < RANKS.length; i++){
for (int j = 0; j < SUITS.length; j++){
System.out.println(index++ +". "+deck[SUITS.length*i + j]);
}
}
findMinAndMax(deck,0,false,false);
}
public static void findMinAndMax(String[] deck, int index, boolean minFound, boolean maxFound){
if(deck[index].equalsIgnoreCase("King of Spades")){
System.out.println("highest card is in position number: "+(index+1));
maxFound = true;
}
else if(deck[index].equalsIgnoreCase("Ace of Diamonds")){
System.out.println("highest card is in position number: "+(index+1));
minFound = true;
}
if(minFound && maxFound){
return;
}
else{
findMinAndMax(deck,index+1,minFound,maxFound);
}
}
}
Output1:
1. 7 of Diamonds
2. 10 of Spades
3. 6 of Diamonds
4. 4 of Hearts
5. 10 of Clubs
6. 3 of Hearts
7. 9 of Spades
8. 4 of Clubs
9. Ace of Hearts
10. Ace of Clubs
11. 3 of Clubs
12. 7 of Hearts
13. Ace of Diamonds
14. 2 of Hearts
15. 5 of Clubs
16. 2 of Spades
17. King of Hearts
18. 10 of Diamonds
19. 9 of Diamonds
20. 4 of Spades
21. 10 of Hearts
22. Queen of Diamonds
23. 3 of Spades
24. 5 of Hearts
25. Queen of Spades
26. 5 of Diamonds
27. 3 of Diamonds
28. 6 of Spades
29. Ace of Spades
30. 8 of Diamonds
31. Jack of Spades
32. 6 of Clubs
33. 2 of Clubs
34. 4 of Diamonds
35. Jack of Diamonds
36. 7 of Spades
37. 8 of Spades
38. 8 of Clubs
39. King of Spades
40. 6 of Hearts
41. Queen of Hearts
42. 7 of Clubs
43. 9 of Hearts
44. King of Diamonds
45. Jack of Hearts
46. 9 of Clubs
47. 8 of Hearts
48. King of Clubs
49. 2 of Diamonds
50. 5 of Spades
51. Jack of Clubs
52. Queen of Clubs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.