Question

software testing without making any changes to the java classes provided below, come up with some...

software testing

without making any changes to the java classes provided below, come up with some junit test cases to test the code.

To get you started, here are four test cases you must implement:

• Use the setString() function in MyCustomStringInterface to set the value to “H3y, l3t'5 put s0me d161ts in this 5tr1n6!11!!”. Then test to see if the CountNumbers() function is equal to the number of sequential digits in the original string (in this case, 9).

• Use the setString() function in MyCustomStringInterface to set the value to “Peter Piper picked a peck of pickled peppers.”. Then test to see if reverseNCharacters() function returns the reversed string when the characters are reversed in groups of 4 and padding is disabled (in this case, “etePiP r repkcipa decep fo kcip delkpep srep.”).

MyCustomString

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

}
}

MyCustomStringInterface


}
string = tmpString.toString();
}
}


public interface MyCustomStringInterface {

  
String getString();

  
void setString(String string);

  
int countNumbers();


String reverseNCharacters(int n, boolean padded);

void convertDigitsToNamesInSubstring(int startPosition, int endPosition);
}
MyIndexOutOfBoundsException

public class MyIndexOutOfBoundsException extends RuntimeException {
   private static final long serialVersionUID = 8226094121089030034L;

   public MyIndexOutOfBoundsException(String message) {
       super(message);
   }

   public MyIndexOutOfBoundsException() {
       super();
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, I have answered previous parts of this question. I have attached two files below – MyCustomStringTest.java containing the specified tests and another file GenericTests.java containing some unspecified test cases for testing all methods (custom test cases). Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//MyCustomStringTest.java

import static org.junit.Assert.*;

import org.junit.Test;

public class MyCustomStringTest {

    private MyCustomStringInterface myCustomStr;

    // JUnit test method for testing countNumbers method

    @Test

    public void testCountNumbers() {

        // initializing MyCustomString object

        myCustomStr = new MyCustomString();

        // using setString, updating the text

        myCustomStr.setString("H3y, l3t'5 put s0me d161ts in this 5tr1n6!11!!");

        // ensuring that the countNumbers return 9

        assertEquals(myCustomStr.countNumbers(), 9);

    }

    // JUnit test method for testing reverseNCharacters method

    @Test

    public void testReverseNCharacters() {

        // initializing MyCustomString object

        myCustomStr = new MyCustomString();

        // using setString, updating the text

        myCustomStr.setString("Peter Piper picked a peck of pickled peppers.");

        // ensuring that correct String is returned when the characters are

        // reversed in groups of 4 and padding is disabled

        assertEquals(myCustomStr.reverseNCharacters(4, false),

                 "etePiP r repkcipa decep fo kcip delkpep srep.");

        // ensuring that correct String is returned when the characters are

        // reversed in groups of 4 and padding is enabled

        assertEquals(myCustomStr.reverseNCharacters(4, true),

                 "etePiP r repkcipa decep fo kcip delkpep srepXXX.");

    }

    // JUnit test method for testing convertDigitsToNamesInSubstring method

    @Test

    public void testConvertDigitsToNamesInSubstring() {

        // initializing MyCustomString object

        myCustomStr = new MyCustomString();

        // using setString, updating the text to "123456789"

        String str = "123456789";

        myCustomStr.setString(str);

        // converting digits between positions 4 and 7 to

        //their written form.

        myCustomStr.convertDigitsToNamesInSubstring(4, 7);

        // ensuring that the string is updated properly

        assertEquals(myCustomStr.getString(), "123Fourfivesixseven89");

    }

}

//GenericTests.java

import static org.junit.Assert.*;

import org.junit.Test;

public class GenericTests {

    // JUnit test method for testing all methods with no fixed specifications

    // for each.

    @Test

    public void testAll() {

        // initializing MyCustomString object

        MyCustomStringInterface myCustomStr = new MyCustomString();

        // using setString, updating the text to some value

        myCustomStr.setString("one 2 three four56 7 nine 47");

        // pass - countNumbers return 4, fail - anything else

        assertEquals(myCustomStr.countNumbers(), 4);

        // using setString, updating the text contains no numbers

        myCustomStr.setString("there are no numbers here");

        // pass - countNumbers return 0, fail - anything else

        assertEquals(myCustomStr.countNumbers(), 0);

        // using setString, updating the text to some value

        myCustomStr.setString("this is some normal text");

        // using reverseNCharacters to reverse each pair of characters with no

        // padding, and verifying the result

        assertEquals(myCustomStr.reverseNCharacters(2, false),

                 "htsii sosemn roam lettx");

        // using reverseNCharacters to reverse each 5 character pair with no

        // padding, and verifying the result

        assertEquals(myCustomStr.reverseNCharacters(5, false),

                 " sihtos sion em lamrtxet");

        // using reverseNCharacters to reverse each 5 character pair with

        // padding enabled, and verifying the result

        assertEquals(myCustomStr.reverseNCharacters(5, true),

                 " sihtos sion em lamrXtxet");

        // using setString, updating the text to some numeric value

        String str = "460";

        myCustomStr.setString(str);

        // converting digits between positions 1 and str.length() to

        // corresponding words

        myCustomStr.convertDigitsToNamesInSubstring(1, str.length());

        // ensuring that the string is updated properly

        assertEquals(myCustomStr.getString(), "Foursixzero");

        // another test case

        str = "1234567";

        myCustomStr.setString(str);

        // converting only the digits between positions 3 and 5. So the 12 at

        // the beginning and 67 at the end will not be affected

        myCustomStr.convertDigitsToNamesInSubstring(3, 5);

        assertEquals(myCustomStr.getString(), "12Threefourfive67");

    }

}

Add a comment
Know the answer?
Add Answer to:
software testing without making any changes to the java classes provided below, come up with some...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test...

    PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test case for the below prompt using the provided java programs Use the setString() function in MyCustomStringInterface to set the value to “Peter Piper picked a peck of pickled peppers.”. Then test to see if reverseNCharacters() function returns the reversed string when the characters are reversed in groups of 4 and padding is disabled (in this case, “etePiP r repkcipa decep fo kcip delkpep srep.”)....

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • I am not passing some of the code when i run the testing . PriorityQueue public...

    I am not passing some of the code when i run the testing . PriorityQueue public class PriorityQueue<T extends Comparable<T>> implements IPriorityQueue<T> {    private Vector<T> theVector = new Vector<>(0, 1);    private int size = 0;    @Override    public void insert(T element) {        theVector.pushBack(element);        size++;        bubbleUp(); }    @Override    public T peekTop() throws java.util.NoSuchElementException {        if (isEmpty()) {            throw new java.util.NoSuchElementException();        }       ...

  • Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden...

    Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override        public int lastIndexOf(Object arg0) { required code }        @Override        public ListIterator<R> listIterator() { required code }        @Override        public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT