Java Reference
In-Depth Information
37 winning.add(number);
38 }
39 return winning;
40 }
41
42 // reads the player's lottery ticket from the console
43 public static Set<Integer> getTicket() {
44 Set<Integer> ticket = new TreeSet<Integer>();
45 Scanner console = new Scanner(System.in);
46 System.out.print("Type " + NUMBERS + " lotto numbers: ");
47 while (ticket.size() < NUMBERS) {
48 int number = console.nextInt();
49 ticket.add(number);
50 }
51 return ticket;
52 }
53 }
Here's one example output from running the program:
Type 6 lotto numbers: 2 8 15 18 21 32
Your ticket was: [2, 8, 15, 18, 21, 32]
Winning numbers: [1, 3, 15, 16, 18, 39]
Matched numbers: [15, 18]
Your prize is $400.00
11.3 Maps
Consider the task of writing a telephone book program that allows users to type a
person's name and search for that person's phone number. You could store the data in
an array, a list, or a set. Perhaps you'd make a small class called PhoneBookRecord
that stores a person's name and phone number and a list to contain the
PhoneBookRecord objects. When you want to search for a phone number, you'd tra-
verse the list, looking for the PhoneBookRecord that matches the name entered by
the user and return the associated phone number.
A solution such as the one just described isn't very practical. If the list of records
is large, it would take the program a long time to look at each one to find the right
record to retrieve the phone number.
In many data-processing tasks, it's useful to link pairs of objects (such as a name
and a telephone number). We often find ourselves saying, “I'd like to associate every
A with a B.” Perhaps we'd like to associate names with addresses so that when the
user types a name, we can quickly look up that person's address. Or perhaps we want
to count the number of occurrences of every word in a book by associating each word
with its count of occurrences.
 
 
Search WWH ::




Custom Search