Write your code in the file Compress.java. *******You may NOT use any modules or
ID: 3759533 • Letter: W
Question
Write your code in the file Compress.java.
*******You may NOT use any modules or classes from outside java.lang (do not add any "import" statements to your programs). ******
I need to use the java IO
Your code should go into a method with the following signature. You may write your own main method to test your code. The graders will ignore your main method:
public static String compress (String original){}
Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of:
the number of characters in the sequence
the repeating character
If a character does not repeat, it should be left alone.
FOR EXAMPLE:
consider the following string:
After applying the RLE algorithm, this string is converted into:
In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w" characters. "5e" represents 5 consecutive lowercase "e" characters, etc.
Write a method called compress that takes a string as input, compresses it using RLE, and returns the compressed string. Case matters - uppercase and lowercase characters should be considered distinct. You may assume that there are no digit characters in the input string. There are no other restrictions on the input - it may contain spaces or punctuation. There is no need to treat non-letter characters any differently from letters.
Explanation / Answer
package current;
public class Compress {
public static void main(String[] args) {
String input = "qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT";
System.out.println("Input string: "+input);
System.out.println(" Compressed string: "+Compress.compress(input));
input = "aaaaa$$bNBBBBc @@tqqr";
System.out.println("Input string: "+input);
System.out.println(" Compressed string: "+Compress.compress(input));
}
public static String compress (String original){
String compressed = "";
if(original != null) {
char ch;
int count = 0;
//You may NOT use any modules or classes from outside java.lang (do not add any "import" statements to your programs
//So not using hasmap or arraylist = if they used program is much easier
char chArr[] = new char[original.length()];
int chCount[] = new int[original.length()];
for(int i = 0; i < original.length(); i++)
chCount[i] = 0;
for(int i = 0; i < original.length(); i++) {
boolean found = false;
//check the character is already counted
if(count > 0 && original.charAt(i) == chArr[count-1]) {
found = true;
chCount[count-1] = chCount[count-1]+1;
}
if(!found) {
chArr[count] = original.charAt(i);
chCount[count] = 1;
count++;
}
}
for(int j = 0; j < count; j++) {
if(chCount[j] > 1)
compressed += chCount[j];
compressed += chArr[j];
}
}
return compressed;
}
}
--------------output----------------
Input string:
qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
Compressed string:
q9w5e2rt5y4qw2Er3T
Input string:
aaaaa$$bNBBBBc @@tqqr
Compressed string:
5a2$bN4Bc3 2@t2qr
--------------------output-----------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.