Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your boss has come to ask for your help with another task. She would like you to write a simple program that encodes and decodes

Your boss has come to ask for your help with another task. She would like you to write a simple program that encodes and decodes secret messages which she plans to demonstrate to elementary school students to kindle their interest in Computer Science.
The encoding/decoding scheme is very simple. Each letter is substituted by some other letter according to a given mapping as shown below.
// String letters = "abcdefghijklmnopqrstuvwxyz";
String enc = "kngcadsxbvfhjtiumylzqropwe";
For example, every 'a' becomes a 'k' when encoding a text, and every 'k' becomes an 'a' when decoding.
You will write a program, SecretMessage.java, which asks the user whether they would like to encode or decode a message, and then encodes or decodes the message using the mapping above. Capital letters are mapped the same way as the lower case letters above, but remain capitalized.
For example, every 'A' becomes 'K' when encoding a text, and every 'K' becomes an 'A' when decoding. Numbers and other characters are not encoded and remain the same. The program repeatedly asks the user for input until the user quits.
Hints:
Think about decomposing the problem into simpler problems. You will likely find it helpful to
write a few very simple methods first:
isLowerCaseLetter takes a char and returns true if that letter is a lower case letter (between
'a' and 'z'). isUpperCaseLetter and isLetter should work similarly.
toLowerCase takes an upper case letter and returns the same letter in lower case. Do not use
conditionals to implement this (if/else or switch). Instead, remember that a char is a number code. Print that number code for 'A', 'B', 'a', 'b' to see how it looks and come up with a very simple way to change an upper case letter to lower case. toUpperCase should work similarly.
codeChar takes a char and a boolean flag. If the flag is true, it returns the encoded char according to the given mapping. If the flag is false, it returns the decoded char according to the given mapping. Do not use conditionals to do the mapping. Instead, notice that the letter 'a' is encoded to the first character of the given encoding map. The letter 'b' to the second,
and so on. For the decoding, you can write a decoding string by hand that you can use to map the encoded letters 'a' to 'z' to their decoded counterparts.
Given a String in Java you can use the charAt method to get the letter at a given position. For example:
String s = "example";
char first = s.charAt(0); // first is assigned 'e' char second = s.charAt(1); // second is assigned 'x'
Notice that to get the character at the first position we pass the argument 0 to the charAt
method and not 1.
codeMessage takes a String and a boolean flag. If the flag is true, it prints the given message
encoded. If the flag is false, it prints the given message decoded. This method just calls the codeChar method for every character in the String and prints the character that codeChar returns. It is really short and simple.
Given a String in Java you can use the length method to get the number of characters in that String. For example:
String s = "hello";
int len = s.length(); // len is assigned 5

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_2

Step: 3

blur-text-image_3

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

Oracle 12c SQL

Authors: Joan Casteel

3rd edition

1305251032, 978-1305251038

More Books

Students also viewed these Databases questions