Question
In this assignment you will implement your OWN version of the String class and some of its methods. You should be familiar with the behavior
In this assignment you will implement your OWN version of the String class and some of its methods. You should be familiar with the behavior of this class. The class has the following specification:
public MyString(char[] original) -- this constructor creates a new instance of MyString using the character array given as a parameter.
public MyString( String original) : this is the constructor for your class, it takes as input an instance of a Java String and breaks it into a character array. (See the method toCharArray in Javas class String, easy way to make a character array from a Java String)
public MyString( MyString original) : this is the constructor for your class, it takes as input an instance of a MyString and creates a deep copy.
public int length() - This method returns the length, or number of characters in the string
public char charAt(int index) -- given a valid index this method returns the character at that index. If an invalid index is given your method should throw an IndexOutOfBoundsException, in the form
throw new IndexOutOfBoundsException(Message);
where message is the text you want printed with the exception
public MyString concat(MyString otherString) -- Concatenates the specified string, otherString, to the end of this string object producing a new MyString.
public int indexOf( char ch) -- Returns the index within this string of the first occurrence of the specified character ch , if ch is not present in the string the method returns -1
public int indexOf( char ch, int fromIndex) -- Returns the index within this string of the first occurrence of the specified character, ch, starting the search at the specified index. ch is not present in the string the method returns -1
public MyString replace( char target, char replacement) -- Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
public MyString substring( int beginIndex) -- Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
public MyString substring( int beginIndex, int endIndex) -- Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
public MyString toUpperCase() -- Converts all of the characters in this String to upper case
public MyString toLowerCase() -- Converts all of the characters in this String to lower case
public boolean equals(MyString anObject) -- Compares this string to the specified parameter. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
public String toString() -- Returns a String containing the contents of the MyString object
public int compareTo(MyString anotherString) -- Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this MyString object is compared lexicographically to the character sequence represented by the argument MyString. The result is a negative integer if this MyString object lexicographically precedes the argument MyString. The result is a positive integer if this MyString object lexicographically follows the argument MyString. The result is zero if the MyStrings are equal
Tester class for MyString.java:
public class TestMyString {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyString brett = new MyString("Brett D Hails");
MyString brian = new MyString("Brian L Thomas");
MyString adam = new MyString("Adam K. Peters");
MyString adam2 = new MyString("Adam K. Petersen");
MyString copy = new MyString("Brett D Hails");
char[] arr = {'d', 'c','f','d'};
MyString arrayString = new MyString(arr);
System.out.println("oops " + "Adam K. Peters".compareTo("Adam K. Petersen"));
System.out.println(brett);
System.out.println( arrayString);
System.out.println( "length of arr is " + arrayString.length()); // should be 4
System.out.println( brian.charAt(6));// should be L
System.out.println( brett.charAt(9)); // should be a
System.out.println( brett.charAt(0)); // should be B
System.out.println( brett.charAt(12)); // should be s
System.out.println( arrayString.concat(brett)); // should be dcfdBrett D Hails
System.out.println( brett.equals(brian)); // should be false
System.out.println(brett.equals(copy)); // should be true
System.out.println(adam.indexOf('K')); // should be 5
System.out.println( brian .indexOf('a', 5)); // should be 12
System.out.println( brett.replace('t', '#')); // should print Bre## D Hails
System.out.println( brett.toUpperCase());
System.out.println( brian.toLowerCase());
//System.out.println( brett.substring(8)); // should print Hails
System.out.println( brett.substring(2, 9)); // should print ett D H
System.out.println( brett.compareTo(brian)); // should be -4
System.out.println(adam.compareTo(brett)); // should be -1
System.out.println( copy.compareTo(brett)); // should be 0
System.out.println(adam2.compareTo(adam));
}
}
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