forked from haonlywan/CodeHS-Java-APCSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.8.10 Word Games (Part 2)
47 lines (36 loc) · 1.11 KB
/
2.8.10 Word Games (Part 2)
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
public class WordGames
{
private String word;
public WordGames(String text)
{
word = text;
}
public String scramble()
{
// switch first half
// and second half
String firstHalf = word.substring(0, (word.length()/2));
String secondHalf = word.substring((word.length()/2));
return secondHalf + firstHalf;
}
public String bananaSplit(int insertIdx, String insertText)
{
// Insert insertText at the position
// insertIdx
String modString = word.substring(0, insertIdx) + insertText + word.substring(insertIdx);
return modString;
}
public String bananaSplit(String insertChar, String insertText)
{
// Insert insertText after the first
// occurence of the insertChar
int insertIdx = word.indexOf(insertChar);
String modString = word.substring(0, insertIdx) + insertText + word.substring(insertIdx);
return modString;
}
public String toString()
{
// Games[word]
return "[" + word + "]";
}
}