Please add detailed comments in the following both codes and submit the new code. Code 1 class
Question:
Please add detailed comments in the following both codes and submit the new code.
Code 1
|
Code 2
import java.io.*; import java.util.*; public class Solution { //to keep track of index public static final String alpha = "abcdefghijklmnopqrstuvwxyz"; publicstaticStringencrypt(Stringmessage,intshiftKey) { message = message.toLowerCase(); String cipherText = ""; for (int ii = 0; ii < message.length(); ii++) { int charPosition = aplha.indexOf(message.charAt(ii)); int keyVal = (shiftKey + charPosition) % 26; char replaceVal = aplha.charAt(keyVal); cipherText += replaceVal; } return cipherText; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String message = new String(); int key = 0; System.out.print("Enter the String for Encryption:"); message = sc.next(); System.out.println(" Enter Shift Key:"); key = sc.nextInt(); System.out.println(" Encrpyted msg:" + encrypt(message, key)); } //main method ends } //Solution Class End |
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang