Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

Add Exceptions to following code follow the instructions below exactly:

If an input index is out of bounds, your method should throw a MyStringIndexOutOfBoundsException exception using its 1-param intconstructor (see below).

In mySubString(low, high) if high < low, the method should throw a MyStringIndexOutOfBoundsException exception using its 1-param String constructor (see below) with the message "highIndex = " + high + " less than lowIndex = " + low.

If an input object reference is null, your method should throw an IllegalArgumentException, except that equals(...) should simply return false if the input is null.

The class MyStringIndexOutOfBoundsException should be derived from java.lang.IndexOutOfBoundsException class and should have the following constructors:

MyStringIndexOutOfBoundsException(): constructs a MyStringIndexOutOfBoundsException with no detail message. It simply calls the default constructor of its superclass.

MyStringIndexOutOfBoundsException(String errMsg): constructs a MyStringIndexOutOfBoundsException with the specified detail message. It should call the 1-param constructor of the superclass passing errMsg as the argument.

MyStringIndexOutOfBoundsException(int index): constructs a new MyStringIndexOutOfBoundsException class with an argument indicating the illegal index. It should call the 1-param constructor of the superclass passing the following string as the argument. "MyString index out of range: " + index

code:

Java : Implementation of public string class, to be called MyString.

public class MyString { private char[] characters; private int length; private final int DEFAULT_SIZE = 5;

public MyString() { length = DEFAULT_SIZE; characters = new char[length]; }

public MyString(char ch) { length = DEFAULT_SIZE; characters = new char[length]; characters[0] = ch; }

public MyString(char ch[]) { length = ch.length; characters = new char[length]; for (int i = 0; i < length; i++) characters[i] = ch[i]; }

/** * Initializes a newly created MyString object so that it * represents the same string as otherMyString. * @param otherMyString */ public MyString(MyString otherMyString) { length = otherMyString.myLength(); characters = new char[length]; for (int i = 0; i < length; i++) characters[i] = otherMyString.myCharAt(i); }

/** * Returns true if, and only if, object o is a MyString * object representing the same string as this string */ public boolean equals(Object o) { MyString other; if (!(o instanceof MyString)) return false; else other = (MyString) o;

// if lengths are different no need to check characters if (this.length != other.length) return false;

int i = 0; while (i < this.length) { if (this.characters[i] != other.characters[i]) return false; // as soon as one char differs they are not the // same i++; } return true; // same length and all same chars -- they are the same }

/** * Returns the char at location index, where the first character is at location 0, * etc. Throws MyStringIndexOutOfBoundsException exception * @param index * @return */ public char myCharAt(int index) { // we may talk about exception handling later this semester if ((index < 0) || (index >= characters.length)) throw new StringIndexOutOfBoundsException(index); return characters[index]; }

/** * 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. * @param otherMyString * @return */ public MyString myConcat(MyString otherMyString) { // Calculate the length of the concatenation. int length = this.characters.length + otherMyString.characters.length;

// Allocate the space for the new myString. char[] temp = new char[length];

// Copy in all the current object characters. for (int i = 0; i < this.characters.length; i++) temp[i] = this.characters[i];

// Copy after that all the characters from str. for (int i = 0; i < otherMyString.characters.length; i++) temp[this.characters.length + i] = otherMyString.characters[i];

// Create the new myString and return it. return new MyString(temp); }

/** * Displays the sequence of char to the screen with an end-of-line at * the end (do not add extra spaces between chars). */ public void myLineDisplay() { for (int i = 0; i < this.characters.length; i++){ if (characters[i] == ' ') { break; } else { System.out.format("%c", characters[i]); } } System.out.println(""); }

/** * returns -1 if ch does not occur in this string, and otherwise * returns the smallest location of ch in this string * @param ch * @return */ public int myIndexOf(char ch) { int fromIndex = 0; if (fromIndex < 0) fromIndex = 0; else if (fromIndex >= length) return -1;

for (int i = fromIndex; i < length; i++) if (characters[i] == ch) return i; return -1; }

/** * Returns the length of this string. * @return */ public int myLength() { return length; } /** * Sets the character at location index to the character ch * @param index * @param ch */ public void setAt(int index, char ch){ if (index < 0) throw new StringIndexOutOfBoundsException(index); if (index > length) throw new StringIndexOutOfBoundsException(index); characters[index]=ch; }

/** * 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 * * @param low * @param high * @return */ public MyString mySubString(int low, int high) { if (low < 0) throw new StringIndexOutOfBoundsException(low); if (high > length) throw new StringIndexOutOfBoundsException(high); if (low > high) throw new StringIndexOutOfBoundsException(high - low);

MyString result = new MyString(); result.length = high - low + 1; result.characters = new char[result.length];

for (int i = 0; i < result.length; i++) result.characters[i] = this.characters[low + i];

return result; }

/** * 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. * * @return */ public char[] myToCharArray() { return characters; }

public static void main(String[] args) {

MyString str2 = new MyString('R'); str2.setAt(1, 'A'); str2.setAt(2, 'M'); str2.setAt(3, 'E'); str2.setAt(4, 'S'); str2.myLineDisplay(); MyString str = new MyString(str2); System.out.println(str.myCharAt(0)); str.myLineDisplay(); System.out.println(str.myLength()); char [] myToArray=str.myToCharArray(); System.out.println(myToArray); str=str.myConcat(str2); str.myLineDisplay(); System.out.println(str.myLength()); MyString substr=str.mySubString(0, 3); substr.myLineDisplay(); } }

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

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

Students also viewed these Databases questions

Question

=+ how might this lead to faster growth in productivity?

Answered: 1 week ago