1. Engineering
  2. Computer Science
  3. implement the method getencryptedline this method is the same as...

Question: implement the method getencryptedline this method is the same as...

Question details

Implement the method getEncryptedLine. This method is the same as getLine but it will shift over all letters by 13. For example, A would become N. Then, Implement the constructor that accepts the key so the user doesn't always have to rotate by 13. Please make it where it makes lower case letters into encrypted lowercase and upper case letters into encrypted upper case letters.

// Encryption

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(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";
    }
}

// Encryption Demo

import java.io.FileNotFoundException;
import java.util.Scanner;

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 + "\n");
    }
}

// Code so far

import java.io.FileNotFoundException;

public class RotateEncryption extends Encryption
{
  public RotateEncryption(String filename) throws FileNotFoundException
  {
    super(filename);
  }

  public RotateEncryption(String filename, int key) throws FileNotFoundException {

    super(filename, key);
  }

  @Override

  public String getLine(int which)
  {
    int noline = -1;
    String line = null;

    while (noline < which && inputFile.hasNextLine())
    {
      line = inputFile.nextLine();

      noline++;

    }
    if (noline == which)
      return line;
    return "File is too short!";
  }
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution