Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Declare and implement a class named Compress . This class will have two strin

ID: 3546552 • Letter: 1

Question


1. Declare and implement a class named
Compress.  This class will have two string data members plainText andcompressedText and the following methods:



* a constructor  initializing the strings with null strings.


* a method to take a text consisting of only lower-case alphabets and blank, and to put it into plainText,


* a method to take a text consisting of only lower-case alphabets and blank, and to put it into compressedText,


* a method to compress plainText to compressedText, using the compression techniques called

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Compress {
    String plainText;
    String compressedText;
   
    public Compress() {
        plainText      = "";
        compressedText = "";
    }
   
    void compress(){
        StringBuffer dest = new StringBuffer();
        for (int i = 0; i < plainText.length(); i++) {
            int runLength = 1;
            while (i + 1 < plainText.length()
                    && plainText.charAt(i) == plainText.charAt(i + 1)) {
                runLength++;
                i++;
            }
            dest.append(plainText.charAt(i));
            dest.append(runLength);
        }
        compressedText = dest.toString();
    }
   
    void decompress(){
        StringBuffer dest = new StringBuffer();
        Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]");
        Matcher matcher = pattern.matcher(plainText);
        while (matcher.find()) {
            int number = Integer.parseInt(matcher.group());
            matcher.find();
            while (number-- != 0) {
                dest.append(matcher.group());
            }
        }
        compressedText = dest.toString();
    }
   
    String plainT(){
        return plainText;
    }
   
    String compressT(){
        return compressedText;
    }
   
    public static void main(String[] args) {
        int i=0;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        Compress str1 = new Compress();
        while(true){
        System.out.println("1.read plainText");
        System.out.println("2.read compressedText");
        System.out.println("3.compress");
        System.out.println("4.decompress");
        System.out.println("5.print plainText");
        System.out.println("6.print compressedText");
        System.out.println("7.quit");
       
        System.out.print("Enter Your choice =");
        try {
            i = Integer.parseInt(in.readLine());
        } catch (NumberFormatException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        switch (i) {
        case 1:
            System.out.print("Enter Your String =");
           
            try {
                str1.plainText = in.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case 2 :
            System.out.print("Enter Your Compressed String =");

            try {
                str1.compressedText = in.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case 3:
            str1.compress();
        break;
       
        case 4:
            str1.decompress();
        break;
        case 5:
            System.out.println("plainText ="+str1.plainT());
        break;
        case 6:
            System.out.println("compressedText ="+str1.compressT());
        break;
        case 7:
            System.exit(0);
        break;
        default:
            break;
        }
        }
       
    }

}