This is the implementation of the Caesar cipher (Section5.3 in the textbook), which we discussed in class. Caesar cipher is a simple approach toencoding messages by shifting each letter in a message along the alphabet by aconstant amount.Your code will be similar to Listing 5.1, but you must use CircularArrayQueue.javainstead 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 encodingQueue = new LinkedList(); Queue decodingQueue = new LinkedList(); 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); }}//CircularArrayQueuepackage jsjf;package jsjf.exceptions.*;public class CircularArrayQueue implements QueueADT{ 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); }}//QueueADTpackage jsjf;public interface QueueADT { public void enqueue(T element); public T dequeue(); public T first(); public boolean isEmpty(); public int size(); public String toString;}
//CircularArrayQueue
package jsjf;package jsjf.exceptions.*;public class CircularArrayQueue implements QueueADT{ 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); }}//QueueADTpackage jsjf;public interface QueueADT { public void enqueue(T element); public T dequeue(); public T first(); public boolean isEmpty(); public int size(); public String toString;}
//QueueADT
package jsjf;public interface QueueADT { public void enqueue(T element); public T dequeue(); public T first(); public boolean isEmpty(); public int size(); public String toString;}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here