Question

Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE...

Write your code in the file StringRec.java. For this problem, the following restrictions apply:

YOUR CODE MUST BE RECURSIVE.

Do not use loops (while, do/while, or for).

Do not declare any variables outside of a method. You may declare local variables inside a method.

Complete the following method:

public static String decompress(String compressedText): Decompress the input text, which has been compressed using the RLE algorithm (previous hw assignment):

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:

qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT


After applying the RLE algorithm, this string is converted into:

q9w5e2rt5y4qw2Er3T

In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w" characters. "5e" represents 5 consecutive lowercase "e" characters, etc.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//importing packages

import java.util.*;

import java.io.*;

//Declaring class name as StringRec

public class StringRec{

//main method start

    public static void main(String[] args) {

//Asking user for enter text compressing text

        System.out.println("What text do you want to decompress?");

//reading input and storing into text variable

        String text = IO.readString();

//calling decompress method for printing o/p

        System.out.println(decompress(text));

    }

// decompress method as static takes text string type argument and returns String variable

    public static String decompress(String text) {

//if input length is less than 1 then return text      

if (text.length()<=1){

            return text;

        }

//declaring local variables

        String firstcharacter="";

        String rest="";

        char c = text.charAt(0);

        if (Character.isLetter(c) == true) {

            firstcharacter = text.substring(0,1);

            rest = text.substring(1);

            return firstcharacter + decompress(rest);

        } else {

            firstcharacter = text.substring(1,2);

            rest = text.substring(2);

            int x = text.charAt(0)-'0';

            char y = text.charAt(1);

            String tst = "";

//declaring local variable in else block

int i = 0;

//calling append method for recursively appending reaming string

append(String tst);

            }//else

return tst + decompress(rest);

        } //decompress()

//append static method takes tst string type argument and returns String variable

public static String append(String tst) {

            if(i >= x) {

             i = 0;

           return tst;

            }

            else return append(tst + y);

         }//append()

}//main

}//class

i/p: qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT

o/p: q9w5e2rt5y4qw2Er3T

Add a comment
Know the answer?
Add Answer to:
Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data...

    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...

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

  • In C code 1. Write and submit the algorithm OR flowchart to indicate you understand the...

    In C code 1. Write and submit the algorithm OR flowchart to indicate you understand the problem 2. Create a project and name the source code lastname_firstname_prog5.c 3. Use the prog5.c as a guide, copy/ paste into your project source code *** build run and test, the outline code - You will need to declare an integer variable called again and initialize it 4. Add the function prototype and implement the function definition for the Greeting function 5. Add the...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • 10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use...

    10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use the main method to test your code. Submit the file AppendRec.java. There is no need to delete the main method, the autograder will only grade appendNTimes. The method appendNTimes is recursive and takes two arguments, a string and an integer. It returns the original string appended to the original string n times. The method signature is as follows public static String appendNTimes ( String...

  • In this problem, you will write a LC-3 assembly code that removes blank spaces from a...

    In this problem, you will write a LC-3 assembly code that removes blank spaces from a string. Assume that the string starts at memory location 0x5000, and is terminated by a ‘\0’ character (ASCII value = 0). Your program should store the modified string in the memory location starting at 0x5100. You do not need to modify the original string stored at 0x5000. You can assume that the original string at 0x5000 will always be less than 100 characters in...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • need source code for NetBeans here is the file Write a class PrimeSequence that implements the Sequence inte...

    need source code for NetBeans here is the file Write a class PrimeSequence that implements the Sequence interface of Worked Example 10.1* and produces a sequence of prime numbers. * See Files section bj6_ch10_we.pdf Hint: Pseudocode to produce next prime: class Prime Sequence implements Sequence instance variable int p function next ()I isPrime-true While (isPrime) increment p; for (d-2; until d*d > p divides p) isPrime-false; d+) if (d if isPrime) return p; isPrime-true Use your PrimeSequence class to generate...

  • You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process th...

    You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process the tokens and determine if there are errors in the program. This program have 3 files Lexer.java, Token.java, Parser.java Part 1 Lexer: The class Lexer should have the following methods: public constructor which takes a file name as a parameter○private getInput which reads the data from the...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT