Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Explain the code by putting comments that explain what the code does Convert all letters to a lowercase Encode each letter by shifting it the

Explain the code by putting comments that explain what the code does

  • Convert all letters to a lowercase
  • Encode each letter by shifting it the right amount using the key, and display the encoded letters into a console window.
  • Encodeeach digit (0 ~ 9) with its ASCII value shifted by the negative value of the key (1 -> 49-key, 0-> 48-key), and display the encoded digits into a console window.
  • Skip all the punctuation marks, blanks, and anything else other than letters and digits and do NOT display them in a console window.

import java.util.*; // for Scanner public class Cipher { public static void main (String [] args) { Scanner console = new Scanner (System.in); System.out.print ("Your Message? ");//asks for message String message = console.nextLine(); System.out.print ("Encoding Key? ");//asks for the key int key = console.nextInt(); Cipher.cipher(message, key); } public static void cipher(String message, int key) {//takes in 2 parameters String encodedMessage="";

for (int i = 0; i < message.length(); i++) {// char c = message.charAt(i); if (Character.isLetter(c)) { char encodedLetter = (char) (c + (key % 26)); if (!Character.isLetter(encodedLetter)) { encodedLetter = (char) (c - (26 - key));//shifts the letter depending on the input key } encodedMessage += encodedLetter;//changes encoded message } else if (Character.isDigit(c)) { int encodedDigit = (int) (c - '0' - key + '0'); if (encodedDigit < '0') { encodedDigit += 10; } encodedMessage += (char) encodedDigit; } } encodedMessage = encodedMessage.toLowerCase();//Change output to all lowercase System.out.println("your message: " + encodedMessage);//prints } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions