$24
Learning
objectives:
Write a Java
class with multiple constructors.
Write a Java
class with more complex methods.
Assignment:
Define a class named MyString whose objects simulate Java
Strings. The class must have two instance variables, one of type int for the length
and one of type char[] for the contents of the
string. You may not use any Java String variables or
values in this program, except as specified below.
Write two public constructors. One has a Java
String parameter. It converts the String to an array of char by the method toCharArray(). This is the only String method that you
are allowed to use in this program. The other has a MyString parameter, and
makes a new MyString with the same contents.
Write the following methods. It is strongly
suggested that you write them in this order, and test each method after you
have written it. Remember that after you write a constructor or method you may
(and should) use it to write further methods.
String
toString() returns the String representation of the MyString object. This
must meet all the usual requirements of a toString() method. Pass the
char[] array to a String constructor. If
the array is named s, this looks like new
String(s). This is the only String constructor that
you are allowed to use in this program.
int
length() returns the length of the MyString object.
char
charAt(int i) returns the character at position i of the MyString object.
At this point write a private constructor. It has a char[] parameter, and
makes a new MyString with the same contents.
Continuing with methods. Be prepared to do some arithmetic in some of
these methods.
MyString
clone()
makes a copy of the MyString object.
boolean
equals(MyString s) returns true or false as this and s are equal or
not.
MyString
concat(MyString s) returns a MyString object that begins with the
contents of this to which the contents of m are appended.
int
indexOf(char c, int i) returns the position of the first occurrence of c in this, at or after the
ith position of this. Returns -1 if c does not occur
in this.
MyString
substring(int i, int j) returns a MyString object that
begins with the ith position of this and ends with
the (j-1)th position of this.
int
indexOf(MyString s, int i) returns the position of the first occurrence of s in this, at or after the
ith position of this. Returns -1 if s does not occur
in this.
MyString
replace(MyString oldPart, MyString newPart) returns a MyString object that is
like this except that the substring oldPart within this is replaced by
the substring newPart. newPart may be longer or
shorter than oldPart, so the resulting MyString may be longer or
shorter than this.
Write a program TestMyString to test your MyString class. We
reserve the right to replace your TestMyString with our own.
Submission
instructions:
Upload the files MyString.java and TestMyString.java to “Program 4”.
Do not submit class files or any of the BlueJ control files. Be sure to press
the submit button.
Policies:
The policies given on WebCT are in effect for this and all assignments.
You may submit on WebCT multiple times, so there is no excuse for not
submitting partial solutions.