Question: In a class called RotateEncryption, implement(override) the method getEncryptedLine. This method will shift over all letters by a key. Implement the constructor that accepts a

In a class called RotateEncryption, implement(override) the method getEncryptedLine. This method will shift over all letters by a key. Implement the constructor that accepts a key from the user. *Note: please use StringBuilder!*

//Given:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Encryption { protected String filename; protected int key; protected Scanner inputFile; public Encryption() { filename = "not set"; key = -1; } public Encryption(String filename) throws FileNotFoundException { this.filename = filename; this.key = 0; this.inputFile = new Scanner(new File(this.filename)); } public Encryption(String filename, int key) throws FileNotFoundException { this.filename = filename; this.key = key; this.inputFile = new Scanner(new File(this.filename)); } public String getLine(int which) { return "getLine not properly implemented"; } public String getEncryptedLine(int which) { return "getEncryptedLine not properly implemented"; } } 

import java.io.FileNotFoundException; import java.util.Scanner; public class EncryptDemo3 { public static void main(String[] args) throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a filename"); String name = keyboard.nextLine(); System.out.println("Enter a line number"); int which = keyboard.nextInt(); System.out.println("Enter key"); int key = keyboard.nextInt(); Encryption test = new RotateEncryption(name, key); String encrypted = test.getEncryptedLine(which); System.out.print("Cipher text: " + encrypted + " "); } } 

//Current Code (for a set key of 13):

import java.io.FileNotFoundException; public class RotateEncryption extends Encryption { public RotateEncryption(String filename) throws FileNotFoundException { super(filename); } public String getEncryptedLine(int which) { String line = getLine(which); String encrypted = ""; for (int index = 0; index < line.length(); index++) { char ch = line.charAt(index); int ascii = (int)ch; if(ch >= 'a' && ch <= 'z') { int newAscii = ((ascii - 'a' + 13) % 26) + 'a'; encrypted = encrypted + (char)newAscii; } else if(ch >= 'A' && ch <= 'Z') { int newAscii = ((ascii -'A' + 13) % 26) + 'A' ; encrypted = encrypted + (char)newAscii; } else { encrypted = encrypted + ch; } } return encrypted; } public String getLine(int which) { int index = -1; String line = "not set"; while (index < which && inputFile.hasNextLine()) { line = inputFile.nextLine(); index++; } if(index == which) { return line; } return "File is too short!"; } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!