Question: Need help with adjusting my code. Getting a red error for certain problems that have a longer key input from user. It is supposed to
Need help with adjusting my code. Getting a red error for certain problems that have a longer key input from user. It is supposed to display key but shows Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the given text using the key.
Use the following cipher:
Take the key and mod it by Example: a key of becomes
If the character is a letter, shift it by the key, but 'wrap around' the alphabet if necessary.
If the character is not a letter, then shift it by the key but do not wrap.
Check the test cases for example.
Make getters to support the CipherDemo. Also, make two custom Exceptions called UselessKeyException and EmptyPlainText. In your constructor, throw UselessKeyException if the key is divisible by and throw EmplyPainText if the plain text is zero characters.
My code:
class EmptyPlainText extends Exception
EmptyPlainTextString s
supers;
class UselessKeyException extends Exception
private final int key;
UselessKeyExceptionString s int key
supers;
this.key key;
int getUselessKey
return this.key;
class Cipher
private String ptext;
private String ctext ;
private int key;
CipherString text, int key throws EmptyPlainText, UselessKeyException
if textisEmpty
throw new EmptyPlainTextError: Nothing to encrypt!";
if key
throw new UselessKeyExceptionError: Key is divisible by That's a bad key!", key;
this.ptext text;
this.key key ;
for int i ; i text.length; i
char ch text.charAti;
if CharacterisLetterch
char shifted charch this.key;
if CharacterisLowerCasech
if shifted z shifted ;
else if CharacterisUpperCasech
if shifted Z shifted ;
ctext shifted;
else
ctext charch this.key;
String getPlainText
return this.ptext;
String getCipherText
return this.ctext;
int getKey
return this.key;
GIVEN CODE:
import java.util.Scanner;
public class CipherDemo
public static void mainString args
Scanner keyboard new ScannerSystemin;
System.out.printlnEnter some text to encrypt";
String input keyboard.nextLine;
System.out.printlnEnter a key";
int key keyboard.nextInt;
try
Cipher c new Cipherinput key;
System.out.printlnPlain text: cgetPlainText;
System.out.printlnCipher text: cgetCipherText;
System.out.printlnKey: cgetKey;
catch EmptyPlainText e
System.out.printlnegetMessage;
catch UselessKeyException e
System.out.printlnegetMessage;
System.out.printlnUseless key: egetUselessKey;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
