Question

In: Computer Science

This is a java assignment inspired by Caesar algorithm with some minor modifications. You are required...

This is a java assignment inspired by Caesar algorithm with some minor modifications. You are required to take in three inputs; a string, which is the message that you want to encrypt, an integer number that we call it salt, which is going to salt your plain message to help create a better encrypted message and an int to be used for displacement of the letters.

Your algorithm is going to encode the message letter by letter. If the position of the letter in your message is odd, then it should add the value of this letter to the displacement value plus salt. If the letter is in even position, then it should be added to the displacement subtracted from salt. For example, assume displacement = 3 and salt = 5, then letter ‘A’ will be replaced by ‘I’, if it is in odd position and by ‘C’ if it is in even position.

The message that you read can contain any character that is typeable (i.e. you can see the typable characters on your keyboard). Your program finally should output the encrypted message.

Sample input and corresponding output:

Input:

Let's meet somewhere cool. How about Tuesday at 9:30 in Chemistry 0400?

5

3

Output:

           Jmr/q(kmc|_{muc•fmpm_kmwj6_Pm•_i`ws|_\smql_?_ir(7B18_ql(Apcu
     g{rzw(.<.8=

JAVA JAVA

Solutions

Expert Solution

Given below is the code for the question. Please do rate it if it helped. Thank you.


Encrypt.java
------
import java.util.Scanner;

public class Encrypt {
   public static String encrypt(String msg, int displacement, int salt) {
       String s = "";
       for(int i = 0; i < msg.length(); i++) {
           char c = msg.charAt(i);
           if(i % 2 == 1) // odd position
           {
               c = (char)(c + displacement + salt);
           }
           else {
               c = (char)(c + displacement - salt);
           }
           s += c;
       }
       return s;
   }
  
   public static void main(String[] args) {
       Scanner scnr = new Scanner(System.in);
       String msg;
       int salt, displacement;
      
       System.out.println("Enter a message to be encrypted");
       msg = scnr.nextLine();
       System.out.print("Enter salt: ");
       salt = scnr.nextInt();
       System.out.print("Enter displacement: ");
       displacement = scnr.nextInt();
      
       System.out.println("The encrypted message is ");
       System.out.println(encrypt(msg, displacement, salt));
   }
}


Related Solutions

Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
Use python simulations to answer the following questions. You should be able to make minor modifications...
Use python simulations to answer the following questions. You should be able to make minor modifications to previous simulations to estimate these probabilities: 1) Suppose the pocket contains one fair coin and one two-headed coin. Find the approximate probability that the coin is fair given that it came up heads on the first flip. 2) Suppose the pocket contains one fair coin and one two-headed coin. Find the approximate probability that the coin is fair given that it came up...
JAVA The previous assignment for the semester project required that you submit a document that included...
JAVA The previous assignment for the semester project required that you submit a document that included class diagram document that used UML notation. By now (if not sooner), you should have a good idea of what classes you will need to include in your Java Application.You may, in fact, had identified additional classes you need to implement. For this assignment, you are to write your classes that are required for your Java application (minimum of three). Additionally, you will need...
IN JAVA Minimal Documentation Required (no javadoc) Purpose The purpose of this assignment is to introduce...
IN JAVA Minimal Documentation Required (no javadoc) Purpose The purpose of this assignment is to introduce you to basic operations on a linked list. Specifics Design a program that generates a linked list of randomly generated Integer objects. Present a menu at program start that gives the user the following options (most of these options will have corresponding methods in a Linked List class): 1. Create a new list. The size will be specified by the user, make sure a...
0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane...
0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane figures from elementary geometry. The object of this assignment is not to do anything useful, but rather to demonstrate how methods can be inherited by extending classes. 1. Theory. A polygon is a closed plane figure with three or more sides, all of which are line segments. The perimeter of a polygon is the sum of the lengths of its sides. A rectangle is...
Caesar Cipher in Java Problem? Objective Practice the cumulative sum and char data type Problem You...
Caesar Cipher in Java Problem? Objective Practice the cumulative sum and char data type Problem You want to create an app to encrypt the text messages that you send to your friends. Once your friend gets the message, the message should be decrypted so that your friend understands it. To implement this app Caesar cipher algorithm should be used. Caesar Cipher text is formed by rotating each letter by a given amount. For example, if you rotate the letter ‘A’...
It is required to develop an application in Java to perform some operations of a bank....
It is required to develop an application in Java to perform some operations of a bank. The application will have the following classes: Class called Person having following data fields (private): firstName (String), lastName(String), gender(char), cpr(long), mobile(String) and following methods: Constructor having 5 parameters to initialize all data fields, Set and get methods for all data fields, toString method to return String equivalent of all data fields, Abstract class called Account having following data fields(protected): accountHolder(Person), ccountNum(long), balance(double)and following methods:...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT