Using methods on the first picture, can someone help me write a java program to
ID: 3703056 • Letter: U
Question
Using methods on the first picture, can someone help me write a java program to make it do what is in picture 2 and 3, but I wanted to make sure that whoever helps me can make method Num 4 do exactly as it is described in the picture 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 (stringt sentences) data that it receives, line by line This method displays all of the elements of the array of (element by element). String 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: eg. shi ft Letter (·A' , 2 7 ) returns . B . . The same operation also applies for lowercase letters a through z e.g. shiftLetterb,-3) returns 'y All other characters are returned unchanged; ie. 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 shift Letter ( ) 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 output 6. public static void displayEncoded (Stringt 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 (Stringt 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 instead.Explanation / 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 keyboard;
public static void main(String[] args) {
keyboard = new Scanner(System.in);
int n;
do
{
System.out.print("How many sentences would you like to encrypt/decrypt (1-10)? ");
n = keyboard.nextInt();
}while(n <1 || n > 10);
String[] sentences = new String[n];
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int shift;
do
{
shift = getShift();
String encoded = "";
for(int i = 0; i < alphabet.length(); i++)
encoded += shiftLetter(alphabet.charAt(i), shift);
System.out.println(alphabet);
System.out.print(encoded);
System.out.print(" Is this the encoding you would like to use y/n? ");
}while(!keyboard.next().toLowerCase().equals("y"));
//get rid of newline before reading sentences
keyboard.nextLine();
getSentences(sentences);
displayOriginal(sentences);
System.out.println();
displayEncoded(sentences, shift);
System.out.println();
displayCombined(sentences, shift);
}
public static int getShift()
{
int shift;
System.out.println();
do
{
System.out.print("What is the shift factor for encryption? ");
shift = keyboard.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] = keyboard.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)
{
System.out.println("The original text:");
for(int i = 0; i < sentences.length; i++)
System.out.println(sentences[i]);
}
public static void displayEncoded(String[] sentences,int shift)
{
System.out.println("The line-by-line encoding is:");
for(int i = 0; i < sentences.length; i++)
System.out.println(convertSentence(sentences[i], shift));
}
public static void displayCombined(String[] sentences, int shift)
{
System.out.println("The combined encoding:");
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.toUpperCase(); //output has all upper case
s = s.replaceAll("\s+|[^A-Z]", ""); //replace all space and non-alphabet character with empty string
//break into 6 char chunks
String s2 = "";
int start = 0, end;
while(start < s.length())
{
end = start + 6;
if(end > s.length())
end = s.length();
s2 += " " + s.substring(start, end);
start = end;
}
s2 = s2.trim(); //remove leading whitespace
return s2;
}
}
output
======
How many sentences would you like to encrypt/decrypt (1-10)? -1
How many sentences would you like to encrypt/decrypt (1-10)? -2
How many sentences would you like to encrypt/decrypt (1-10)? 11
How many sentences would you like to encrypt/decrypt (1-10)? 12
How many sentences would you like to encrypt/decrypt (1-10)? 1234
How many sentences would you like to encrypt/decrypt (1-10)? 4
What is the shift factor for encryption? 20
ABCDEFGHIJKLMNOPQRSTUVWXYZ
UVWXYZABCDEFGHIJKLMNOPQRST
Is this the encoding you would like to use y/n? y
Enter your 4 sentences below:
Sentence 1> Yes we'll walk with a walk that is measured and slow.
Sentence 2> And we'll go where the chalk-white arrows go,
Sentence 3> For the children, they mark, and the children, they know
Sentence 4> The place where the sidewalk ends.
The original text:
Yes we'll walk with a walk that is measured and slow.
And we'll go where the chalk-white arrows go,
For the children, they mark, and the children, they know
The place where the sidewalk ends.
The line-by-line encoding is:
SYMQYF FQUFEQ CNBUQU FENBUN CMGYUM OLYXUH XMFIQ
UHXQYF FAIQBY LYNBYW BUFEQB CNYULL IQMAI
ZILNBY WBCFXL YHNBYS GULEUH XNBYWB CFXLYH NBYSEH IQ
NBYJFU WYQBYL YNBYMC XYQUFE YHXM
The combined encoding:
SYMQYF FQUFEQ CNBUQU FENBUN CMGYUM OLYXUH XMFIQU HXQYFF AIQBYL YNBYWB UFEQBC NYULLI QMAIZI LNBYWB CFXLYH NBYSGU LEUHXN BYWBCF XLYHNB YSEHIQ NBYJFU WYQBYL YNBYMC XYQUFE YHXM
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.