In java, please and thank you so much!!! Write a program called CaesarLite that
ID: 3715295 • Letter: I
Question
In java, please and thank you so much!!!
Write a program called CaesarLite that encodes a message using a Caesar cipher (httos:/len. wikipedia.org/wiki/Caesar cioher). 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 Bto 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 goes past the beginning or end 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 9000 Problem 2 Write a program called Caesar that encobes messages using an improved Caesar cipher. As in the previous problem, the user should be prompted for the number of positions to shift and a message to encode. For this version, however, shifting beyond the end (or beginning) of the alphabet wraps around to the beginning (or end). For example, a shift of 3 would map A to D, B to E, and Y to B. And a shift of -3 would map A to x, B to Y, and Y to V. You can assume that only capital letters need to be shifted and that other characters will not be changed. Example 1: Enter shift: 4 Enter message: HELLO WORLD Encoded message: LIPPs ASVPH Example 2: Enter shift: -1 Enter message: IBM 9000 Encoded message: HAL 9000Explanation / Answer
CeasarLite.Java
import java.util.Scanner;
public class CeasarLite {
private static String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String message = null;
private int shift=Integer.MAX_VALUE;
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* @return the shift
*/
public int getShift() {
return shift;
}
/**
* @param shift the shift to set
*/
public void setShift(int shift) {
this.shift = shift;
}
/* returns the encoded message*/
public String getEncodedMsg(){
String cipherText ="";
if(message==null || shift == Integer.MAX_VALUE){
return null;
}
for(int i=0;i<message.length();i++){//interacting over the characters in message
char char1 = message.charAt(i);
int charPos = alphabets.indexOf(char1);//finding whether the same is an uppercase alphabet
if(charPos==-1){
cipherText+=char1;//if yes then just add without encode
}else{
charPos+=shift;//cipher character position = original position in alphabets + shift
if(charPos>=0 && charPos<alphabets.length()){//condition to skip for when position beyond alphabets postion range
cipherText+=alphabets.substring(charPos, charPos+1);//encode the message character
}
}
}
return cipherText;
}
public static void main(String[] args) {
//accept inputs and encode the message
CeasarLite object = new CeasarLite();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the shift:");
object.setShift(Integer.parseInt(scanner.nextLine()));
System.out.println("Enter the message:");
object.setMessage(scanner.nextLine());
System.out.println("Encoded message:"+object.getEncodedMsg());
}
}
OUTPUT
Enter the shift:
4
Enter the message:
HELLO
Encoded message:LIPPS
Enter the shift:
-1
Enter the message:
IBM 9000
Encoded message:HAL 9000
package apr242018;
import java.util.Scanner;
Ceasar .Java
public class Ceasar {
private static String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String message = null;
private int shift=Integer.MAX_VALUE;
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* @return the shift
*/
public int getShift() {
return shift;
}
/**
* @param shift the shift to set
*/
public void setShift(int shift) {
this.shift = shift;
}
/* returns the encoded message*/
public String getEncodedMsg(){
String cipherText ="";
if(message==null || shift == Integer.MAX_VALUE){
return null;
}
for(int i=0;i<message.length();i++){//interacting over the characters in message
char char1 = message.charAt(i);
int charPos = alphabets.indexOf(char1);//finding whether the same is an uppercase alphabet
if(charPos==-1){
cipherText+=char1;//if yes then just add without encode
}else{
charPos+=shift;//cipher character position = original position in alphabets + shift
charPos%=alphabets.length();//making circular reference
if(charPos>=0 && charPos<alphabets.length()){//condition to skip for when position beyond alphabets postion range
cipherText+=alphabets.substring(charPos, charPos+1);//encode the message character
}
}
}
return cipherText;
}
public static void main(String[] args) {
//accept inputs and encode the message
Ceasar object = new Ceasar();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the shift:");
object.setShift(Integer.parseInt(scanner.nextLine()));
System.out.println("Enter the message:");
object.setMessage(scanner.nextLine());
System.out.println("Encoded message:"+object.getEncodedMsg());
}
}
OUTPUT
Enter the shift:
4
Enter the message:
HELLO WORLD
Encoded message:LIPPS ASVPH
Enter the shift:
-1
Enter the 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.