Question
Here is the Starter file you have to use: public class MyString { private char[] letters; public MyString( String other ) { // MUST BE
Here is the Starter file you have to use:
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
Here is the tester file you do not modify, just use to test MyString.:
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" " + 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" " + 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") ) ); } }
Here are the instructions:
You will write your own version of the String class and call it the MyString class.
Your MyString class will have only one data member:
private char[] letters;
It will be a very simplified version which will impliment only these public methods: (you may write private helper methods)
(worth 10%) public MyString( String other ) Inititialize this class's internal letters array to a deep copy of the incoming (real Java) String.
(worth 10%) public MyString( MyString other ) Inititialize this class's internal letters array to a deep copy of the incoming MyString.
(worth 5%) public int length() return the count of letters in this MyString's char array.
(worth 10%) public char charAt(int index) return the char stored at this index of this MyString
(worth 15%) int compareTo(MyString other) return 0 if the passed in MyString is identical to this one return 1 if this MyString is greater than the passed in MyString otherwise return -1
(worth 10%) public boolean equals(MyString other) return true only if the passed in MyString is identical to this one (HINT: reuse a method already written)
(worth 10%) public int indexOf(int startIndex, char ch) return the index of first occurance where the passed is char is found in this MyString but dont start the search at index 0. Start the search at index=startIndex if not found return -1
(worth 25%) public int indexOf(MyString other) return the index of first occurance where the passed is MyString is found in this MyString if not found return -1
(worth 5%) public String toString() return a standard Java String that is identical to this MyString
Your output must look EXACTLY like this below:
If you need help writing the indexOf (MyString) use this for guidance:
Thanks!!
s1=H e 110 world s2-Hello World Hello World identical to Hello World s3="GoodBye world' and its length is: 13 Hello World not identical to GoodBye World s1-albert s2-alpha s1. compareTo(S2) ==> -1 s1-zebrano s2-zebra s1.compareTo(S2) ==> 1 s1-cattle s 2-Catty s1. compareTo(S2) ==> -1 s1=pneumoencepha lograph!cal ly s2=pneumoencepha lograph!cal ly s1. compareTo(S2) ==> 0 [41'th letter of GoodBye World isB ld found in GoodBye World at index: 10 Goo found in GoodBye World at index: o Bye found in GoodBye World at index: 4 zorp found in GoodBye World at index: -1Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started