Question: Program Specifications: Because graphics and sound files can be such large files, we often try to squeeze them into a format that takes up less
Program Specifications:
Because graphics and sound files can be such large files, we often try to squeeze them into a format that takes up less space. This is the idea behind MP3 and graphic compression. In this exercise, you will work with a technique we call "Compression".
Compression works by replacing repetitive sequences of identical data items with shorter "tokens" that represent entire sequences. Applying compression 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 repeating character.
A number representing how many times that character repeats in the sequence.
If the string includes spaces, each space will be counted as a character and will be replaced with # (hashtag notation)
If a character does not repeat, it should be left alone. You may assume that the character counts will be single-digit numbers. In other words, a character will not repeat more than 9 times consecutively.
An Example:
qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
After applying compression to the string, the string is converted into:
qw9e5r2ty5q4wE2rT3
The "w9" represents a sequence of 9 consecutive lowercase "w" characters. "e5" represents 5 consecutive lowercase "e" characters, etc. However, the final representation of encoded message will be in uppercase and in the groups of 6 characters separated by a space with last group having the leftover characters.
QW9E5R 2TY5Q4 WE2RT3
Another Example: If the input string contains spaces, each occurence of space will be replaced by hashtag (#) notation. In the following example there are 3 spaces between 'r' and 'w' and single space between 'w' and 'e'.
qwwwwweeeerrr www eerttreee
After applying compression to the string, the string is converted into:
qw5e4r3#3w3#e2rt2re3
The formatted output of Vidan encoder will be: QW5E4R 3#3W3# E2RT2R E3
What your program should do:
public static String getInputString(Scanner keyboard) Prompt the user to enter a string of their choosing. You should use a method called getInputString() to prompt and read in and return the user's input string to the main method. Remember to declare the scanner in the main method and pass it as a parameter to getInputString(). Ensure that the length of the string is at least one or you should display a validation error (see sample run).
public static String viDanCompression(String str) Incorporate a method called Compression() which accepts as a parameter the user's input string and returns the compressed string.
public static String formatCompressed(String compressed) Incorporate a method called formatCompressed() that accepts the compressed string and returns the formatted string. The formatted string will be in Upper case with cluster of 6 characters separated by spaces. Th last cluster may hold less than 6 characters.
Sample Run 1:
Enter string: qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT The compressed string yields: qw9e5r2ty5q4wE2rT3 The formatted output of encoder: QW9E5R 2TY5Q4 WE2RT3
Sample Run 2:
Enter string: Please enter a string of at least length 1
Sample Run 3:
Enter string: wwwwaaadexxxxxx wwwwaaadexxxxxx wwwwaaadexxxxxx The compressed string yields: w4a3dex6#2w4a3dex6#2w4a3dex6 The formatted output of encoder: W4A3DE X6#2W4 A3DEX6 #2W4A3 DEX6
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
