-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP2for2018.java
More file actions
101 lines (73 loc) · 2.1 KB
/
P2for2018.java
File metadata and controls
101 lines (73 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.ArrayList;
public class P2for2018 {
public static void main(String [] args){
String[] wordsAll_1 = {"777", "0000", "999", "6666", "5555"};
String[] wordsAll_2 = {"the", "red", "fox", "the", "red"};
WordPairList wpl = new WordPairList(wordsAll_2);
for(WordPair wp: wpl.getAllPairs()){
System.out.println(wp);
}
System.out.println(wpl.numMatches());
System.out.println(wpl.countMatch(new WordPair("RED", "Fox")));
}
}
class WordPair{
private String first;
private String second;
public WordPair(String first, String second) {
this.first = first;
this.second = second;
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
@Override
public boolean equals(Object object){
String first = ((WordPair)object).getFirst();
String second = ((WordPair)object).getSecond();
if (first.equalsIgnoreCase(this.first) && second.equalsIgnoreCase(this.second)){
return true;
}
return false;
}
public String getFirst() {
return first;
}
public String getSecond() {
return second;
}
}
class WordPairList{
private ArrayList<WordPair> allPairs;
public WordPairList(String[] words){
allPairs = new ArrayList<WordPair>();
for(int i = 0; i < words.length; i++){
for(int j = i + 1; j < words.length;j++){
allPairs.add(new WordPair(words[i], words[j]));
}
}
return;
}
public ArrayList<WordPair> getAllPairs() {
return allPairs;
}
public int numMatches(){
int numCount = 0;
for(WordPair wp : allPairs){
if (wp.getFirst().equals(wp.getSecond())){
numCount++;
}
}
return numCount;
}
public int countMatch(WordPair wordpair){
int ct = 0;
for (WordPair wp : allPairs){
if(wp.equals(wordpair)){
ct++;
}
}
return ct;
}
}