/* Driver.java * The purpose of this program is to practice with an array of objects. * In this program, the array will be a deck of cards. * 1. Read a file containing a list of 52 shuffled cards into an array of Card objects. * 2. Ask the user to enter some (e.g. five) numbers 1-52. * For each number, find the corresponding card in the array. * Once you know what this card is, find out if it's a heart and/or a face card. * 3. Output the total number of hearts found and face cards. * Note: We don't do any random numbers or shuffling in this program. We assume * the input file has a shuffled deck. */ import java.util.Scanner; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Driver { public static void main(String [] args) throws FileNotFoundException { Scanner inFile = new Scanner(new FileInputStream("cards.txt")); Scanner kbd = new Scanner(System.in); // Create array of 52 cards. Card [] deck = new Card [52]; // Read input file. Each string represents a card like "Q-d", with a // denomination, a hyphen, and a suit. Create a card object one at a time // and put it into the array. int index = 0; while (index < 52) { String cardString = inFile.next(); char denom = cardString.charAt(0); char suit = cardString.charAt(2); ////////////////////////////////////////////////////////////////// // Enter statement(s) here to create a card object and put it // into the array. ////////////////////////////////////////////////////////////////// ++index; } // Now, ask the user for some numbers in the range 1-52. For example, // the user might type in the numbers 30, 5 and 16. // For each number the user enters, find the corresponding card in the // deck. Then ask if this card is a heart. Ask if this card is a face card. // Keep track of the total number of hearts and face cards. int numHearts = 0; int numFaceCards = 0; System.out.printf("Enter five numbers (1-52): "); // Let's read a whole line of input. // The loop below will read one integer at a time out of this // input string. String inputLine = kbd.nextLine(); Scanner scanLine = new Scanner(inputLine); index = 0; while (index < 5) { // read an integer from the scanLine // Retrieve a card object from the array based on this value. // Note that you don't call a constructor here. You are not // creating a "new" card, just retrieving one from the array. // Print out this card by calling the card's toString(). // If this card is a heart, increment heart counter. // If this card is a face card, increment face-card counter. ++index; } // Final output - the number of hearts & face cards. System.out.printf("I found %d hearts and %d face cards.\n", numHearts, numFaceCards); } }