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

The Java method removeDuplicates() is designd to remove all duplicte strings in

ID: 3905416 • Letter: T

Question

The Java method removeDuplicates() is designd to remove all duplicte strings in the object.
(Clarification: removeDuplicates() should retain the first instance, and remove subsequent duplicate instances of the string.)

The following JUnit test method for removeDuplicates() has a flaw that is not syntactic. Tht is, assume this test
compiles and runs to completion.
In the test method, words is an instance of an object that stores strings in the order they are added. words has
methods add(), removeDuplicates(), and getFirst(), which do exactly what you would expect from their names. You
can assume that words has been properly instantiated and the add() and getFirst() methods have already been
tested.

@Test
public void testRemoveDuplicates(){
words.add("orange");
words.add("apple");
words.add("apple");
words.add("banana");
words.removeDuplicates();
assertTrue("removeDuplicates method", words.getFirst().equals("orange"));
}

Describe the flaw in terms of the RIPR model. Be as precise, specific, and concise as you
can. For full credit, you must use the terminology introduced in the book.
Rewrite the test method to correct the flaw. (You can add more methods such as toString()
to the object)

Explanation / Answer

Solution:

As per the code given, after the insert (or add() ) operations to words (which stores strings in the order as they are inserted), the states of words should be -> [ "orange", "apple", "apple", "banana" ] .

Now as per mentioned, the order is maintained when the elements are withdrawn from the list, so if we assume that removeDuplicates() works as per description given, we can state, the state of 'words' after the removeDuplicates() should be > [ "orange", "apple", "banana" ]

The flaw:

Now, when we assert our operation, we are not actually asserting the removeDuplicates() ; as the first element which we get from getFirst() still remains "orange". Hence in order to correct this we can make use of "toString()" i.e.

ASSUMING toString() method prints the following : [ "element1", "element2" .... etc

@Test
public void testRemoveDuplicates(){
words.add("orange");
words.add("apple");
words.add("apple");
words.add("banana");

int sizeOfListBeforeRemoving = words.toString().split(",").length;

words.removeDuplicates();

int sizeOfListAfterRemoving = words.toString().split(",").length;

assertTrue("removeDuplicates method", sizeOfListBeforeRemoving != sizeOfListAfterRemoving);

//OR

//assertFalse(sizeOfListBeforeRemoving, sizeOfListAfterRemoving);
}

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