Question
The Java program compress below takes two parameters: a string s and an integer factor, which indicates how many consecutive occurences of any character c
The Java program compress below takes two parameters: a string s and an integer factor, which indicates how many consecutive occurences of any character c should be compressed in the form cn where n is the number of consecutive occurrences of c. The program compresses the input string s by the given compression factor and return the resulting compressed string. For example, given the input string "aabbb" and a factor 2, the resulting compressed string will be "a2b3", but it the factor is 3 instead, then the sequence of two as is not compressed and the resulting string will be "aab3". 1 public static String compress(String s, int factor) { 2 if (factor < 2) 3 throw new IllegalArgumentException("compression factor must be >=2"); 4 if (s.length() <= 2) 5 return s; 6 char last = s.charAt(0); 7 int count = 1; 8 String current = String.valueOf(last); 9 String result = ""; 10 for (int i = 1; i < s.length(); i++) { 11 char c = s.charAt(i); 12 if (c == last) { 13 current += c; 14 count++; 15 } else { 16 if (count >= factor) 17 result += last + String.valueOf(count); 18 else 19 result += current; 20 last = c; 21 current = String.valueOf(c); 22 count = 1; 23 } 24 } 25 if (count >= factor) 26 result += last + String.valueOf(count); 27 else 28 result += current; 29 return result; 30 } Data-flow Testing: For all the variables contained in the compress method above, list all the definition-use pairs, distinguishing between predicate (p-use) and computational uses (c-use). You can use the following table as template. Variable: s, factor Def-use pairs (p-use): (1,2),... (3,4),... Def-use pairs (c-use): (5,6),... (7,8),...
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