forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise9.java
More file actions
153 lines (144 loc) · 4.79 KB
/
Copy pathExercise9.java
File metadata and controls
153 lines (144 loc) · 4.79 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Exercise on encapsulation and generalization.
*/
public class Exercise9 {
public static void main(String[] args) {
System.out.println(encapsuleGeneralize("((3 + 7) * 2)"));
System.out.println(encapsuleGeneralize("(3 * 5) +6]"));
System.out.println(encapsuleGeneralize("Henry[the first one"));
System.out.println(encapsuleGeneralize("I didn't know what to say{it was disgusting}"));
System.out.println(isAbecedarian("abdest"));
System.out.println(isAbecedarian("abbey"));
System.out.println(isAbecedarian("cat"));
System.out.println(isDoubloon("Emmett"));
System.out.println(isDoubloon("catR"));
System.out.println(isAnagram("stop","pots"));
System.out.println(isAnagram("allen downey","well annoyed"));
System.out.println(isAnagram("crime","micro"));
System.out.println(canSpell("quijibo","jib"));
System.out.println(canSpell("Annabel","Annabelle"));
}
public static int encapsuleGeneralize(String s){
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(' || c == '[' || c == '{') {
count++;
} else if (c == ')' || c == ']' || c == '}') {
count--;
}
}
return count;
}
/**
* This method determines whether a word is abecedarian,
* so if the letters in the word appear in alphabetical order.
* @param s the word given
* @return true if the word is abecedarian,
* false otherwise
*/
public static boolean isAbecedarian(String s){
boolean isAbecedarian = true;
s = s.toLowerCase();
for(int i=1; i < s.length(); i++){
if(s.charAt(i-1) > s.charAt(i)){
isAbecedarian = false;
break;
}
}
return isAbecedarian;
}
/**
* This method checks whether a word is a "doubloon",
* so if every letter in the word appears exactly twice.
* @param s the word given
* @return true if the word is a doubloon,
* false otherwise
*/
public static boolean isDoubloon(String s){
boolean isDoubloon = false;
s = s.toLowerCase();
for(int i=0; i < s.length(); i++){
int count = 0;
for(int j=0;j < s.length(); j++){
if(s.charAt(i) == s.charAt(j)) {
count++;
if (count == 2) {
isDoubloon = true;
}
else if (count > 2) {
isDoubloon = false;
break;
}
}
}
}
return isDoubloon;
}
/**
* This method checks whether two words are anagrams
* of each other, so if they contain the same letters
* and the same number of each letter.
* @param s1 the first word given
* @param s2 the second word given
* @return true if the two words are anagrams of each other,
* false otherwise
*/
public static boolean isAnagram(String s1, String s2){
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
if(s1.length() != s2.length()){
return false;
}
boolean isAnagram = true;
for(int i=0; i < s1.length(); i++){
char c = s1.charAt(i);
int count = 0;
for(int k=0; k < s1.length(); k++){
if(s1.charAt(i)==s1.charAt(k))
count++;
}
for(int j = 0;j < s2.length(); j++){
if(s1.charAt(i) == s2.charAt(j)){
count --;
}
}
if(count != 0)
return !isAnagram;
}
return isAnagram;
}
/**
* This method checks whether the set of tiles
* with letters on them can spell the word given,
* like in the game 'Scrabble'.
* @param s the tiles with letters on them
* @param t the word to spell
* @return true if the tiles can spell the word,
* false otherwise
*/
public static boolean canSpell(String s,String t){
s = s.toLowerCase();
t = t.toLowerCase();
if(s.length() < t.length()){
return false;
}
boolean canSpell = true;
for(int i=0; i < s.length(); i++){
char c = s.charAt(i);
int count = 0;
for(int k=0; k < s.length(); k++){
if(s.charAt(i)==s.charAt(k))
count++;
}
for(int j = 0;j < t.length(); j++){
if(s.charAt(i) == t.charAt(j)){
count --;
}
}
if(count < 0)
return !canSpell;
}
return canSpell;
}
}