Comp 110 SillyString - Part II

Part II - More Utility Methods

Points: 277

Full EC Deadline: Wednesday 3/1 at 11:59pm

Deadline (Extended): Friday 3/3 at 11:59pm

I am shortening this assignment to reduce its scope. Originally all 10 methods remaining in your SillyString class were to be required. I am modifying this so that only 7 methods are required and 3 are now for extra credit.

Leave yourself plenty of time for the methods split and splice. These are the most challenging algorithms to come up with of the required methods. Consider taking on split last.

Completing the 3 extra credit methods is worth up to 43 additional points. I would encourage you to try to complete all 10! The extra credit methods are challenging so please complete the required methods first.

Section 4.

Note: For all of the following methods in sections 4, 5, and 6, you will want to be sure not to modify the _chars field of the SillyString the method is called on.

4.1 SillyString reverse()

Return a new SillyString object with its characters in reverse order.

Note: You will want to be sure *not* to modify the original SillyString object's _chars field.

SillyString letters = new SillyString("abc");
SillyString reversed = letters.reverse();
console.print(reversed); // "cba"

4.2 SillyString toUpperCase()

Return a new SillyString object with all of its lowercase characters converted to uppercase.

Note two special properties of characters you should make use of in this method:

First, you can compare two characters using inequalities. For example:

'a' >= 'a' && 'a' <= 'z' -> true
'b' >= 'a' && 'b' <= 'z' -> true
'z' >= 'a' && 'z' <= 'z' -> true
'A' >= 'a' && 'A' <= 'z' -> false
'2' >= 'a' && '2' <= 'z' -> false

Second, you can "add" or "subtract" from a char value to change the character. Note, however, you will need to "cast" the result back to a char. Examples of how to do this are shown below:

char a = 'a';
char b = (char) (a + 1);
char z = (char) (a + 25);
char y = (char) (z - 1);

Every character has a specific numerical code called its ASCII code. You can find an ASCII table here. Using that table, you should be able to figure out how to use some arithmetic to convert a lowercase character into an uppercase character.

Here are some example outputs:

SillyString characters = new SillyString("abc123XYZ");
SillyString upper = characters.toUpperCase();
console.print(upper); // "ABC123XYZ"

Concatenate Overview

4.3 SillyString concatenate(SillyString other)

This method should return a new SillyString object which contains the original SillyString object concatenated with the other SillyString object parameter.

Note that you can ask the other SillyString for its chars array using its getChars method. For example other.getchars()

SillyString letters = new SillyString("abc");
SillyString numbers = new SillyString("123");
SillyString concat = letters.concatenate(numbers);
console.print(concat); // "abc123"
concat = numbers.concatenate(letters);
console.print(concat); // "123abc"

Method Reuse using this.<methodName>()

4.4 SillyString repeat(int n)

This method should return a new SillyString object which contains the original SillyString object repeated n times concatenated one after the other.

Consider making reuse of your concatenate method from 4.3 by using the this keyword to call the method on the same object. Watch the video above for an example of how this works and refer to Lecture 12's slides.

SillyString letters = new SillyString("abc");
SillyString repeated = letters.repeat(0);
console.print(repeated); // ""
repeated = letters.repeat(1);
console.print(repeated); // "abc"
repeated = letters.repeat(3);
console.print(repeated); // "abcabcabc

Section 5.

5.1 SillyString substring(int start, int end)

Return a new SillyString object containing the characters of the original object from indices start to end - 1.

You can assume start is at least 0 and end is at most the length of the _chars array. However, if you'd like to check for these conditions and ensure that you never start at a negative index or end out of bounds of the array it is good practice.

SillyString characters = new SillyString("abc123XYZ");
SillyString abc = characters.substring(0, 3);
SillyString numbers = characters.substring(3, 6);
SillyString xyz = characters.substring(6, 9);
SillyString empty = characters.substring(0, 0);
console.print(abc); // "abc"
console.print(numbers); // "123"
console.print(xyz); // "XYZ"
console.print(empty); // ""

5.2 SillyString[] split(char delimiter)

Extra Credit Method #1 of 3. This one is worth 23 points.

Split up the original SillyString into an array of SillyStrings every time the delimiter character is encountered. So, for example, imagine a SillyString of "a-b-c", the resulting array would have three SillyString elements "a", "b", and "c". Should the SillyString start or end with delimiter, the resulting array should have an empty SillyString at the start or end of the returned array.

SillyString phrase = new SillyString(" the quick brown fox ");
SillyString[] words = phrase.split(' ');
for (int i = 0; i < words.length; i++) {
  console.print(i + ": " + words[i]);
}
// Output:
// 0: 
// 1: the
// 2: quick
// 3: brown
// 4: fox
// 5:

5.3 SillyString join(SillyString glue, SillyString[] pieces)

Use the glue parameter to join all of the pieces elements to the end of the original SillyString.

Hint: we encourage you to try making use of your concatenate method! If you didn't watch the video on how to make reuse of concatenate, we'd encourage you to watch this video!

SillyString the = new SillyString("the");
SillyString quick = new SillyString("quick");
SillyString brown = new SillyString("brown");
SillyString fox = new SillyString("fox");
SillyString[] words = new SillyString[] { quick, brown, fox };
SillyString space = new SillyString(" ");
SillyString tildes = new SillyString("~~~");
SillyString phrase = the.join(space, words);
console.print(phrase); // the quick brown fox
phrase = the.join(tildes, words);
console.print(phrase); // the~~~quick~~~brown~~~fox

Section 6.

Splice Walkthrough

Reusing Existing Methods

6.1 SillyString splice(int offset, int length, SillyString other)

Starting from offset, remove the following length characters from the original SillyString, and insert the other SillyString's characters starting at offset.

You can assume that offset and offset + length are valid indices.

Hint: watch the videos above for an overview and consider how you can make use of both substring and concatenate to simplify the implementation of this method.

Consider the following cases carefully.

SillyString letters = new SillyString("abc");
SillyString empty = new SillyString("");
SillyString z = new SillyString("z");
SillyString xyz = new SillyString("xyz");
SillyString x = new SillyString("x");
console.print(letters.splice(2, 0, z)); // "abzc"
console.print(letters.splice(2, 0, xyz)); // "abxyzc"
console.print(letters.splice(1, 1, empty)); // "ac"
console.print(letters.splice(1, 1, z)); // "azc"
console.print(letters.splice(1, 1, xyz)); // "axyzc"
console.print(letters.splice(0, 3, x)); // "x"
console.print(letters.splice(0, 3, xyz)); // "xyz"

6.2 SillyString replace(SillyString search, SillyString replace)

NOTE: This is method 2 of the 3 extra credit methods. It is worth 10 points. Our recommendation is to come back to this method after obtaining full credit on the rest of Part 2.

Replace the first occurrence of the search parameter with the replace parameter. Hint: we encourage you to try making use of your splice method while building this.

SillyString phrase = new SillyString("the quick brown fox");
SillyString the = new SillyString("the");
SillyString a = new SillyString("a");
SillyString o = new SillyString("o");
console.print(phrase.replace(the, a)); // a quick brown fox
console.print(phrase.replace(o, a)); // the quick brawn fox

6.3 SillyString replaceAll(SillyString search, SillyString replace)

NOTE: This is method 3 of the 3 extra credit methods. It is worth 10 points. Our recommendation is to come back to this method after obtaining full credit on the rest of Part 2.

Replace all occurrences of the search parameter with the replace parameter. Hint: we encourage you to try making use of your splice method while building this.

SillyString phrase = new SillyString("the quick brown fox");
SillyString the = new SillyString("the");
SillyString a = new SillyString("a");
SillyString o = new SillyString("o");
console.print(phrase.replaceAll(the, a)); // a quick brown fox
console.print(phrase.replaceAll(o, a)); // the quick brawn fax