Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Caesar cipher in Java ------ CicularArrayQueue.java (I have implemented all the methods execpt the constructor) package Lab08; import sun.invoke.empty.Empty; /** * @version 10/16/2018 */ public

Caesar cipher in Java

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

------

CicularArrayQueue.java (I have implemented all the methods execpt the constructor)

package Lab08; import sun.invoke.empty.Empty; /**  * @version 10/16/2018  */ public class CircularArrayQueue implements QueueInterface { private T[] queue; // Circular array of queue entries and one unused location private int frontIndex; // Index of front entry private int backIndex; // Index of back entry private boolean initialized = false; private static final int DEFAULT_CAPACITY = 3; private static final int MAX_CAPACITY = 10000; public CircularArrayQueue() { this(DEFAULT_CAPACITY); } // end default constructor public CircularArrayQueue(int initialCapacity) { checkCapacity(initialCapacity); // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempQueue = (T[]) new Object[initialCapacity + 1]; this.queue = tempQueue; this.frontIndex = 0; this.backIndex = initialCapacity; this.initialized = true; } // end constructor public CircularArrayQueue(T[] initialContent) { // TODO Project 2A   } // end constructor public void enqueue(T newEntry) { System.out.println("enqueue(" + newEntry + ")"); // ***TESTING checkInitialization(); ensureCapacity(); backIndex = (backIndex + 1) % queue.length; queue[backIndex] = newEntry; System.out.println("queue[" + backIndex + "] = " + newEntry); // ***TESTING } // end enqueue public T getFront() throws EmptyQueueException { checkInitialization(); if (isEmpty()) throw new EmptyQueueException(); else return queue[frontIndex]; } // end getFront public T dequeue() throws EmptyQueueException { if (isEmpty()) throw new EmptyQueueException(); else { T front = queue[frontIndex]; queue[frontIndex] = null; frontIndex = (frontIndex + 1) % queue.length; return front; } } public boolean isEmpty() { return frontIndex == ((backIndex + 1) % queue.length); //return true; // THIS IS A STUB } // end isEmpty public void clear() { // null out only the used slots queue[frontIndex] = null; queue[backIndex] = null; } // end clear // Throws an exception if this object is not initialized. private void checkInitialization() { if (!this.initialized) throw new SecurityException("CircularArrayQueue object is not initialized properly."); } // end checkInitialization // Throws an exception if the client requests a capacity that is too large. private void checkCapacity(int capacity) { if (capacity > MAX_CAPACITY) throw new IllegalStateException("Attempt to create a queue " + "whose capacity exceeds " + "allowed maximum."); } // end checkCapacity // Doubles the size of the array queue if it is full. // Precondition: checkInitialization has been called. private void ensureCapacity() { if (frontIndex == ((backIndex + 2) % queue.length)) { T[] oldQueue = queue; int oldSize = oldQueue.length; int newSize = 2 * oldSize; checkCapacity(newSize - 1); @SuppressWarnings("unchecked") T[] tempQueue = (T[]) new Object[newSize]; queue = tempQueue; for (int index = 0; index  

------------------

Cipher.java

package Lab08; /**  * A class that implements a cipher with repeating key algorithm.  *  * @author YOUR NAME  * @version 10/16/2018  */  public class Cipher { private Integer[] key; private final int ADD_FACTOR = 1; private final int SUBTRACT_FACTOR = -1; public Cipher(Integer... key) { // TODO Project 2B   } public String encode(String message) { // TODO Project 2B  //calls code method with ADD_FACTOR return "???"; } public String decode(String encoded) { // TODO Project 2B  //calls code method with SUBTRACT_FACTOR return "???"; } private String code(String message, int addOrSubtractFactor) { StringBuilder encoded = new StringBuilder(); // TODO Project 2B  System.out.println(getKeyQueue()); // calls getKeyQueue to get the queue for encoding and decoding return encoded.toString(); } private QueueInterface getKeyQueue() { // TODO Project 2B   return null; // THIS IS A STUB } public static void main(String args[]) { System.out.println("************** TESTING THE CIPHER ************** "); Cipher cipher = new Cipher(5, 12, -3, 8, -9, 4, 10, 2, 3, 5, 1); String encoded = cipher.encode("All programmers are playwrights and all computers are lousy actors."); System.out.println("--->The original message encoded is:"); System.out.println(encoded); String decoded = cipher.decode(encoded); System.out.println("--->The original message decoded is:"); System.out.println(decoded); encoded = cipher.encode("There is no elevator to success, You have to take the stairs..."); System.out.println(" --->The original message encoded is:"); System.out.println(encoded); decoded = cipher.decode(encoded); System.out.println("--->The original message decoded is:"); System.out.println(decoded); cipher = new Cipher(3, 1, 7, 4, 2, 5); encoded = cipher.encode("Trust but Verify"); System.out.println(" --->The original message encoded is:"); System.out.println(encoded); decoded = cipher.decode(encoded); System.out.println("--->The original message decoded is:"); System.out.println(decoded); encoded = cipher.encode("race car"); System.out.println(" --->The original message encoded is:"); System.out.println(encoded); decoded = cipher.decode(encoded); System.out.println("--->The original message decoded is:"); System.out.println(decoded); } } ----------------------

QueueInterface.java

package Lab08; /**  An interface for the ADT queue.  @author Frank M. Carrano  @author Timothy M. Henry  @version 4.0 */ public interface QueueInterface { /** Adds a new entry to the back of this queue.  @param newEntry An object to be added. */  public void enqueue(T newEntry); /** Removes and returns the entry at the front of this queue.  @return The object at the front of the queue.  @throws EmptyQueueException if the queue is empty before the operation. */  public T dequeue(); /** Retrieves the entry at the front of this queue.  @return The object at the front of the queue.  @throws EmptyQueueException if the queue is empty. */  public T getFront(); /** Detects whether this queue is empty.  @return True if the queue is empty, or false otherwise. */  public boolean isEmpty(); /** Removes all entries from this queue. */  public void clear(); } // end QueueInterface 
Application #2 A Caesar cipher is a simple approach to encoding messages by shifting each letter in a message along the alphabet by a constant amount of k. For example, if k equals 3, then in an encoded message, each letter is shifted three characters forward: a is replaced with d, b with e, c with fand so on. The end of the alphabet wraps back around to the beginning. To decode the message each letter is shifted the same number of characters backwards. Julius Caesar actually used this type of cipher in some of his secret government correspondence. Unfortunately, the Caesar cipher is fairly easy to break. There are only 26 possibilities for shifting the characters, and the code can be broken by trying various key values until it works An improvement can be made to this encoding technique if we use a so called repeating key. Instead of shifting each character by a constant amount, we can shift each character by a different amount using a list of key values. If the message is longer that the list of key values, we just start using the key over again from the beginning. For example, if the key values are 3 1 7 4 2 5 then the first character is shifted by three, the second character by one, the third character by seven, etc After shifting the sixth character by five, we start using the key over again. The seventh character is shifted by three, the eighth by one, etc. The following table shows the message "knowledge is power" encoded using this repeating key: 3 1 74 2 5 31 74 25 31 7 4 2 5 0 Notice that this encryption approach encodes the same letter into different characters, depending on where it occurs in the message and thus which key value is used to encode it. A. Using the QueueInterface defined in the textbook, implement circularArraygueue class that uses a "circular" array with one unused location

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions

Question

How does a company use product costing?

Answered: 1 week ago