implementation of the Caesar cipher (Section 5.3 in the textbook), which we discussed in class. Caesar cipher is a simple approach to encoding messages by shifting each letter in a message along the alphabet by a constant amount.
This is the implementation of the Caesar cipher (Section
5.3 in the textbook), which we discussed in class. Caesar cipher is a simple approach to
encoding messages by shifting each letter in a message along the alphabet by a
constant amount.
Your code will be similar to Listing 5.1, but you must use CircularArrayQueue.java
instead of the Java API queue.
//5.1
import java.util.*;
public class Codes {
public static void main(String[] args) {
int[] key = {5, 12, -3, 8, -9, 4, 10};
Integer keyValue;
String encoded = "", decoded = "";
String message = "All programmer are playwrights and all "+"computers are lausy actors.";
Queue<Integer> encodingQueue = new LinkedList<Integer>();
Queue<Integer> decodingQueue = new LinkedList<Integer>();
for (int scan=0; scan < key.length; scan++) {
encodingQueue.add(key[scan]);
decodingQueue.add(key[scan]);
}
for (int scan=0; scan < message.length(); scan++) {
keyValue = encodingQueue.remove();
encoded += (char)(message.charAt(scan)+keyValue);
encodingQueue.add(keyValue);
}
System.out.println("Encoded Message: \n"+encoded+"\n");
for(int scan=0; scan < encoded.length(); scan++) {
keyValue = decodingQueue.remove();
decoded += (char)(encoded.charAt(scan)-keyValue);
decodingQueue.add(keyValue);
}
System.out.println("Decoded Message:\n"+decoded);
}
}
//CircularArrayQueue
package jsjf;
package jsjf.exceptions.*;
public class CircularArrayQueue<T> implements QueueADT<T>{
private final static int DEFAULT_CAPACITY = 100;
private int front, rear, count;
private T[] queue;
public CircularArrayQueue(int initialCapacity) {
front=rear=count=0;
queue=(T[])(new Object[initialCapacity]);
}
public CircularArrayQueue() {
this(DEFAULT_CAPACITY);
}
}
//QueueADT
package jsjf;
public interface QueueADT<T> {
public void enqueue(T element);
public T dequeue();
public T first();
public boolean isEmpty();
public int size();
public String toString;
}
Step by step
Solved in 2 steps with 1 images