Question
Java : implement your own public string class, to be called MyString MyString should be able to store arbitrarily long strings of Java's char type.
Java : implement your own public string class, to be called MyString
MyString should be able to store arbitrarily long strings of Java's char type. Internally, the text must be represented as an array of characters. Your code may make use of arrays and other basic Java language features, but must never use (directly or indirectly) the Java's String or StringBuffer classes or any of their methods.
must be placed in package cs2.muttxt. then follow the instructions below exactly
Part 1 - Methods:
The required methods for the MyString class, with their signatures, return types and behaviors.
public MyString( ): initializes a newly created MyString object so that it represents an empty character sequence. (We'll say the MyString object is the "empty string" in this case.) The DEFAULT_SIZE should always be 5.
public MyString(char ch): initializes a newly created MyString object so that it represents a character sequence consisting of just ch. The DEFAULT_SIZE should always be 5. Thus the remaining four cells in the underlying array will be empty.
public MyString(MyString otherMyString): initializes a newly created MyString object so that it represents the same string as otherMyString.
public boolean equals(Object o): returns true if, and only if, object o is a MyString object representing the same string as this string
public char myCharAt(int index): returns the char at location index, where the first character is at location 0, etc. Throws MyStringIndexOutOfBoundsException exception (see below).
public MyString myConcat(MyString otherMyString): returns this MyString object if otherMyString is the empty string, and otherwise returns a new MyString which represents the concatenation of this string with the other string following it.
public void myLineDisplay( ): displays the sequence of char to the screen with an end-of-line at the end (do not add extra spaces between chars).
public int myIndexOf(char ch): returns -1 if ch does not occur in this string, and otherwise returns the smallest location of ch in this string
public int myLength( ): returns the length of this string.
public void setAt(int index, char ch): sets the character at location index to the character ch. Throws MyStringIndexOutOfBoundsException exception. Please implement it in a way that one can APPEND a char to the sequence. One can append a char only to the position right after the current last char! For example, if the length is 5, mystr.setAt(5,'c') should succeed while mystr.setAt(6, 'a') should fail! Assume your internal array of chars is named ary, and length is the length of the sequence of characters, and size is the size of ary. Thus, we cannot have (length > size) at any point of time.
if index is within the current sequence then modify ary[index]
else if index is provided in such a way to APPEND a char
expand the array if necessary (when?)
modify ary[index]
modify length
else throw MyStringIndexOutOfBoundsException exception
You will have to increase the internal array size if the user adds more characters than your array can hold. For example, since the initial size is 5, on the 6th char add, you will have to increase the size. I recommend doubling the size whenever this occurs.
public MyString mySubString(int low, int high): returns a new MyString representing the substring of this string from location low up through location high - 1. If (low == high) returns the empty string. Throws MyStringIndexOutOfBoundsException exception (see below).
public char[] myToCharArray(): converts this MyString to a new character array. It should return a newly allocated character array whose length is the length of this MyString and whose contents are initialized to contain the character sequence represented by this MyString.
Step 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