Java Reference
In-Depth Information
Terminology
The terms generic class and parameterized class mean the same thing, namely a class with
one or more type parameters.
Display 14.6 Using Our Ordered Pair Class (part 1 of 2)
1
import java.util.Scanner;
2 public class GenericPairDemo
3{
4 public static void main(String[] args)
5
{
6
Pair<String> secretPair =
7
new Pair<String>("Happy", "Day");
8
9
Scanner keyboard = new Scanner(System.in);
10
System.out.println("Enter two words:");
11
String word1 = keyboard.next();
12
String word2 = keyboard.next();
13
Pair<String> inputPair =
14
new Pair<String>(word1, word2);
15
if (inputPair.equals(secretPair))
16
{
17
System.out.println("You guessed the secret words");
18
System.out.println("in the correct order!");
19
}
20
else
21
{
22
System.out.println("You guessed incorrectly.");
23
System.out.println("You guessed");
24
System.out.println(inputPair);
25
System.out.println("The secret words are");
26
System.out.println(secretPair);
27
}
28
}
29
}
Sample Dialogue
Enter two words:
two words
You guessed incorrectly.
You guessed
first: two
Search WWH ::




Custom Search