Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: You will write your own version of the String class and call it the MyString class. Your MyString class will have only one data

JAVA: 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 implement only these public methods: (you may write private helper methods):

public MyString( String other ) Initialize this class's internal letters array to a deep copy of the incoming (real Java) String.

public MyString( MyString other ) Initialize this class's internal letters array to a deep copy of the incoming MyString.

public int length() return the count of letters in this MyString's char array.

public char charAt(int index) return the char stored at this index of this MyString

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

public boolean equals(MyString other) return true only if the passed in MyString is identical to this one (HINT: reuse a method already written)

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

public int indexOf(MyString other) Re-use the public int indexOf(int startIndex, char ch) return the index of first occurance where the passed is MyString is found in this MyString if not found return -1

public String toString() return a standard Java String that is identical to this MyString

The following code is what I got so far, but I am stuck on how to write the public int indexOf(int startIndex, char ch) and public int indexOf(MyString other)

image text in transcribedimage text in transcribedimage text in transcribed

Thank you!

public class MyString 2 private char letters; public MyString(String other) 7 letters for (int new char [other. length() + 1]; i = 0;

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: 3

blur-text-image

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