1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
cricket20 [7]
3 years ago
14

Write a program that produces a Caesar cipher of a given message string. A Caesar cipher is formed by rotating each letter of a

message by a given amount. For example, if your rotate by 3, every A becomes D; every B becomes E; and so on. Toward the end of the alphabet, you wrap around: X becomes A; Y becomes B; and Z becomes C. Your program should prompt for a message and an amount by which to rotate each letter and should output the encoded message.

Computers and Technology
1 answer:
PilotLPTM [1.2K]3 years ago
8 0

Answer:

Here is the JAVA program that produces Caeser cipher o given message string:

import java.util.Scanner;  //to accept input from user

public class Main {  

   public static void main(String args[]) {  //start of main function

       Scanner input = new Scanner(System.in);  //creates object of Scanner

       System.out.println("Enter the message : ");  //prompts user to enter a plaintext (message)

       String message = input.nextLine();  //reads the input message

       System.out.println("Enter the amount by which by which to rotate each letter : ");  //prompts user to enter value of shift to rotate each character according to shift

       int rotate = input.nextInt();  // reads the amount to rotate from user

       String encoded_m = "";  // to store the cipher text

       char letter;  // to store the character

       for(int i=0; i < message.length();i++)  {  // iterates through the message string until the length of the message string is reached

           letter = message.charAt(i);  // method charAt() returns the character at index i in a message and stores it to letter variable

           if(letter >= 'a' && letter <= 'z')  {  //if letter is between small a and z

            letter = (char) (letter + rotate);  //shift/rotate the letter

            if(letter > 'z') {  //if letter is greater than lower case z

               letter = (char) (letter+'a'-'z'-1); }  // re-rotate to starting position  

            encoded_m = encoded_m + letter;}  //compute the cipher text by adding the letter to the the encoded message

           else if(letter >= 'A' && letter <= 'Z') {  //if letter is between capital A and Z

            letter = (char) (letter + rotate);  //shift letter

            if(letter > 'Z') {  //if letter is greater than upper case Z

                letter = (char) (letter+'A'-'Z'-1);}  // re-rotate to starting position  

            encoded_m = encoded_m + letter;}  //computes encoded message

           else {  

         encoded_m = encoded_m + letter;  } }  //computes encoded message

System.out.println("Encoded message : " + encoded_m.toUpperCase());  }} //displays the cipher text (encoded message) in upper case letters

Explanation:

The program prompts the user to enter a message. This is a plaintext. Next the program prompts the user to enter an amount by which to rotate each letter. This is basically the value of shift. Next the program has a for loop that iterates through each character of the message string. At each iteration it uses charAt() which returns the character of message string at i-th index. This character is checked by if condition which checks if the character/letter is an upper or lowercase letter. Next the statement letter = (char) (letter + rotate);   is used to shift the letter up to the value of rotate and store it in letter variable. This letter is then added to the variable encoded_m. At each iteration the same procedure is repeated. After the loop breaks, the statement     System.out.println("Encoded message : " + encoded_m.toUpperCase()); displays the entire cipher text stored in encoded_m in uppercase letters on the output screen.

The logic of the program is explained here with an example in the attached document.

You might be interested in
Given public class Fishing { byte b1 = 4; int i1 = 123456; long L1 = (long) i1; //Line A short s2 = (short) i1; //Line B byte b2
Aleksandr [31]

Answer:

Line E.

Explanation:

The  given program is as follows:

public class Fishing {

   byte b1 = 4; int i1 = 123456; long L1 = (long) i1;   //Line A

   short s2 = (short) i1;     //Line B

   byte b2 = (byte) i1;     //Line C

   int i2 = (int)123.456;    //Line D

   byte b3 = b1 + 7;     //Line E

   }

In the above code Line E will not compile and give following compilation error:

error: incompatible types: possible lossy conversion from int to byte

This error is coming because in Java b1 + 7 will be interpreted as int variable expression. Therefore in order to make code of Line E work the expression should be type casted as byte.  

The correct code will be as follows:

public class Fishing {

   byte b1 = 4; int i1 = 123456; long L1 = (long) i1;   //Line A

   short s2 = (short) i1;     //Line B

   byte b2 = (byte) i1;     //Line C

   int i2 = (int)123.456;    //Line D

   byte b3 = (byte)(b1 + 7);     //Line E

   }

5 0
3 years ago
Yo I need to know where to find a ps4 for free or a very cheap price please
svetlana [45]
Download offer up from the apple store or play store and search that up there’s many good deals and sometimes comes with games and controls included for cheap!
8 0
2 years ago
Read 2 more answers
Which of the following Office Online apps is most effective for creating spreadsheets?
navik [9.2K]
The answer is C Excel. Excel allows you to make spreadsheets.

Hope this helps :-)
4 0
3 years ago
Read 2 more answers
Waht is the difference between authorization and access control? Access control specifies what a user can do, and authorization
spayn [35]
<h2>Answer:</h2>

Authorization specifies what a user can do, and access control enforces what a user can do.

<h2>Explanation:</h2>

Authorization is simply granting access to an authenticated user of an application. It specifies what a user can and/or cannot do. For example, for a user to access their banking details in an online banking service, they have to be authorized by first authenticating them to prove their identity. Another example is in an organizational system where some users (normally called admins) can access certain database info whereas some other users (normally called the regular users) cannot.

Access control is used to enforce the policies dictated by authorization. In other words, access control enforces the policy of what a user can and/or cannot do. Access control makes authorization possible. It is sometimes called privileges or permissions. For example, the <em>security tab </em>in the operating system of Windows, allow to set access privileges for certain files and/or folders. Another example is in an organizational system where some files on the organization's server are configured in such a way that access to it is restricted and dependent on some further authorization.

5 0
3 years ago
Read 2 more answers
python Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program wi
devlian [24]

Answer:

current_price = int(input("Enter current price: "))

last_months_price = int(input("Enter last month's price: "))

print("This house is $%d. The change is a $%d since last month." % (current_price, current_price - last_months_price))

print("The estimated monthly mortgage is $%d." % int(current_price*0.045/12))

Explanation:

- Ask the user to enter the values for <em>current_price</em> and <em>last_months_price</em>.

- Print the current price.

- Calculate change from last month (current_price - last_months_price) and print it.

- Calculate the mortgage (using given formula (current_price * 0.051) / 12) and print it.

7 0
3 years ago
Read 2 more answers
Other questions:
  • Good participation in music is strictly limited to those who perform well. true or false
    13·2 answers
  • Which of the following function headings arevalid? If they are invalid, explain why.
    12·1 answer
  • Does any one here play destiny on xbox one
    12·1 answer
  • Which of the following instructions should be allowed only in kernel mode? (a) Disable all interrupts. (b) Read the time-of-day
    11·1 answer
  • Review universal data models and discuss how these are being used more widely today. Does a data modeling project using a packag
    9·2 answers
  • How can a signature be added to an email message? Check all that apply.
    10·2 answers
  • AWT
    7·1 answer
  • 1. Write a high level algorithm for cooking a cheeseburger.
    7·1 answer
  • 10) ________ objects control the flow of the application. A) Boundary B) Utility C) Control D) All of the above
    11·1 answer
  • Write a FOR loop that displays the following numbers exactly like this (you must use a loop):
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!