// Lab 9 - Model class public class Cipher { private int key; // Constructor pub
ID: 3687124 • Letter: #
Question
// Lab 9 - Model class
public class Cipher {
private int key;
// Constructor
public Cipher(int k) {
key = k;
} // end Constructor
public String encrypt(String message) {
//YOU DO THIS
//For each character in message
//find the character that is key values after it
//in the alphabet. (If the character is 'a' and
//key is 4, you should find 'e'.)
//use ASCII values to find the replacement
//build a string of the replacement characters,
//and return it
//remove the line below when you're done
return "";
} // end encrypt()
public String decrypt(String message) {
//YOU DO THIS
//For each character in message
//find the character that is key values before it
//in the alphabet. (If the character is 'e' and
//key is 4, you should find 'a'.)
//use ASCII values to find the replacement
//build a string of the replacement characters,
//and return it
//remove the line below when you're done
return "";
} // end decrypt()
} // end class
// Lab 9 - View (Console) class
import java.util.*;
public class View {
private Scanner s;
// Constructor
public View() {
s = new Scanner(System.in);
} // end Constructor
public String getMessage() {
//YOU DO THIS
//Ask the user to enter the message
//return what they type
//remove the line below when you're done
return "";
} // end getMessage()
public int getKey() {
//YOU DO THIS
//Ask the user to enter the key
//return what they type
//Use a try/catch with a loop to keep
//asking until they enter an integer
//remove the line below when you're done
return 0;
} // end getKey()
public void displayResult(String msg) {
//YOU DO THIS
//display msg to the console
} // end displayResult
} // end class
PLEASE HELP ME RESCUE ME SAVE ME
Explanation / Answer
public class Cipher {
private int key;
public Cipher(int k) {
key = k;
System.out.println(“The Key is:” + key);
}
public String encrypt(String message) { // message = msg (user entered)
string s;
char ch;
int index;
BufferedReader br = new BufferedReader(new OnputStreamReader(System.in));
System.out.println(“Enter index”);
index = br.readLine();
ch = message.charAt(index);
int a = (int) ch;
int last = 0;
while(last!=-1) {
char c = (char) (a+key);
char val = replacecharAt(message, ch,c);
s + = val;
} }
return s;
}
}
public String decrypt(String message) { //message =s (encrypted)
message = message.toLowerCase();
string d;
char ch;
int index;
BufferedReader br = new BufferedReader(new OnputStreamReader(System.in));
System.out.println(“Enter index”);
index = br.readLine();
ch = message.charAt(index);
int a = (int) ch;
int last = 0;
while(last!=-1) {
char c = (char) (a-key);
char val = replacecharAt(message, ch, c);
d + = val;
}
return d;
}
}
import java.util.*;
import java.io.*;
import java.lang.*;
public class View {
public static void main(String args[]) throws Exception {
Cipher c = new Cipher();
private Scanner s;
public View() {
s = new Scanner(System.in);
public String getMessage() {
String msg;
System.out.println(Enter string:”);
msg = s.nextLine();
msg = msg.toLowerCase();
System.out.println(“The string is:” + msg);
return msg;
}
public int getKey() {
try {
int k;
System.out.println(“Enter key:”);
k = s.nextLine();
if( k.isDigit()) {
return k;
} else System.out.println(“enter key again”);
c.Cipher(int);
}
catch (NumberFormatException e) {
e.stackTrace();
}
return 0;
}
c.encrypt(String msg);
c.decrypt(String msg);
public void displayResult(String msg) {
System.out.println(“The enter msg is:” + msg);
System.out.println(“The encrypted message is:” + s);
System.out.println(“The decrypted message is:” +d);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.