Q. Complete the following StringProcessingImpl.java” class ….. so that the unit tests shown below pass?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.passing.unittests; public class StringProcessingImpl implements StringProcessing { public int getWordCount(String input) { return 0; } public int getCapitalsCount(String input) { return 0; } public String encode(String input) { return null; } public String removeRepeatedConsecutiveCharacters(String string, int consecutiveAllowedCount) { return null; } public String replaceRepeatedWordsWith(String input, String... replaceWord) { return null; } } |
Unit test
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.passing.unittests; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class StringProcessingTest { private static final String INPUT = "Logic takes you from A to B, but imagination takes you everywhere"; private StringProcessing sp = null; @Before public void init() { sp = new StringProcessingImpl(); } @Test public void testgetWordCount() { Assert.assertEquals("Wrong", 12, sp.getWordCount(INPUT)); } @Test public void testGetCapitalsCount() { Assert.assertEquals("Wrong", 3, sp.getCapitalsCount(INPUT)); } @Test public void testEncode() { Assert.assertEquals("Wrong", "Ybtvp gnxrf lbh sebz N gb O, ohg vzntvangvba gnxrf lbh rireljurer", sp.encode(INPUT)); } @Test public void testRemoveRepeatedConsecutiveCharacters() { Assert.assertEquals("Wrong", "blah Blah Blah", sp.removeRepeatedConsecutiveCharacters("blaah Blaaaah Blahhhh", 1)); Assert.assertEquals("Wrong", "blaah Blaah Blahh", sp.removeRepeatedConsecutiveCharacters("blaah Blaaaah Blahhhh",2)); } @Test public void testReplaceRepeatedWords() { Assert.assertEquals("Wrong", "Logic takes you from A to B, but imagination recognises us everywhere", sp.replaceRepeatedWordsWith (INPUT, "recognises", "us")); //takes -> recognises , you -> us } } |
Solution
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
package com.passing.unittests; import java.util.HashSet; import java.util.Set; public class StringProcessingImpl implements StringProcessing { public int getWordCount(String input) { if(input == null || input.length() == 0){ return 0; } String[] split = input.split("\\s+"); // regex to split on 1 or more spaces return split.length; } public int getCapitalsCount(String input) { if(input == null || input.length() == 0){ return 0; } int counter = 0; char[] charArray = input.toCharArray(); for (char c : charArray) { if(Character.isUpperCase(c)) { counter++; } } return counter; } /** * System.out.println((int) ('Y' - 'L')); //13 System.out.println((int) ('N' - 'A')); //13 */ public String encode(String input) { if(input == null || input.length() == 0){ return input; } StringBuilder sb = new StringBuilder(); for (char c : input.toCharArray()) { if (c >= 'a' && c <= 'm') { c += 13; } else if (c >= 'A' && c <= 'M'){ c += 13; } else if (c >= 'n' && c <= 'z'){ c -= 13; } else if (c >= 'M' && c <= 'Z'){ c -= 13; } //System.out.println(c + " is " + n); sb.append(c); } return sb.toString(); } public String removeRepeatedConsecutiveCharacters(String input, int consecutiveAllowedCount) { if(input == null || input.length() == 0){ return input; } char[] charArray = input.toCharArray(); char prev_c = ' ', cons_char_seen_count = 0; StringBuilder sb = new StringBuilder(input.length()); for (char current_c : charArray) { if(current_c == prev_c){ ++cons_char_seen_count; } else { cons_char_seen_count = 0; // resetting the count prev_c = current_c; } if(cons_char_seen_count < consecutiveAllowedCount){ sb.append(current_c); } } return sb.toString(); } /** * varargs is used with ... */ public String replaceRepeatedWordsWith(String input, String... replaceWord) { if(input == null || input.length() == 0){ return input; } Set<String> processedWords = new HashSet<String>(20); StringBuilder sb = new StringBuilder(50); int index = 0; for (String word : input.split("\\s+")) { if(!processedWords.contains(word)){ sb.append(word + " "); processedWords.add(word); } else { sb.append(replaceWord[index] + " "); index++; } } return sb.toString().trim(); } } |
Key points #1. The encode method is using the rot13 encoding that is used for hiding offensive material from…