In: Computer Science
I am having an issue with trying to make my code to have a required output
////////////Input//////////
With computer science, you can work in any industry. 0
//////////Output//////////
Required Output
Enter some text to encrypt\n Enter a key\n Error: Key is divisible by 26. That's a bad key!\n Useless key: 0\n
///////////Cipher.java///////// ------------ My code.
public class Cipher
{
private String plainText;
private int key;
public Cipher(String text, int key) throws EmptyPlainText, UselessKeyException
{
if (text == null || text.length() == 0)
{
throw new EmptyPlainText("Error: Nothing to encrypt!");
}
if (key % 26 == 0)
{
throw new UselessKeyException(26, "Error: Key is divisible by 26. That's a bad key!");
}
this.plainText = text;
this.key = key;
}
public String getPlainText()
{
return plainText;
}
public int getKey()
{
return key;
}
public String getCipherText()
{
String cipher = "";
int k = key % 26;
char c;
int integer;
for (int counter = 0; counter < plainText.length(); counter++)
{
c = plainText.charAt(counter);
if (Character.isUpperCase(c))
{
integer = c - 'A';
integer += k;
integer %= 26;
c = (char) (integer + 'A');
} else if (Character.isLowerCase(c))
{
integer = c - 'a';
integer += k;
integer %= 26;
c = (char) (integer + 'a');
} else
c += k;
cipher += c;
}
return cipher;
}
}
///////////CipherDemo/////////
import java.util.Scanner;
public class CipherDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter some text to encrypt");
String input = keyboard.nextLine();
System.out.println("Enter a key");
int key = keyboard.nextInt();
try {
Cipher c = new Cipher(input, key);
System.out.println("Plain text: " + c.getPlainText());
System.out.println("Cipher text: " + c.getCipherText());
System.out.println("Key: " + c.getKey());
} catch (EmptyPlainText e) {
System.out.println(e.getMessage());
} catch (UselessKeyException e) {
System.out.println(e.getMessage());
System.out.println("Useless key: " + e.getUselessKey());
}
}
}
//Java code
public class EmptyPlainText extends Exception {
String message;
public EmptyPlainText()
{
super();
message ="";
}
public EmptyPlainText(String message)
{
super(message);
}
@Override
public String getMessage() {
return message;
}
}
//======================================
public class UselessKeyException extends Exception {
String message;
int uselessKey;
public UselessKeyException()
{
super();
message ="";
uselessKey=0;
}
public UselessKeyException(int uselessKey,String message)
{
this.message = message;
this.uselessKey = uselessKey;
}
@Override
public String getMessage() {
return message;
}
public int getUselessKey() {
return uselessKey;
}
@Override
public synchronized Throwable getCause() {
return super.getCause();
}
}
//=====================================
public class Cipher
{
private String plainText;
private int key;
public Cipher(String text, int key) throws EmptyPlainText, UselessKeyException
{
if (text == null || text.length() == 0)
{
throw new EmptyPlainText("Error: Nothing to encrypt!");
}
if (key % 26 == 0)
{
throw new UselessKeyException(key , "Error: Key is divisible by 26. That's a bad key!");
}
this.plainText = text;
this.key = key;
}
public String getPlainText()
{
return plainText;
}
public int getKey()
{
return key;
}
public String getCipherText()
{
String cipher = "";
int k = key % 26;
char c;
int integer;
for (int counter = 0; counter < plainText.length(); counter++)
{
c = plainText.charAt(counter);
if (Character.isUpperCase(c))
{
integer = c - 'A';
integer += k;
integer %= 26;
c = (char) (integer + 'A');
} else if (Character.isLowerCase(c))
{
integer = c - 'a';
integer += k;
integer %= 26;
c = (char) (integer + 'a');
} else
c += k;
cipher += c;
}
return cipher;
}
}
//==============================
import java.util.Scanner;
public class CipherDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter some text to encrypt");
String input = keyboard.nextLine();
System.out.println("Enter a key");
int key = keyboard.nextInt();
try {
Cipher c = new Cipher(input, key);
System.out.println("Plain text: " + c.getPlainText());
System.out.println("Cipher text: " + c.getCipherText());
System.out.println("Key: " + c.getKey());
} catch (EmptyPlainText e) {
System.out.println(e.getMessage());
} catch (UselessKeyException e) {
System.out.println(e.getMessage());
System.out.println("Useless key: " + e.getUselessKey());
}
}
}
//Output

//If you need any help regarding this solution ........... please leave a comment ..... thanks
"C:\Program Files\Javaljdk1.8.0_221\bin\java.exe" ... Enter some text to encrypt Java Enter a key Error: Key is divisible by 26. That's a bad key! Useless key: 26 Process finished with exit code 0