In java please, Write a program called CaesarLite that encodes a message using a
ID: 3717938 • Letter: I
Question
In java please,
Write a program called CaesarLite that encodes a message using a Caesar cipher (https://en.wikipedia.org/wiki/Caesar cipher). The user should be prompted for the number of positions to shift (positive or negative) and a message to encode. For example, a shift of 3 would map A to D and B to E. You can assume that only capital letters need to be shifted and that other characters will not be changed. For this version, you can assume that the shift never of the alphabet. Example 1: Enter shift: 4 Enter message: HELLO Encoded message: LIPPS Example 2: Enter shift: -1 Enter message: IBM 9000 Encoded message: HAL 9000Explanation / Answer
CaesarLite.java
import java.util.Scanner;
public class CaesarLite {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter shift:");
int n = scan.nextInt();
scan.nextLine();
System.out.println("Enter message:");
String s = scan.nextLine();
System.out.println("Encoded message:");
for(int i=0;i<s.length();i++) {
if(Character.isAlphabetic(s.charAt(i))) {
System.out.print((char)(s.charAt(i)+n));
} else {
System.out.print(s.charAt(i));
}
}
System.out.println();
}
}
Output:
Enter shift:
-1
Enter message:
IBM 9000
Encoded message:
HAL 9000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.