Question
PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a junit test case for the below prompt using the provided
PLEASE HURRY
Software Testing
Don't make any changes to the provided code. Please write a junit test case for the below prompt using the provided java programs
Use the setString() function in MyCustomStringInterface to set the value to 123456789. Then, use the convertDigitsToNamesInSubstring() function to convert numbers between 4 and 7 (inclusive) to their written form. Test to see if the string is now equal to the correct value (in this case, 123fourfivesixseven89).
MyCustomString.java
public class MyCustomString implements MyCustomStringInterface { private String string;
@Override public String getString() { return string; }
@Override public void setString(String string) { this.string = string; }
@Override public int countNumbers() { StringBuffer tmpString = new StringBuffer(); int count=0; boolean inNumber=false; //avoid null pointer exception! if(string==null || string.isEmpty()) return 0; for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); if (Character.isDigit(ch)) { if (!inNumber) { count++; inNumber = true; } } else { if (inNumber) { inNumber = false; } } } return count; }
@Override public String reverseNCharacters(int n, boolean padded) { if (string == null) { throw new NullPointerException(); } if (n <= 0) { throw new IllegalArgumentException(); } StringBuffer tmpString = new StringBuffer(); StringBuffer resultString = new StringBuffer(); StringBuffer currString = new StringBuffer(string);
int i; for (i = 0; i + n < currString.length(); i+=n) { tmpString = tmpString.append(new StringBuffer(currString.substring(i,i+n)).reverse()); } if (padded) { for (int x = n - (currString.length() - i); x > 0; x--) currString.append('X'); } tmpString = tmpString.append(new StringBuffer(currString.substring(i)).reverse());
return tmpString.toString(); }
@Override public void convertDigitsToNamesInSubstring(int startPosition, int endPosition) { if (string == null) { throw new NullPointerException(); } if ((startPosition > endPosition)) { throw new IllegalArgumentException(); }
if (endPosition > string.length() || (startPosition < 1)){ throw new MyIndexOutOfBoundsException(); } StringBuffer tmpString = new StringBuffer(); String tmpDigit = new String(); boolean look_back = false; for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); if ((i < startPosition - 1) || (i > endPosition - 1)) { if(look_back) { look_back = false; } tmpString.append(ch); continue; } else { if (Character.isDigit(ch)) { switch (ch) { case '0': tmpDigit = "Zero"; break; case '1': tmpDigit = "One"; break; case '2': tmpDigit = "Two"; break; case '3': tmpDigit = "Three"; break; case '4': tmpDigit = "Four"; break; case '5': tmpDigit = "Five"; break; case '6': tmpDigit = "Six"; break; case '7': tmpDigit = "Seven"; break; case '8': tmpDigit = "Eight"; break; case '9': tmpDigit = "Nine"; break; } if(look_back) tmpString.append(tmpDigit.toLowerCase()); else tmpString.append(tmpDigit); look_back = true; } else { if(look_back){ look_back = false;} tmpString.append(ch);
} } } string = tmpString.toString(); } }
MyCustomStringInterface.java
/** * This is an interface for a simple class that represents a string, defined * as a sequence of characters. * * This interface should NOT be altered in any way. */ public interface MyCustomStringInterface {
/** * Returns the current string. If the string is null, it should return null. * * @return Current string */ String getString();
/** * Sets the value of the current string. * * @param string The value to be set */ void setString(String string);
/** * Returns the number of numbers in the current string, where a number is defined as an * unbroken sequence of digits. * * If the current string is null, empty, or uninitialized, the method should return 0. * * Examples: * - countNumbers would return 2 for string "My numbers are 11 and 9". * * @return Number of numbers in the current string */ int countNumbers();
/** * Returns a string that consists of all characters in the original string with each segment of n characters * reversed. * If padded is true, the final segment of characters will have the character 'X' added to it enough time to make * a full segment. *
*
* Examples: * - For n=2 and padded=true, the method would return the string with every pair of characters swapped in place, and * if there were an odd number of characters, an X would be added to the last segment before it is reversed. * - For n=3 and padded=false, the method would return the string with every segment of 3 characters reversed in place, * and the final segment would be reversed even if less than 3 characters without any additional characters added. *
* Values n and padded are passed as parameters. The starting character is considered to be in Position 1. * * @param n Determines size of the string segments to be reversed * @param padded Determines whether an incomplete final segment will be padded with 'X'. * @return String with the segments in their original order and the contents of the segments reversed. * @throws NullPointerException If the current string is null or uninitialized. * @throws IllegalArgumentException If "n" less than or equal to zero, and the current string is not null. */ String reverseNCharacters(int n, boolean padded);
/** * Replace the individual digits in the current string, between startPosition and endPosition * (included), with the corresponding English names of those digits, with the first letter of a number (unbroken * string of digits) capitalized. The first character in the string is considered to be in Position 1. * * Examples: * - String 460 would be converted to Foursixzero * - String 416 would be converted to Fouronesix * * @param startPosition Position of the first character to consider * @param endPosition Position of the last character to consider
* @throws NullPointerException If the current string is null or uninitialized. * @throws IllegalArgumentException If "startPosition" > "endPosition" * @throws MyIndexOutOfBoundsException If "startPosition" < 1 or "endPosition" is out of bounds (greater than the * length of the string) and "startPosition" <= "endPosition" */ void convertDigitsToNamesInSubstring(int startPosition, int endPosition); }
MyIndexOutOfBoundsException.java
public class MyIndexOutOfBoundsException extends RuntimeException { private static final long serialVersionUID = 8226094121089030034L;
public MyIndexOutOfBoundsException(String message) { super(message); }
public MyIndexOutOfBoundsException() { super(); } }
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