outprintln1 shuffle shuffle1encodetest Systemoutprintln2 shuffle

Outprintln1 shuffle shuffle1encodetest

This preview shows page 37 - 40 out of 42 pages.

System.out.println("1 shuffle: " + shuffle1.encode(test)); System.out.println("2 shuffle: " + shuffle2.encode(test)); System.out.println("3 shuffle: " + shuffle3.encode(test)); System.out.println("4 shuffle: " + shuffle4.encode(test)); test = "abcd12345"; System.out.println("Shuffling " + test); System.out.println("1 shuffle: " + shuffle1.encode(test)); System.out.println("2 shuffle: " + shuffle2.encode(test)); System.out.println("3 shuffle: " + shuffle3.encode(test)); System.out.println("4 shuffle: " + shuffle4.encode(test)); test = "abcde12345"; System.out.println("Shuffling " + test); System.out.println("1 shuffle: " + shuffle1.encode(test)); System.out.println("2 shuffle: " + shuffle2.encode(test)); System.out.println("3 shuffle: " + shuffle3.encode(test)); System.out.println("4 shuffle: " + shuffle4.encode(test)); System.out.println("5 shuffle: " + shuffle5.encode(test)); System.out.println("6 shuffle: " + shuffle6.encode(test)); System.out.println("\nTesting decode on " + test); String shuffled = shuffle1.encode(test);
Image of page 37
38 System.out.println("encoded message: " + shuffled); System.out.println("decoded message: " + shuffle1.decode(shuffled)); shuffled = shuffle2.encode(test); System.out.println("\nencoded message: " + shuffled); System.out.println("decoded message: " + shuffle2.decode(shuffled)); shuffled = shuffle3.encode(test); System.out.println("\nencoded message: " + shuffled); System.out.println("decoded message: " + shuffle3.decode(shuffled)); shuffled = shuffle4.encode(test); System.out.println("\nencoded message: " + shuffled); System.out.println("decoded message: " + shuffle4.decode(shuffled)); } } File name: SubstitutionCipher.java import java.lang.Character; public class SubstitutionCipher implements MessageEncoder, MessageDecoder { private int shift; // Creates a new instance of SubstitutionCipher public SubstitutionCipher(int n) { shift = n; } public String encode(String plainText){ String cipherText = ""; for(int i=0; i<plainText.length(); i++){ Character c = plainText.charAt(i); cipherText = cipherText + codeCharacter(c); } return cipherText; } private Character codeCharacter(Character c){ return (char)(c + shift); } // This is for the project public String decode(String cipherText){ String plainText = ""; for(int i=0; i<cipherText.length(); i++){ Character c = cipherText.charAt(i); plainText = plainText + decodeCharacter(c); } return plainText; } private Character decodeCharacter(Character c){ return (char)(c - shift); } public static void main(String[] args) { SubstitutionCipher shiftBy3 = new SubstitutionCipher(3); System.out.println("Demonstrating cipher.");
Image of page 38
CSJA1DP/203/0/2016 39 String plain = "A message to encode"; System.out.println("Plain text: " + plain); String cipher = shiftBy3.encode(plain); System.out.println("Cipher text: " + cipher); SubstitutionCipher shiftByMinus3 = new SubstitutionCipher(-3); System.out.println("This should decode the cipher."); String decoded = shiftByMinus3.encode(cipher); System.out.println("Decoded text: " + decoded); System.out.println("Testing the decode method."); decoded = shiftBy3.decode(cipher); System.out.println("Decoded text: " + decoded); } } File name: CodeProgram.java import java.util.*; public class CodeProgram { public static void main(String[] args) { System.out.println("Welcome to the Cipher program"); Scanner reader = new Scanner(System.in); System.out.println("What is the key (shift amount) for your code?"); int shift = reader.nextInt();
Image of page 39
Image of page 40

You've reached the end of your free preview.

Want to read all 42 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture