Java Reference
In-Depth Information
Display 14.5
A Generic Ordered Pair Class (part 2 of 2)
37 public boolean equals(Object otherObject)
38 {
39 if (otherObject = = null )
40 return false;
41 else if (getClass() != otherObject.getClass())
42 return false ;
43 else
44 {
45 Pair<T> otherPair = (Pair<T>)otherObject;
46 return (first.equals(otherPair.first)
47 && second.equals(otherPair.second));
48 }
49 }
50 }
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!");
(continued)
 
Search WWH ::




Custom Search