Skip to content

Commit a88d4ac

Browse files
bluecrayon52bluecrayon52
bluecrayon52
authored and
bluecrayon52
committed
added comments, fixed ensureCapacity method
1 parent 0a57ad6 commit a88d4ac

File tree

1 file changed

+129
-37
lines changed

1 file changed

+129
-37
lines changed

MyString.java

+129-37
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* CSC230-02 Spring 2016
55
*/
66

7-
import java.util.Arrays;
7+
import java.util.Arrays; // used in equals method
88

99
public class MyString {
1010

@@ -21,46 +21,67 @@ public MyString() {
2121
public MyString(String str) {
2222
currLength = str.length();
2323
strChars = new char[currLength];
24-
ensureCapacity();
24+
2525
for (int i = 0; i < currLength; i++) {
2626
strChars[i] = str.charAt(i);
2727
}
28+
ensureCapacity();
2829
}
2930

3031
// Copy Constructor
3132
public MyString(MyString str) {
3233
currLength = str.currLength;
3334
strChars = new char[currLength];
34-
ensureCapacity();
35+
3536
for (int i = 0; i < currLength; i++) {
3637
strChars[i] = str.strChars[i];
38+
3739
}
38-
40+
ensureCapacity();
3941
}
4042

41-
// Length Method
43+
// Length Method, returns length of string in array, not last array index
4244
public int length() {
4345
return currLength;
4446
}
4547

4648
// Allocate additional memory for the string
4749
private void ensureCapacity() {
4850

49-
if(this.currLength == this.strChars.length){
50-
char[] cap = new char[this.currLength*2];
51+
// increase size of array
52+
char[] cap = new char[this.currLength + 1];
53+
54+
// pass values to temp array
55+
for(int i = 0; i < currLength; i++){
56+
cap[i] = strChars[i];
57+
}
58+
// new instance of strChars
59+
strChars = new char[this.currLength + 1];
60+
61+
//pass values back
62+
for(int a = 0; a < currLength; a++){
63+
strChars[a] = cap[a];
64+
}
65+
5166
}
5267

53-
}
68+
5469

5570
// return a string representation of the MyString object
5671
public String toString() {
5772

58-
if(strChars == null){
73+
if(strChars == null){ // for empty MyString
5974
String stringRep = new String();
6075
return stringRep;
6176
}
77+
78+
// copy array to trim null for printout
79+
char[] copy = new char[currLength];
6280

63-
String stringRep = new String(strChars);
81+
for(int i = 0; strChars[i] != '\u0000'; i++){
82+
copy[i] = strChars[i];
83+
}
84+
String stringRep = new String(copy);
6485
return stringRep;
6586
}
6687

@@ -70,19 +91,23 @@ public String toString() {
7091

7192
public MyString concat(MyString str) {
7293
int comboLength = str.currLength + currLength;
73-
char[] copyArr = new char[comboLength];
74-
75-
for (int i = 0; i < currLength; i++) {
76-
copyArr[i] = strChars[i];
94+
95+
// set length of new array
96+
char[] copyArr = new char[comboLength];
97+
98+
//place first (calling) string into array
99+
for (int i = 0; i < currLength; i++) {
100+
copyArr[i] = strChars[i];
77101
}
78-
102+
// place second (perameter) string into array
79103
for (int i = currLength, a = 0; i < comboLength; i++, a++) {
80-
copyArr[i] = str.strChars[a];
104+
copyArr[i] = str.strChars[a];
81105
}
82-
83-
String stringRep = new String(copyArr);
84-
85-
MyString Concat = new MyString(stringRep);
106+
// turn array into string
107+
String stringRep = new String(copyArr);
108+
109+
// pass string to MyString constructor
110+
MyString Concat = new MyString(stringRep);
86111

87112
return Concat;
88113
}
@@ -93,18 +118,24 @@ public MyString concat(MyString str) {
93118
public MyString concat(String str) {
94119
int strLength = str.length();
95120
int comboLength = strLength + currLength;
121+
122+
// set length of new array
96123
char[] copyArr = new char[comboLength];
97-
124+
125+
// place first (calling) string into array
98126
for (int i = 0; i < currLength; i++) {
99127
copyArr[i] = strChars[i];
100128
}
101-
129+
130+
//place second (peramter) string into array
102131
for (int i = currLength, a = 0; i < comboLength; i++, a++) {
103132
copyArr[i] = str.charAt(a);
104133
}
105-
134+
135+
// turn array into string
106136
String stringRep = new String(copyArr);
107-
137+
138+
//pass string to MyString constructor
108139
MyString Concat = new MyString(stringRep);
109140

110141
return Concat;
@@ -124,9 +155,12 @@ public boolean equals(MyString str) {
124155

125156
// or an alternative to importing java.util.Array for .equals method
126157
public boolean equalTo(MyString str) {
127-
158+
159+
// check for length equality
128160
if (str.currLength != currLength) {
129161
return false;
162+
163+
// traverse arrays for inequalities
130164
} else if (str.currLength == currLength) {
131165
for (int i = 0; i < currLength; i++) {
132166
if (strChars[i] != str.strChars[i]) {
@@ -143,36 +177,51 @@ public boolean equalTo(MyString str) {
143177
pos int if this MyString is alph. after perameter
144178
*/
145179
public int compareTo(MyString str) {
180+
181+
// determine shortest string for inbounds traversal indexing
146182
int length_diff = currLength - str.currLength;
147183
int charDiff = 0;
148-
184+
185+
// calling object is shorter
149186
if (length_diff < 0) {
150187
for (int i = 0; i < currLength; i++) {
188+
189+
// lower case using wrapper class
151190
charDiff = Character.toLowerCase(strChars[i])
152191
- Character.toLowerCase(str.strChars[i]);
192+
193+
//return first difference
153194
if (charDiff < 0 || charDiff > 0) {
154195
return charDiff;
155196
}
156197
}
157198
}
158-
199+
200+
// perameter is shorter or equality of lengths
159201
if (length_diff >= 0) {
160202
for (int i = 0; i < str.currLength; i++) {
203+
204+
// lower case using wrapper class
161205
charDiff = Character.toLowerCase(strChars[i])
162206
- Character.toLowerCase(str.strChars[i]);
207+
208+
// return first difference
163209
if (charDiff < 0 || charDiff > 0) {
164210
return charDiff;
165211
}
166212
}
167213
}
168-
214+
215+
// return no difference
169216
return charDiff;
170217
}
171218

172219
/* .get method, takes int, returns char at index location
173220
int must be in range!
174221
*/
175222
public char get(int index) {
223+
224+
// respond to out of bounds indexing
176225
if (index < 0 || index > currLength - 1) {
177226
System.out.println("index out of bounds!");
178227
return '?';
@@ -181,12 +230,19 @@ public char get(int index) {
181230
}
182231
// .toUpper method, ruturns MyString that is in all upper case
183232
public MyString toUpper() {
233+
234+
// copy origional to leave it unchanged
184235
char[] strChars2 = new char[currLength];
185236
for (int i = 0; i < currLength; i++) {
237+
238+
//upper case using wrapper class
186239
strChars2[i] = Character.toUpperCase(strChars[i]);
187240
}
241+
242+
// turn array into string
188243
String stringRep = new String(strChars2);
189-
244+
245+
// pass string to MyString constructor
190246
MyString Upper = new MyString(stringRep);
191247

192248
return Upper;
@@ -196,12 +252,19 @@ public MyString toUpper() {
196252

197253
// .toLower method, returns MyString that is in all lower case
198254
public MyString toLower() {
255+
256+
// copy origional to leave it unchanged
199257
char[] strChars2 = new char[currLength];
200258
for (int i = 0; i < currLength; i++) {
259+
260+
// lower case using wrapper class
201261
strChars2[i] = Character.toLowerCase(strChars[i]);
202262
}
263+
264+
//turn array into string
203265
String stringRep = new String(strChars2);
204-
266+
267+
// pass string to MyString constructor
205268
MyString Lower = new MyString(stringRep);
206269

207270
return Lower;
@@ -214,29 +277,39 @@ public MyString toLower() {
214277
If parameter is not found in the calling object, method returns -1
215278
*/
216279
public int indexOf(MyString str) {
280+
281+
// assume no occurance initially
217282
int index = -1;
218283

284+
// if perameter is longer than calling object, no occurance
219285
if (str.currLength > currLength) {
220286

221287
return index;
222288
}
223-
289+
290+
// traverse calling array until perameter can no longer fits
224291
for (int i = 0; i <= currLength - str.currLength; i++) {
225-
292+
293+
// peramter of length 1
226294
if (strChars[i] == str.strChars[0] && str.currLength == 1) {
227295
index = i;
228296
return index;
229297
}
230-
298+
// search for occurance of first letter in perameter string
231299
if (strChars[i] == str.strChars[0]) {
300+
301+
// check for subsequent equality
232302
for (int a = 1; a < str.currLength; a++) {
233303
if (strChars[i + a] == str.strChars[a]) {
304+
305+
// check for first complete occurance
234306
if(a == str.currLength -1){
235307
index = i;
236308
return index;
237309
}
238310
}
239311

312+
// subsequent equality failed, back to outer for loop
240313
else {
241314
break;
242315
}
@@ -253,26 +326,37 @@ public int indexOf(MyString str) {
253326
If parameter is not found in the calling object, method returns -1
254327
*/
255328
public int lastIndexOf(MyString str) {
329+
330+
// assume no occurance initially
256331
int index = -1;
257332

333+
// if perameter is longer than calling object, no occurance
258334
if (str.currLength > currLength) {
259335

260336
return index;
261337
}
262-
338+
// traverse calling array until perameter can no longer fits
263339
for (int i = 0; i <= currLength - str.currLength; i++) {
264-
340+
341+
// peramter of length 1, set index but keep looking
265342
if (strChars[i] == str.strChars[0] && str.currLength == 1) {
266343
index = i;
267344
}
268-
345+
346+
//search for occurance of first letter in perameter string
269347
if (strChars[i] == str.strChars[0]) {
348+
349+
// check for subsequent equality
270350
for (int a = 1; a < str.currLength; a++) {
271351
if (strChars[i + a] == str.strChars[a]) {
352+
353+
// for first complete occurance, set index, keep looking
272354
if(a == str.currLength -1){
273355
index = i;
274356
}
275-
357+
/*potential first occurance failed, index not set
358+
back to outer for loop
359+
*/
276360
} else if (index == -1){
277361
break;
278362
}
@@ -286,12 +370,16 @@ public int lastIndexOf(MyString str) {
286370
index.
287371
*/
288372
public MyString substring(int start) {
373+
374+
// initialize and populate new array
289375
char[] sub = new char[currLength - start];
290376
for(int n = start, a = 0; n < currLength; n++, a++){
291377
sub[a] = strChars[n];
292378
}
379+
// turn array into string
293380
String stringRep = new String(sub);
294381

382+
//pass string to MyString constructor
295383
MyString substring = new MyString(stringRep);
296384

297385
return substring;
@@ -302,12 +390,16 @@ public MyString substring(int start) {
302390
where n is the starting index and m is one past the ending index.
303391
*/
304392
public MyString substring(int start, int end) {
393+
394+
// initialize and populate array
305395
char[] sub = new char[end - start +1];
306396
for(int n = start, a = 0; n <= end; n++, a++){
307397
sub[a] = strChars[n];
308398
}
399+
// turn array into string
309400
String stringRep = new String(sub);
310401

402+
// pass string to MyString constructor
311403
MyString substring = new MyString(stringRep);
312404

313405
return substring;

0 commit comments

Comments
 (0)