- Complete the previous exercises in Chapter 12 before completing this exercise.
- Open CardDriverTwo.java and add the following method.
public static int search(Card[] cards, Card target){
for(int i = 0; i < cards.length; i++){
if(cards[i].equals(target)){
return i;
}
}
return -1;
}
- Add the following to the main method.
int suit = Integer.parseInt(args[0]); int rank = Integer.parseInt(args[1]); Card theCard = new Card(suit,rank); System.out.println(theCard); int location = search(cards, theCard); System.out.println(cards[location]);
- Compile and run the program.
$ javac CardDriverTwo.java $ java CardDriverTwo 2 2 Ace of Clubs King of Spades 2 of Hearts 2 of Hearts $ java CardDriverTwo 13 2 Ace of Clubs King of Spades King of Hearts King of Hearts $ java CardDriverTwo 8 3 Ace of Clubs King of Spades 8 of Spades 8 of Spades
The complete class should appear as follows.
public class CardDriverTwo {
public static int search(Card[] cards, Card target){
for(int i = 0; i < cards.length; i++){
if(cards[i].equals(target)){
return i;
}
}
return -1;
}
public static void main(String[] args){
Card[] cards = new Card[52];
int index = 0;
for(int suit = 0; suit < 4; suit++)
{
for(int rank=1; rank <= 13; rank++)
{
cards[index] = new Card(rank,suit);
index++;
}
}
System.out.println(cards[0]);
System.out.println(cards[51]);
int suit = Integer.parseInt(args[0]);
int rank = Integer.parseInt(args[1]);
Card theCard = new Card(suit,rank);
System.out.println(theCard);
int location = search(cards, theCard);
System.out.println(cards[location]);
}
}


