can somebody help me with my java project? it deals with rot13 this is what it l
ID: 3698119 • Letter: C
Question
can somebody help me with my java project? it deals with rot13
this is what it looks like
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
*
*
* Encryption is a way of taking information and encoding it. Hopefully you also know how to decode it, too!
*
* One example is ROT13, which is a simple encryption that barely even counts as encryption.
* The idea is that you take a letter and replace it with the letter that is 13 places after it. For example,
* the letter 'H' would become 'U'. In this way, you really can change the alphabet to put U and I
* next to each other.
* As a larger example, performing a ROT13 on the phrase "Hello world!" results in "Uryyb jbeyq!"
*
* [ ] Implement (or write the code for) a method called "rot13"
* that accepts a String as a parameter, and returns a String.
* [ ] rot13() will use a for loop to be able to read each character, and perform ROT13 on it.
* After you've changed (or didn't change!) a character, append it to the returned String.
* [ ] Call rot13Helper() to make the job easier! Your next line is going to be "What's rot13Helper()?"
*
* [ ] Implement a method called "rot13Helper" that
* accepts a character as a parameter, and returns a character.
* This will do the ROT13 work that takes a character,
* casts it into an integer, and adds or subtracts 13 to its ASCII value.
* Returns the changed (or not changed!) character.
*
* [ ] Write a method for encryption that both reads and writes to a file.
* It accepts a Scanner object and PrintWriter object as parameters, and does not return anything.
* It reads the Scanner object line by line.
* After it reads a line, it encrypts a line by performing ROT13 on that line
* by passing the line (as a parameter) to rot13(),
* and then prints the returned result from rot13() to the PrintWriter object.
*
* [ ] Write a method for decryption that both reads and writes to a file.
* It accepts a Scanner object and PrintWriter object as parameters, and does not return anything.
* It reads the Scanner object line by line.
* After it reads a line, it decrypts the line. How do we reverse ROT13?
*
* [ ] Make a way so that a user can tell the program that he/she wants to either encrypt or decrypt.
* You may use any way we have learned to receive user input.
*
* [ ] If the user chooses to encrypt, you must ask them for the file to encrypt, and also the file to write the encryption to.
*
* [ ] If the user chooses to decrypt, you must ask them for the file to decrypt, and also the file to write the decryption to.
*
* [ ] Write your own encryption algorithm. Make sure it's reversible!
* Adjust your program so that a user can choose between using your algorithm and ROT13.
*/
public class Encryption {
}
Explanation / Answer
public class Encryption {
public static String rot13(String yeh){
String lolwat = “”;
for(int index = 0; index <yeh.length(); index ++)
{
lolwat +=rot13helper(yeh.charAt(index));
}
return lolwat;
}
public static char rot13helper(char hah){
int i = (char)hah;
if (i >= 65 && i <= 77)
{
i +=13;
}
else if (i >= 78 && i <= 90)
{
i-=13;
}
else if (i >= 97 && i <= 109)
{
i+=13;
}
else if (i >= 110 && i <= 122)
{
i-=13;
}
return (char)i;
}
public static void encryption(String file1, String file2)
{
try{
Scanner readFile = new Scanner(new File(file1));
PrintWriter writeFile = new PrintWriter(new File(file2));
while (readFile.hasNext())
{
String write = readFile.nextLine();
write = rot13(write);
writeFile.println(write);
}
readFile.close();
writeFile.close();
} catch (Exception e) {
}
}
public static void decryption(String file1, String file2)
{
try{
Scanner readFile = new Scanner(new File(file1));
PrintWriter writeFile = new PrintWriter(new File(file2));
while (readFile.hasNext())
{
String write = readFile.nextLine();
write = rot13(write);
writeFile.println(write);
}
readFile.close();
writeFile.close();
} catch (Exception e) {
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String nextLine = “”;
do {
System.out.println(“Do you want to decrypt or encrypt? Or STOP?”);
nextLine = input.nextLine();
if (nextLine.equals(“encrypt”))
{
System.out.println(“Which file do you want to encrypt?”);
String file1 = input.nextLine();
String file2 = input.nextLine();
encryption(file1,file2);
System.out.println(“END encrypt”);
}
else if (nextLine.equals(“decrypt”))
{
System.out.println(“Which file do you want to decrypt to?”);
String file2 = input.nextLine();
String file1 = input.nextLine();
decryption(file2,file1);
System.out.println(“END decrypt”);
}
} while (!nextLine.equals(“STOP”));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.