Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We need to write our own version of a String class called MyString given the following starter file: public class MyString { private char[] letters;

We need to write our own version of a String class called MyString given the following starter file:

public class MyString { private char[] letters; public MyString( String other ) { // MUST BE DEEP COPY: USE REAL STRING'S BUILT IN METHOD // TO RETURN A DEEP COPY OF THE THE UNDERLYING CHAR ARRAY } public MyString( MyString other ) { // DONT do this -> letters = other.letters BECUASE THAT IS SHALLOW CopY!!! // Make a deep copy } public int length() { return 0; } public char charAt(int index) { return '\0'; // THE null CHAR } int compareTo(MyString other) { return 0; } public boolean equals(MyString other) { return false; } // LOOKING for c but starting at [startIndex], not at [0] // You should use this in your other Indexof Method public int indexOf(int startIndex, char ch) { return -1; } public int indexOf(MyString other) { // RE-USE the indexOf( int startIndex, char ch) method above in here return -1; } public String toString() { return null; } } // END MYSTRING CLASS 

-------------------------------------------------------------------------------------------

And this tester file:

public class MyStringTester { public static void main( String[] args ) { MyString s1 = new MyString("Hello World"); System.out.println( "s1=" + s1 ); MyString s2 = new MyString(s1); System.out.println( "s2=" + s2 ); if ( s1.equals( s2 ) ) System.out.println( s1 + " identical to " + s2 ); else System.out.println( s1 + " not identical to " + s2 ); MyString s3 = new MyString( "GoodBye World" ); System.out.println( "s3='" + s3 + "' and its length is: " + s3.length() ); if ( s1.equals( s3 ) ) System.out.println( s2 + " identical to " + s3 ); else System.out.println( s1 + " not identical to " + s3 ); s1=new MyString("albert"); s2=new MyString("alpha"); System.out.println( "s1=" + s1 + " s2=" + s2 ); System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // since "albert" < "alpha" returns -1 s1=new MyString("zebrano"); s2=new MyString("zebra"); System.out.println( "s1=" + s1 + " s2=" + s2 ); System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // since "zebrano" > "zebra" returns 1 s1=new MyString("cattle"); s2=new MyString("catty"); System.out.println( "s1=" + s1 + " s2=" + s2 ); System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // since "cattle" < "cattle" returns 1 s1=new MyString("pneumoencephalographically"); s2=new MyString("pneumoencephalographically"); System.out.println( "s1=" + s1 + " s2=" + s2 ); System.out.println( "s1.compareTo(s2) ==> " + s1.compareTo(s2) ); // returns 0 they are same string value System.out.println( "[4]'th letter of " + s3 + " is " + s3.charAt(4) ); MyString key = new MyString("rld"); System.out.println( key + " found in " + s3 + " at index: " + s3.indexOf( key ) ); // SAME THING BUT WITHOUT USING A MyString VAR FOR THE KEY System.out.println( new MyString("Goo") + " found in " + s3 + " at index: " + s3.indexOf( new MyString("Goo") ) ); System.out.println( new MyString("Bye") + " found in " + s3 + " at index: " + s3.indexOf( new MyString("Bye") ) ); System.out.println( new MyString("zorp") + " found in " + s3 + " at index: " + s3.indexOf( new MyString("zorp") ) ); } } 

-----------------------------------------------------------------------------------------

I believe I have most of it correct but I'm having issues with my indexOf methods. They look like this thus far:

public int indexOf(int startIndex, char ch)

{

for(int i = startIndex; i < this.length(); i++)

{

if(ch == letters[i])

return i;

}

return -1;

}

public int indexOf(MyString other)

{ // RE-USE the indexOf( int startIndex, char ch) method above in here

int indexOfFirst = indexOf(0, other.charAt(0));

if(indexOfFirst == NOT_FOUND)

return NOT_FOUND;

while(indexOfFirst != NOT_FOUND)

{

int index = indexOf(indexOfFirst, other);

if(index != NOT_FOUND)

return index;

else

indexOfFirst = indexOf(++indexOfFirst, other.charAt(0));

}

return -1;

}

private int indexOf(int i, MyString other)

{

String append = " ";

int j = 0;

while((j < other.length()) && (this.letters != null))

{

Character c = letters[i];

String a = c.toString();

append = append.concat(a);

i++;

j++;

}

MyString comp = new MyString(append);

boolean compare = other.equals(comp);

if(compare == false)

return -1;

else

return i;

}

int compareTo(MyString other) { if(this.length() > other.length()) return 1; else if(this.length() < other.length()) return -1; else { int i = 0;

while(this.charAt(i) == other.charAt(i)) i++;

if(i == this.length()) return 0; else return -1; } }

---------------------------------------------------------------

The private method indexOf is not required for the assignment but in a separate help document, he recommended we create one that we call in the second public indexOf method in a while loop. I'm pretty sure I have the general structure correct but I'm just not getting the correct output. Any help would be appreciated.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Obtain the h parameters for the two-port of Fig. 19.91. j6

Answered: 1 week ago