Consider a method replaceAll for Bag that takes two arguments and replaces each
ID: 3663236 • Letter: C
Question
Consider a method replaceAll for Bag that takes two arguments and replaces each occurrence
of the first argument in the bag with the second. For example, if a bag of strings contained “hello”,
“there”, and “hello”, then replaceAll("hello", "hi") would transform the bag contents into “hi”,
“there”, “hi”.
(a) Implement JUnit test cases for this method.
(b) Provide an implementation of this method for the class OurBag<T> that we wrote.
(c) Provide an implementation of this method for the class LinkedBag<T> that we wrote.
Explanation / Answer
We can write the above program in JAVA shown as below
import java.lang.String;
public class StringReplaceAllExample {
public static void main(String[] args) {
String str = "Introduction 1231 to 124 basic 1243 programming 34563 concepts 5455";
String Str1 = str.replaceAll("[0-9]+", "");
System.out.println(Str1);
Str1 = str.replaceAll("[a-zA-Z]+", "Java");
System.out.println(Str1);
}
}
Output of the above program would be
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.