This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSomePracticeMethods.java
More file actions
194 lines (181 loc) · 6.51 KB
/
Copy pathSomePracticeMethods.java
File metadata and controls
194 lines (181 loc) · 6.51 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Handell Desulme
// 2/17/2019
// Assignment 4. practice with static methods and strings
//
// This program contains multiple string algorithms in static methods to solve a variety of problems.
// NOTE: I made the last method case sensitive because of the example in the comment & there was no JUnit test to specify otherwise.
public class SomePracticeMethods {
public static void main(String args[]){
char c = 'M';
String s = "soMething";
String t = "pleemt";
String no = "ooooo";
String reverse = "Temple";
String palindrome = "racecar";
String h = "Mississippi";
String n = "ss";
String base = "lightbugning";
String suffix = "bug";
System.out.println(inTU(c)); //true
System.out.println(indexOfFirstTULetter(s)); //2
System.out.println(indexOfFirstTULetter("elephant", 2)); //2
System.out.println(indexOfLastTULetter(s)); //4
System.out.println(allTempleLetters(t)); //true
System.out.println(noTempleLetters(no)); //true
System.out.println(withoutTULetters(s)); //"sohing"
System.out.println(reversed(reverse)); //"elpmeT"
System.out.println(numOccurrences(h, n)); //2
System.out.println(sameInReverse(palindrome)); //true
System.out.println(appendIfMissing(base, suffix)); //"lightbugningbug"
}
/*
* returns true if c is a letter in the word "temple" or false otherwise
*/
public static boolean inTU(char c) {
String temple = "temple";
for (int i = 0; i < temple.length(); i++){
if (c==temple.toUpperCase().charAt(i) || c==temple.toLowerCase().charAt(i)){
return true;
}
}
return false;
}
/*
* returns the index of the first occurrence of in s
* of a letter in the word "temple" or
* -1 if s contains no letters in the word "temple"
*/
public static int indexOfFirstTULetter(String s) {
String temple = "temple";
for (int i = 0; i < s.length(); i++){
for (int j = 0; j < temple.length(); j++){
if (s.charAt(i)==temple.toUpperCase().charAt(j) || s.charAt(i)==temple.toLowerCase().charAt(j)) {
return s.indexOf(s.charAt(i));
}
}
}
return -1;
}
/*
* returns the index of the first occurrence of a letter in "temple" in s starting
* from index startPosition or -1 if there are none at index
* startPosition or later. Notice that this method has the same name as the
* previous one, but that it takes a different number of arguments. This is
* perfectly legal in Java. It's called "method overloading"
*/
public static int indexOfFirstTULetter(String s, int startPosition) {
String temple = "temple";
for (int i = startPosition; i < s.length(); i++){
for (int j = 0; j < temple.length(); j++){
if (s.charAt(i)==temple.toUpperCase().charAt(j) || s.charAt(i)==temple.toLowerCase().charAt(j)){
return s.length() - s.substring(i).length();
}
}
}
return -1;
}
/*
* returns the index of the last occurrence of a letter in the word "temple"
* in s or -1 if s
* contains none
*/
public static int indexOfLastTULetter(String s) {
String temple = "temple";
for (int i = s.length()-1; i >= 0; i--){
for (int j = 0; j < temple.length(); j++){
if (s.charAt(i)==temple.toUpperCase().charAt(j) || s.charAt(i)==temple.toLowerCase().charAt(j)){
return s.indexOf(s.charAt(i), i);
}
}
}
return -1;
}
/* returns true if every letter in s is a letter
* in the word "temple" or false otherwise */
public static boolean allTempleLetters(String s) {
for (int i = 0; i < s.length(); i++){
if (inTU(s.charAt(i))==false) {
return false;
}
}
return true;
}
/* returns true if no letter in s is a letter
* in the word "temple" or false otherwise */
public static boolean noTempleLetters(String s) {
for (int i = 0; i < s.length(); i++){
if (inTU(s.charAt(i))==true) {
return false;
}
}
return true;
}
/*
* returns a new String which is the same as s, but with all of the letters
* in the word "temple" removed.
*/
public static String withoutTULetters(String s) {
String newString = "";
for (int i = 0; i < s.length(); i++){
if (inTU(s.charAt(i))==false) {
newString += s.charAt(i);
}
}
return newString;
}
/*
* returns s in reverse. For example, if s is "Temple", the method returns the
* String "elpmeT"
*/
public static String reversed(String s) {
String newString = "";
for (int i = s.length()-1; i >= 0; i--){
newString += s.charAt(i);
}
return newString;
}
/*
* returns the number of times that n occurs in h. For example, if h is
* "Mississippi" and n is "ss" the method returns 2.
*/
public static int numOccurrences(String h, String n) {
int occurrences = 0;
if (h.indexOf(n)==-1){
return occurrences; //returns 0
} else {
occurrences++;
return occurrences += numOccurrences(h.substring(h.indexOf(n)+n.length()),n); //returns 1 + recursive total
}
}
/*
* returns true if s is the same backwards and forwards and false otherwise
*/
public static boolean sameInReverse(String s) {
String newString = "";
for (int i = s.length()-1; i >= 0; i--){
newString += s.charAt(i);
}
if (s.equals(newString)){
return true;
} else {
return false;
}
}
/*
* Returns a new String that looks like base appended with suffix. If base
* already ends with suffix, it returns base.
*
* For example, if base is "lightning" and suffix is "bug", returns
* "lightningbug".
*
* If base is "lightningbug" and suffix is "bug", it also returns
* "lightningbug".
*/
public static String appendIfMissing(String base, String suffix) {
String newString = base;
if (base.lastIndexOf(suffix)==base.length()-suffix.length()) {
return newString;
}
return newString+=suffix;
}
}