In: Computer Science
In Java please
Cipher.java:
/* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java Cipher <filename> <cipher-key> <digit>\n"); System.out.println("Options:\n"); System.out.println("filename\tThe filename of the text file to open"); System.out.println("cipher-key\tThe cipher key for encryption/decryption"); System.out.println("digit\t\t1 to encode or 2 to decode"); System.exit(1); } // Extract input args to variables String inputFilename = args[0]; String key = args[1]; int action = Integer.parseInt(args[2]); if(action != 1 && action != 2) { System.out.println("Option " + action + " is not valid"); System.exit(1); } String outputFilename = getOutputFilename(inputFilename, action); Scanner input = openInput(inputFilename); PrintWriter output = openOutput(outputFilename); // Read in data and output to file // Convert all letters to lowercase for output StringBuilder message = new StringBuilder(input.useDelimiter("\\Z").next()); for(int i = 0; i < message.length(); ++i) { int offset = key.charAt(i % key.length()) - 'a'; if(action == 1) { message.setCharAt(i, shiftUpByK(message.charAt(i), offset)); } else { message.setCharAt(i, shiftDownByK(message.charAt(i), offset)); } } output.println(message.toString().toLowerCase()); // Close streams input.close(); output.close(); } /** * Open input for reading * * @param filename * @return Scanner * @throws FileNotFoundException */ public static Scanner openInput(String filename) throws FileNotFoundException { return new Scanner(new File(filename)); } /** * Open output for writing * * @param filename * @return PrintWriter * @throws FileNotFoundException */ static PrintWriter openOutput(String filename) throws FileNotFoundException { return new PrintWriter(new File(filename)); } /** * Encode letter by some offset d * * @param c input character * @param offset amount to shift character value * @return char encoded character */ public static char shiftUpByK(char c, int offset) { if ('a' <= c && c <= 'z') return (char) ('a' + (c - 'a' + offset) % NUM_LETTERS); if ('A' <= c && c <= 'Z') return (char) ('A' + (c - 'A' + offset) % NUM_LETTERS); return c; // don't encrypt if not an ic character } /** * Decode letter by some offset d * * @param c input character * @param offset amount to shift character value * @return char decoded character */ public static char shiftDownByK(char c, int offset) { if ('a' <= c && c <= 'z') return (char) ('a' + (((c - 'a' - offset) % NUM_LETTERS) + NUM_LETTERS ) % NUM_LETTERS); if ('A' <= c && c <= 'Z') return (char) ('A' + (((c - 'A' - offset) % NUM_LETTERS) + NUM_LETTERS ) % NUM_LETTERS); return c; // don't decrypt if not an ic character } /** * Changes file extension to ".coded" or ".decoded" * * @param filename * @return String new filename or null if action is illegal */ public static String getOutputFilename(String filename, int action) { int indexOfExtension = filename.indexOf("."); String fileNameWithoutExtension; if(indexOfExtension != -1) { fileNameWithoutExtension = filename.substring(0, indexOfExtension); } else { fileNameWithoutExtension = filename; } if(action == 1) { return fileNameWithoutExtension + ".coded"; } else { return fileNameWithoutExtension + ".decoded"; } } public String getInfo() { return "Program 3, Student's name here"; } }
20.23 Program 5b: CipherEx catch exception
Instructor note:
The key is expected to be converted to lowercase before encoding/decoding.
Objectives
Program Description
This program is identical to Cipher.java with these exceptions (no pun intended):
Output
Test case 5 will display an error message:
Error opening file badFilename
Test case 6 will display an error message:
Usage: Cipher inputFileName cipherKey (1)encode (2)decode
The other test cases will be the same as the Cipher class.
I have implemented the updated code to handle the exception per the given description.
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT :
2.OUTPUT :
3.CODE :
/*
* Fix me
*/
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Cipher {
public static final int NUM_LETTERS = 26;
public static final int ENCODE = 1;
public static final int DECODE = 2;
public static void main(String[] args) /* FIX ME */ throws Exception {
// letters
String alphabet = "abcdefghijklmnopqrstuvwxyz";
// Check args length, if error, print usage message and exit
try{
if (args.length != 3) {
throw new Exception();
}
}catch(Exception e){
System.out.println("Usage: Cipher inputFileName cipherKey (1)encode (2)decode");
System.exit(1);
}
// Extract input args to variables
String inputFilename = args[0];
String key = args[1];
int action = Integer.parseInt(args[2]);
try{
if(action != 1 && action != 2) {
throw new Exception();
}}catch(Exception e){
System.out.println("Option " + action + " is not valid");
System.exit(1);
}
String outputFilename = getOutputFilename(inputFilename, action);
Scanner input = openInput(inputFilename);
PrintWriter output = openOutput(outputFilename);
// Read in data and output to file
// Convert all letters to lowercase for output
StringBuilder message = new StringBuilder(input.useDelimiter("\\Z").next());
for(int i = 0; i < message.length(); ++i) {
int offset = key.charAt(i % key.length()) - 'a';
if(action == 1) {
message.setCharAt(i, shiftUpByK(message.charAt(i), offset));
} else {
message.setCharAt(i, shiftDownByK(message.charAt(i), offset));
}
}
output.println(message.toString().toLowerCase());
// Close streams
input.close();
output.close();
}
/**
* Open input for reading
*
* @param filename
* @return Scanner
* @throws FileNotFoundException
*/
public static Scanner openInput(String filename) {
Scanner s=null;
try{
s=new Scanner(new File(filename));
}catch(FileNotFoundException e){
System.out.println("Error opening file "+filename);
System.exit(0);
}return s;
}
/**
* Open output for writing
*
* @param filename
* @return PrintWriter
* @throws FileNotFoundException
*/
static PrintWriter openOutput(String filename) throws FileNotFoundException {
return new PrintWriter(new File(filename));
}
/**
* Encode letter by some offset d
*
* @param c input character
* @param offset amount to shift character value
* @return char encoded character
*/
public static char shiftUpByK(char c, int offset) {
if ('a' <= c && c <= 'z')
return (char) ('a' + (c - 'a' + offset) % NUM_LETTERS);
if ('A' <= c && c <= 'Z')
return (char) ('A' + (c - 'A' + offset) % NUM_LETTERS);
return c; // don't encrypt if not an ic character
}
/**
* Decode letter by some offset d
*
* @param c input character
* @param offset amount to shift character value
* @return char decoded character
*/
public static char shiftDownByK(char c, int offset) {
if ('a' <= c && c <= 'z')
return (char) ('a' + (((c - 'a' - offset) % NUM_LETTERS) + NUM_LETTERS ) % NUM_LETTERS);
if ('A' <= c && c <= 'Z')
return (char) ('A' + (((c - 'A' - offset) % NUM_LETTERS) + NUM_LETTERS ) % NUM_LETTERS);
return c; // don't decrypt if not an ic character
}
/**
* Changes file extension to ".coded" or ".decoded"
*
* @param filename
* @return String new filename or null if action is illegal
*/
public static String getOutputFilename(String filename, int action) {
int indexOfExtension = filename.indexOf(".");
String fileNameWithoutExtension;
if(indexOfExtension != -1) {
fileNameWithoutExtension = filename.substring(0, indexOfExtension);
} else {
fileNameWithoutExtension = filename;
}
if(action == 1) {
return fileNameWithoutExtension + ".coded";
} else {
return fileNameWithoutExtension + ".decoded";
}
}
public String getInfo() {
return "Program 3, Student's name here";
}
}