Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using JUnit 4.12, you should write a test class called MyStringTester.java that contains 12 test cases for the following methods: MyString() myLength() myToCharArray() setAt(...) [3

Using JUnit 4.12, you should write a test class called MyStringTester.java that contains 12 test cases for the following methods:

MyString()

myLength()

myToCharArray()

setAt(...) [3 different test cases]

equals(...) [3 different test cases]

myCharAt(...)

mySubSting(...)

myConcat(...)

For example, to test the myLength method:

Create a MyString object called mystr

Add 6 characters to mystr

Call mystr.myLength(), and check if it returns 6

For the setAt and equals methods, you need to come up with 3 different scenarios. Examples for the equals method:

equals returns false when the input is of a different type (say, Integer instead of MyString)

equals returns false when the input represents a different sequence of chars

equals returns true when the input represents the same sequence of chars

Below is the code Mystring.java and MyStringIndexOutOfBoundsException.java

Mystring.java.

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) {

if (otherMyString == null)

{

throw new IllegalArgumentException();

}

else

{

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 MyMyStringIndexOutOfBoundsException exception

* @param index

* @return

*/

public char myCharAt(int index) {

if ((index < 0) || (index >= characters.length))

throw new MyStringIndexOutOfBoundsException(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) {

if (otherMyString == null)

throw new IllegalArgumentException();

// 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 MyStringIndexOutOfBoundsException(index);

if (index > length)

throw new MyStringIndexOutOfBoundsException(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 MyMyStringIndexOutOfBoundsException exception

*

* @param low

* @param high

* @return

*/

public MyString mySubString(int low, int high) {

if (low < 0)

throw new MyStringIndexOutOfBoundsException(low);

if (high > length)

throw new MyStringIndexOutOfBoundsException(high);

if (low > high)

throw new MyStringIndexOutOfBoundsException("highIndex = " + high

+ " less than lowIndex = " + 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;

}

}

MyStringIndexOutOfBoundsException.java

import java.lang.IndexOutOfBoundsException;

public class MyStringIndexOutOfBoundsException extends IndexOutOfBoundsException{

/**

* Constructor that extends from IndexOutOfBoundsException class

*/

public MyStringIndexOutOfBoundsException()

{

super();

}

/**

* Constructor that extends from IndexOutOfBoundsException class

* @param String

* prints out custom error message

*/

public MyStringIndexOutOfBoundsException(String errMsg)

{

super(errMsg);

}

/**

* Constructor that extends from IndexOutOfBoundsException class

* @param index number that is out of range

* Prints out error message with the index

*/

public MyStringIndexOutOfBoundsException(int index)

{

super("MyString index out of range: "+index);

}

}

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

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

(a) What is the complete defining relation? Pg45

Answered: 1 week ago

Question

11:31

Answered: 1 week ago