Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Statement: Create a new project called Lab8-SimpleEncoderDecoder. Add the SimpleEncoderDecoder.java starter code provided. Making use of the existing methods, construct two encoding and decoding

Problem Statement: Create a new project called Lab8-SimpleEncoderDecoder. Add the SimpleEncoderDecoder.java starter code provided.

Making use of the existing methods, construct two encoding and decoding schemes. Implement these as methods. Skeleton code and method headers have been provided. Once you have completed your decoding methods, try to decode the messages in secret1.txt and secret2.txt.

This is secret1.txt:

ynivnodko9obogdkrgobkog

dmkxkdyxcsomxovvomho

dslkrkcsds

deyqxsfsvkoukwog

ynogdkrgpy

deyopsvkoukwog

ofsqogdkrgpy

This is secret2.txt:

nbycockbobo

ofsyvodckbolve

csmkxnomynowocckqo

erygklyediy

im9cyxoosqrd

The code:

/*

* Author:

* Date:

*

* Description: A set of utility methods than can be used to

* encode or decode messages. The types of encodings/decodings

* involve rotating characters, rotating strings and reversing

* strings.

*/

import java.util.Scanner;

import java.io.File;

import java.io.IOException;

public class SimpleEncoderDecoder {

public static void main(String[] args) throws IOException {

File f = new File("secret1.txt");

Scanner scanner = new Scanner(f);

System.out.println("secret1.txt as decoded by decoder1...");

// While there are more lines to decode for secret1.txt ...

while(scanner.hasNextLine()) {

String line = scanner.nextLine();

//Display decoded line

System.out.println(decoder1(line));

}

scanner.close();

f = new File("secret2.txt");

scanner = new Scanner(f);

System.out.println(" secret2.txt as decoded by decoder2...");

// While there are more lines to decode for secret2.txt...

while(scanner.hasNextLine()) {

String line = scanner.nextLine();

//Display decoded line

System.out.println(decoder2(line));

}

}

// YOUR CODE BELOW TO COMPLETE encoder1, decoder1, encoder2

// AND decoder2

// START MODIFICATIONS

/**************************************************************************

* encoder1 - Encodes by shifting each character by 10 positions (e.g.,

* a -> k) and then reversing the order of the characters in the string.

* As an example 'ab' would encode to 'lk'.

*

* String message - A string comprised only of lower case letters.

* return encoded - An encoded string

*/

public static String encoder1(String message) {

String encoded = "";

return encoded;

}

/**************************************************************************

* decoder1 - Decodes by shifting each character back 10 positions (e.g.,

* k -> a) and then reversing the order of the characters in the string.

* As an example 'lk' would encode to 'ab'.

*

* String code - A string encoded by encoder1.

* return decoded - The decoded string

*/

public static String decoder1(String code) {

String decoded = "";

return decoded;

}

/**************************************************************************

* encoder2 - Encodes by shifting each character by 10 positions (e.g.,

* a -> k) and then rotating the order of string.

* As an example 'abc' would encode to 'mkl'.

*

* String message - A string comprised only of lower case letters.

* return encoded - An encoded string

*/

public static String encoder2(String message) {

String encoded = "";

return encoded;

}

/**************************************************************************

* decoder2 - Decodes by shifting each character back 10 positions (e.g.,

* k -> a) and then rotating the characters in the string.

* As an example 'mkl' would encode to 'abc'.

*

* String code - A string encoded by encoder2.

* return decoded - The dencode string

*/

public static String decoder2(String code) {

String decoded = "";

return decoded;

}

// END MODIFCATIONS

/**************************************************************************

* retainAlphaNumeric - Forms a new string by removing any non alphanumeric

* characters or upper case letters from given string

*

* String x - Input string which may contain spaces, punctuation, etc.

* return keepers - A string comprised only of lower case letters

*/

public static String retainAlphaNumeric(String x) {

String keepers = "";

for(int i = 0; i < x.length(); i++) {

if(Character.getNumericValue(x.charAt(i)) >= 10 &&

Character.getNumericValue(x.charAt(i)) <= 36) {

keepers += x.charAt(i);

} else {

keepers += "";

}

}

return keepers;

}

/**************************************************************************

* reverseString - Reverses the order of the characters in a string

*

* String x - input string

* return rX - The input string in reverse order

*/

public static String reverseString(String x) {

String rX = "";

for(int i = 0; i < x.length(); i++) {

rX += x.charAt(x.length() - 1 - i);

}

return rX;

}

/**************************************************************************

* char2Position - Maps a lower case letter to an ordinal integer value

*

* char x - A lower case letter.

* return - Integer value representing the 'position' of the letter in

* the alphabet (e.g., 1 for a).

*/

public static int char2Position(char x) {

return Character.getNumericValue(x) - 9;

}

/**************************************************************************

* position2Char - Maps an ordinal integer to a lower case letter.

*

* int x - An integer value representing the position of a letter in the alphabet

* return - The xth lower case letter for a given x.

*/

public static char position2Char(int x) {

return Character.forDigit(x + 9, 36);

}

/**************************************************************************

* offsetEncoder - Shifts all of the characters in a string by a specified

* offset, wrapping around the end of the alphabet (i.e., z shifted by 2

* becomes b).

*

* String x - A string to encode comprised only of lowercase letters

* int offset - A positive number of positions to shift each character

* return - The encoded string

*/

public static String offsetEncoder(String x, int offset) {

String encoded = "";

for(int i = 0; i < x.length(); i++) {

int newPosition = char2Position(x.charAt(i));

newPosition = (newPosition + offset) % 26;

encoded += position2Char(newPosition);

}

return encoded;

}

/**************************************************************************

* offsetDecoder - Shifts all of the characters in a string by a specified

* offset, wrapping around the end of the alphabet (i.e., b shifted down by 2

* becomes z).

*

* String x - A string decoded by offsetEncoder comprised only of lowercase letters

* int offset - A positive number of positions to shift each character

* return - The decoded string

*/

public static String offsetDecoder(String x, int offset) {

String decoded = "";

for(int i = 0; i < x.length(); i++) {

int newPosition = char2Position(x.charAt(i));

newPosition = (newPosition - offset);

if(newPosition <= 0) {

newPosition += 26;

}

decoded += position2Char(newPosition);

}

return decoded;

}

/**************************************************************************

* rotateEncoder - Encodes a string by rotating it such that the last

* character in the string becomes the first and every other character

* is shifted one position to the right.

*

* String x - Input string

* return encoded - The encoded string in which the last character of the

* input string has been wrapped around to become the first character.

*/

public static String rotateEncoder(String x) {

String encoded = x;

if(x.length() >= 2) {

encoded = x.charAt(x.length() - 1) + x.substring(0, x.length() - 1);

}

return encoded;

}

/**************************************************************************

* rotateEncoder - Encodes a string by rotating it such that the last

* character in the string becomes the first and every other character

* is shifted one position to the right. This is repeated numRotation

* times.

*

* String x - Input string

* int numRotations - The number of times the string should be rotated

* return encoded - The encoded string in which the last character of the

* input string has been wrapped around to become the first character.

*/

public static String rotateEncoder(String x, int numRotations) {

String encoded = x;

for(int i = 0; i < numRotations; i++) {

encoded = rotateEncoder(encoded);

}

return encoded;

}

/**************************************************************************

* rotateDecoder - Decodes a string by rotating it such that the first

* character in the string becomes the last and every other character

* is shifted one position to the left.

*

* String x - Input string that has been encoded by rotateEncoder

* return decoded - The encoded string in which the first character of the

* input string has been wrapped around to become last first character.

*/

public static String rotateDecoder(String x) {

String decoded = x;

if(x.length() >= 2) {

decoded = x.substring(1, x.length()) + x.charAt(0);

}

return decoded;

}

/**************************************************************************

* rotateDecoder - Decodes a string by rotating it such that the first

* character in the string becomes the last and every other character

* is shifted one position to the left. This is repeated numRotation

* times.

*

* String x - Input string that has been encoded by rotateEncoder

* int numRotations - The number of times to apply the rotation

* return decoded - The encoded string in which the first character of the

* input string has been wrapped around to become last first character.

*/

public static String rotateDecoder(String x, int numRotations) {

String decoded = x;

for(int i = 0; i < numRotations; i++) {

decoded = rotateDecoder(decoded);

}

return decoded;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 3 Lnai 9853

Authors: Bettina Berendt ,Bjorn Bringmann ,Elisa Fromont ,Gemma Garriga ,Pauli Miettinen ,Nikolaj Tatti ,Volker Tresp

1st Edition

3319461303, 978-3319461304

More Books

Students also viewed these Databases questions