Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using JUnit 4.12, you should write a test class called MyStringTester.java that

ID: 3864650 • Letter: U

Question

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

}

}

Explanation / Answer

MyStringTester.java

import static org.junit.Assert.*;
import org.junit.Test;

public class MyStringTester {
/**
*Creates MyString object
*@result a String object
*@exception IllegalArgumentException
*/
   //Testing MyString
   @Test
   public void testMyString()
   {
       MyString test = new MyString();
       boolean result=false;  
       if(test instanceof MyString)
           result=true;
       assertEquals(true,result);
   }
   @Test
   public void testMyString2()
   {
       MyString test= new MyString('a');
       boolean result=false;
       if((test instanceof MyString)&&(test.myLength()==1))
           result=true;
       assertEquals(true,result);
   }
   @Test(expected=IllegalArgumentException.class)  
   public void testMyString3()
   {
       MyString test= new MyString(null);
       MyString result= new MyString(test);
   }
  
   //Testing out myLength
   @Test
   public void testMyLength()
   {
       MyString test = new MyString('a');
       int result= test.myLength();
       assertEquals(1,result);
   }
  
   //Testing myToCharArray
   @Test
   public void testMyToCharArray()
   {
       MyString test = new MyString();
       char[] result = test.myToCharArray();
       char[] array = new char[5];
       boolean result2=true;
       for(int i=0; i<result.length; i++)
       {
           if(result[i]!=array[i])
               result2 = false;
       }
       assertEquals(true, result2);
   }
   @Test
   public void testMyToCharArray2()
   {
       MyString test = new MyString();
       char []test2=test.myToCharArray();
       Boolean result=false;
       if (test2 instanceof char[])
       {
           result=true;
       }
       assertEquals(true,result);
   }
  
   //TestingSetAt
   @Test
   public void test1SetAt()
   {
       MyString test = new MyString();
       test.setAt(0,'a');
       MyString result = new MyString();
       result.setAt(0,'a');
       assertEquals(test,result);
   }
   @Test
   public void test2SetAt()
   {
       MyString test = new MyString();
       test.setAt(2,'b');
       MyString result = new MyString();
       result.setAt(2,'b');
       assertEquals(test,result);
   }
   @Test
   public void test3SetAt()
   {
       MyString test = new MyString();
       test.setAt(4,'c');
       MyString result = new MyString();
       result.setAt(4,'c');
       assertEquals(test,result);
   }
   @Test(expected=MyStringIndexOutOfBoundsException.class)
   public void test4SetAt()
   {
       MyString test=new MyString();
       test.setAt(6,'a');
   }
  
   //Testing Equals
   @Test
   public void test1Equals()
   {
       MyString test= new MyString();
       boolean result=test.equals('5');
       assertEquals(false,result);
   }
   @Test
   public void test2Equals()
   {
       MyString test = new MyString('a');
       MyString otherMyString = new MyString('b');
       boolean result=test.equals(otherMyString);
       assertEquals(false,result);
   }
   @Test
   public void test3Equals()
   {
       MyString test = new MyString('a');
       MyString otherMyString =new MyString('a');
       boolean result=test.equals(otherMyString);
       assertEquals(true,result);
   }
   @Test
   public void test4Equals()
   {  
       Integer test= new Integer(1);
       Integer test2= new Integer(1);
       boolean result=test.equals(test2);
       assertEquals(true,result);
      
   }
   @Test
   public void test5Equals()
   {
       MyString test= new MyString();
       boolean result=test.equals(null);
       assertEquals(false, result);
   }
   @Test
   public void test6Equals()
   {
       MyString test= new MyString();
       Integer number= new Integer(1);
       boolean result=test.equals(number);
       assertEquals(false,result);
   }  
  
   //Testing MyCharAt
   @Test
   public void testMyCharAt()
   {
       MyString test = new MyString('a');
       char result=test.myCharAt(0);
       assertEquals('a',result);
   }
   @Test(expected=MyStringIndexOutOfBoundsException.class)
   public void testMyCharAt2()
   {
       MyString test = new MyString();
       char result=test.myCharAt(6);
   }
   @Test(expected=MyStringIndexOutOfBoundsException.class)
   public void testMyCharAt3()
   {
       MyString test = new MyString();
       char result=test.myCharAt(-1);
   }
  
   //Testing MySubString
   @Test
   public void testMySubString()
   {
       MyString test = new MyString('a');
       test.setAt(1, 'b');
       test.setAt(2, 'c');
       test.setAt(3, 'd');
       MyString result = test.mySubString(1, 3);
       MyString result2 = new MyString('b');
       result2.setAt(1,'c');
       assertEquals(result2, result);
   }
   @Test(expected=MyStringIndexOutOfBoundsException.class)
   public void testMySubString2()
   {
       MyString test= new MyString('a');
       test.setAt(1, 'b');
       test.setAt(2, 'c');
       test.setAt(3, 'd');
      
       MyString result=test.mySubString(10,5);
   }
  
   //Testing MyConcat
   @Test
   public void testMyConcat()
   {
       MyString test = new MyString('a');
       MyString test2 = new MyString('b');
       MyString result= test.myConcat(test2);
       MyString result2= new MyString(2);
       result2.setAt(0, 'a');
       result2.setAt(1, 'b');
       assertEquals(result2, result);  
   }
   @Test(expected=IllegalArgumentException.class)
   public void testMyConcat2()
   {
       MyString test = new MyString(null);
       MyString test2 = new MyString();
       MyString result=test2.myConcat(test);
   }
      
  
}


MyString.java

public class MyString extends MyStringIndexOutOfBoundsException{

/**
* array that will be utilized for all of the methods
*/
private char[] charArray;
/**
* default size of the array and MyString Object
*/
public final int DEFAULT_SIZE=5;

       /**
       * Creates an Empty String
       */
        public MyString()//Constructor creates an empty MyString object
        {

           charArray= new char[DEFAULT_SIZE];
        }
     
        /**
         * @param ch places character in object
         */
        public MyString(char ch)//Constructor creates MyString with a character
        {
           charArray= new char[DEFAULT_SIZE];
           charArray[0]=ch;
        }
    
        //Personal Constructor
        public MyString(int newSize)

        {
           charArray=new char[newSize];
        }
      
        /**
         * @return the MyString length
         */
        public int myLength()//Tells you the length of the MyString Object
        {
           int counter=0;
           for (int i=0; i<charArray.length;i++)
           {
               if(charArray[i]!=0)
               {
                   counter++;
               }
           }
           return counter;
        }
      
        /**
         * @param otherMyString is set to MyString
         */
        public MyString(MyString otherMyString)//Copies a MyString object to another
        {
           if(otherMyString==null)
           {
               throw new IllegalArgumentException();
           }
           else
           {
               charArray=new char[otherMyString.myLength()];//From one array to the other
               for (int i=0; i<otherMyString.myLength();i++)
               {
                   charArray[i]=otherMyString.myCharAt(i);
               }
           }
        }
      
        //PUT THIS INTO NIKE!!!!!!!!!
        /**
         * @param index , spot to search
         * @return a character
         */
        public char myCharAt(int index)//Gives you the char at that spot
        {
           if((index>charArray.length)||(index<0))
           {
               throw new MyStringIndexOutOfBoundsException(index);
           }
           else
           return charArray[index];
        }
     
      
       /**
        * @param MyString object
        * @return true
        * @return false
        */
        public boolean equals(Object o)//Checks equality of object
        {
           boolean equals=false;
           if (!(o instanceof MyString))
               equals=false;
           else if (o instanceof MyString)
           {
               MyString o2=(MyString)o;
               for (int i=0; i<this.myLength();i++)
               {
                   if ( this.myCharAt(i)!= o2.myCharAt(i) )
                   {
                           equals=false;
                           break;//goes directly to the return statement
                   }
                   else
                       equals=true;
               }
           }
           else if(o==null)
                   equals=false;
          
           //will only return true if conditions are met
           return equals;
        }
     
      
        /**
         * @param otherMyString, used for comparison to another object
         * @return a empty string
         * @return MyStringObject
         */
        public MyString myConcat(MyString otherMyString)
        {
           if (otherMyString==null)
           {
               throw new IllegalArgumentException();
           }
           else if(otherMyString.myLength()==0)
           {
               return this;
           }
           else
           {
               /*creates a new MyString object, then loops through
               both the array and otherMyString and copies it one at
               a time into the new object. */
               int length=this.myLength()+otherMyString.myLength();
               MyString concat=new MyString(length);
               for(int i=0;i<this.myLength();i++)
               {
                   concat.setAt(i,this.myCharAt(i));
               }
      
               for(int counter=0,i=this.myLength();i<length;i++, counter++)
               {
                   concat.setAt(i, otherMyString.myCharAt(counter));
               }
               return concat;
           }
          
        }
      
        /**
         * @param index, place in object
         * @param ch, character to place
         * @exception MyStringIndexOutOfBoundsException index is not valid
         */
        public void setAt(int index, char ch)
     
        {
           //Lengthens the array by adding an extra character
           if(index==charArray.length)
           {
               char[] tempArray= new char[index*2];//array is doubled
               for(int i=0; i<charArray.length;i++)
               {
                   tempArray[i]=charArray[i];
               }
               tempArray[index]=ch;
               charArray=tempArray;
           }
           else if((index>charArray.length)||(index<0))
           {
               throw new MyStringIndexOutOfBoundsException(index);
           }
           else
           {  
               //simply just places the character at index
               charArray[index]=ch;
           }
        }
     
      
        /**
         * Simply prints out the MyString Object contents
         */
        public void myLineDisplay()
        {
           for(int i=0; i<charArray.length;i++)
           {
  
               System.out.print(this.myCharAt(i));
           }
        }
     
        /**
         * @param ch character to be found
         * @return index place where letter is
         * @return -1 if char isn't found
         */
        public int myIndexOf(char ch)
        {
           //loops through trying to find a match
           int index=0;
           boolean found=false;
           for(int i=0;i<charArray.length;i++)
           {
               if(ch==this.myCharAt(i))
               {
                   found=true;
                   index=i;
                   break;
               }
               else if (found==false)
               {
                   return -1;
               }
           }
                   return index;
        }
      
        /**
         * @param low beginning place of old MyString
         * @param high ending place of old MyString
         * @return substring new MyString Object
         * @return empty MyString object
         * @exception MyStringIndexOutOfBoundsException
         *            high and low values aren't valid
         *
         */
        public MyString mySubString(int low, int high)
        {
           if(low<high)
           {
               //Places new String part inside of new MyString Object
               MyString substring=new MyString();
               int length=high-low;
               for (int i=0;i<length;i++)
               {
                   substring.setAt(i, this.myCharAt(i+low));
               }
            return substring;
           }
           else if(high<low)
           {
               throw new MyStringIndexOutOfBoundsException("High index= "
               +high+" less than lowIndex= "+low);
           }
           else
           {
               return new MyString();
           }
        }  

        /**     

         * @return myStringArray, array filled with the MyString characters
         */
        public char[] myToCharArray()//puts the MyString object contents into a array
        {
           char [] newArray = charArray;
           return newArray;
}
     
        public static void main(String[]args)
        {
         MyString test = new MyString('a');
         test.setAt(1, 'b');
         test.myLineDisplay();
        }
}


MyStringIndexOutOfBoundsException.java

import java.lang.IndexOutOfBoundsException;
/**
*Creates the exceptions specified to the MyString Class
*extends 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);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote