Public Methods the Program Must Provide All methods must be implemented as named
ID: 3702077 • Letter: P
Question
Public Methods the Program Must Provide All methods must be implemented as named as as described in order for Web-CAT to be able to test and score them correctly Here are the seven methods you need to write in addition to your main() method for this program 1. public static int getShift() This method asks the user to enter a shift value between -25 and 25 inclusive, displays a conversion chart for the user to approve, and then returns the shift value only if they approve of the conversion chart that's been displayed. Data validation should occur on the value between -25 and 25 2. public static void getSentences (String sentences []) This method allows the user to enter text into each of the elements of the String array that it receives (You will probably need to call the nextLine() method of your Scanner once before reading in sentences.) 3. public static void displayOriginal (String[ sentences) This method displays all of the elements of the array of String data that it receives, line by line (element by element) 4. public static char shiftLetter (char toConvert, int shift) This method will shift uppercase letters A through Z shift positions upwards or downwards within the uppercase letters; e.g. shiftLetter('A, 27) returns 'B' . The same operation also applies for lowercase letters a through z ; e.g. shiftLetter ('b', -3) returns 'y' . All other characters are returned unchanged; i.e. shiftLetter'!', 5) returns '!'. 5. public static String convertSentence (String sentence, int shift) This method will do the actual conversion of a String of data to its encoded equivalent in 6-character chunks of data and return the new, encoded String . It should call on the shiftLetter method to do the actual character conversion for each individual character. In other words, individual character conversion should not happen within this method. This method doesn't do any outpu 6. public static void displayEncoded (String[] sentences, int shift) This method will display in encoded format all of the elements of the array of String data that it receives. It will need to call on the method convertSentence() to convert each string before it displays it. Note that the original array should not be modified with encoded data. 7. public static void displayCombined (String[ sentences, int shift) This method takes an array of String data and combines all of the String s into a single String that is then processed by the convertSentence) method. The method should essentially display the same results as the displayEncoded() method, except that there won't be separate lines of output but rather one large result insteadExplanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;
public class Encoder {
public static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
int numberSentences;
do
{
System.out.print("How many sentences would you like to encrypt/decrypt (1-10)? ");
numberSentences = scan.nextInt();
}while(numberSentences <1 || numberSentences > 10);
String[] sentences = new String[numberSentences];
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int shift;
do
{
shift = getShift();
System.out.println(alphabet);
for(int i = 0; i < alphabet.length(); i++)
System.out.print(shiftLetter(alphabet.charAt(i), shift));
System.out.println();
System.out.println("Is this the encoding you would like to use y/n? ");
}while(!scan.next().toLowerCase().equals("y"));
scan.nextLine(); //get rid of newline before reading sentences
getSentences(sentences);
System.out.println("The original list of strings is");
displayOriginal(sentences);
System.out.println();
System.out.println("The line-by-line encoding is: ");
displayEncoded(sentences, shift);
System.out.println();
System.out.println("The combined encoding: ");
displayCombined(sentences, shift);
}
public static int getShift()
{
int shift;
do
{
System.out.print("What is the shift factor for encryption (-25 to 25)? ");
shift = scan.nextInt();
}while(shift < -25 || shift > 25);
return shift;
}
public static void getSentences(String[] sentences)
{
System.out.printf("Enter your %d sentences below: ", sentences.length);
for(int i = 0; i < sentences.length; i++)
{
System.out.printf("Sentence %d> ", i+1);
sentences[i] = scan.nextLine();
}
}
public static char shiftLetter(char toConvert, int shift)
{
int c;
if(Character.isLowerCase(toConvert))
{
c = toConvert - 'a';
c += shift;
if(c < 0)
c += 26;
else if(c > 25)
c -= 26;
c += 'a';
}
else if(Character.isUpperCase(toConvert))
{
c = toConvert - 'A';
c += shift;
if(c < 0)
c += 26;
else if(c > 25)
c -= 26;
c += 'A';
}
else
c = toConvert; //no change for other characters
return (char)c;
}
public static void displayOriginal(String[] sentences)
{
for(int i = 0; i < sentences.length; i++)
System.out.println(sentences[i]);
}
public static void displayEncoded(String[] sentences,int shift)
{
for(int i = 0; i < sentences.length; i++)
System.out.println(convertSentence(sentences[i], shift));
}
public static void displayCombined(String[] sentences, int shift)
{
String s = "";
for(int i = 0; i < sentences.length; i++)
s += sentences[i];
System.out.println(convertSentence(s, shift));
}
public static String convertSentence(String sentence, int shift)
{
String s ="";
for(int i = 0; i < sentence.length(); i++)
s += shiftLetter(sentence.charAt(i), shift);
s = s.replaceAll("\s+", "");
String newString = s.substring(0, Math.min(6, s.length()));
for(int i = 6; i < s.length(); i += 6)
{
newString += " " + s.substring(i, Math.min(i+6, s.length()));
}
return newString;
}
}
output
======
How many sentences would you like to encrypt/decrypt (1-10)? 3
What is the shift factor for encryption (-25 to 25)? 5
ABCDEFGHIJKLMNOPQRSTUVWXYZ
FGHIJKLMNOPQRSTUVWXYZABCDE
Is this the encoding you would like to use y/n?
y
Enter your 3 sentences below:
Sentence 1> Hi! Good morning.
Sentence 2> How are you?
Sentence 3> Let's go play.
The original list of strings is
Hi! Good morning.
How are you?
Let's go play.
The line-by-line encoding is:
Mn!Ltt irtwsn sl.
Mtbfwj dtz?
Qjy'xl tuqfd.
The combined encoding:
Mn!Ltt irtwsn sl.Mtb fwjdtz ?Qjy'x ltuqfd .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.