Question
Need help ASAP in Java package circularlinkedlist; import java.util.Iterator; public class CircularLinkedList implements Iterable { // Your variables Node head; Node tail; int size; //
Need help ASAP in Java
package circularlinkedlist;
import java.util.Iterator;
public class CircularLinkedList
// Your variables
Node
Node
int size; // BE SURE TO KEEP TRACK OF THE SIZE
// implement this constructor
public CircularLinkedList() {
}
// I highly recommend using this helper method
// Return Node
// be sure to handle out of bounds cases
private Node
return null;
}
// attach a node to the end of the list
public boolean add(E item) {
this.add(size,item);
return false;
}
// Cases to handle
// out of bounds
// adding to empty list
// adding to front
// adding to "end"
// adding anywhere else
// REMEMBER TO INCREMENT THE SIZE
public void add(int index, E item){
}
// remove must handle the following cases
// out of bounds
// removing the only thing in the list
// removing the first thing in the list (need to adjust the last
thing in the list to point to the beginning)
// removing the last thing
// removing any other node
// REMEMBER TO DECREMENT THE SIZE
public E remove(int index) {
return null;
}
// Turns your list into a string
// Useful for debugging
public String toString(){
Node
StringBuilder result = new StringBuilder();
if(size == 0){
return "";
}
if(size == 1) {
return head.item.toString();
}
else{
do{
result.append(current.item);
result.append(" ==> ");
current = current.next;
} while(current != head);
}
return result.toString();
}
public Iterator
return new ListIterator
}
// provided code for different assignment
// you should not have to change this
// change at your own risk!
// this class is not static because it needs the class it's inside
of to survive!
private class ListIterator
Node
Node
int index;
@SuppressWarnings("unchecked")
//Creates a new iterator that starts at the head of the
list
public ListIterator(){
nextItem = (Node
index = 0;
}
// returns true if there is a next node
// this is always should return true if the list has
something in it
public boolean hasNext() {
// TODO Auto-generated method stub
return size != 0;
}
// advances the iterator to the next item
// handles wrapping around back to the head automatically
for you
public E next() {
// TODO Auto-generated method stub
prev = nextItem;
nextItem = nextItem.next;
index = (index + 1) % size;
return prev.item;
}
// removed the last node was visted by the .next() call
// for example if we had just created a iterator
// the following calls would remove the item at index 1
(the second person in the ring)
// next() next() remove()
public void remove() {
int target;
if(nextItem == head) {
target = size - 1;
} else{
target = index - 1;
index--;
}
CircularLinkedList.this.remove(target);
//calls the above class
}
}
// It's easiest if you keep it a singly linked list
// SO DON'T CHANGE IT UNLESS YOU WANT TO MAKE IT HARDER
private static class Node
E item;
Node
public Node(E item) {
this.item = item;
}
}
5 Assignment Use CircularLinkedList.java as a starting point to make a new program that reads in a 'deck' of 28 numbers (from a file, from a command line, or an array your choice), asks the user for one or more messages to decrypt, and decrypts them using the modified Solitaire algorithm described above. Note that if your program is decrypting multiple messages, all but the first should be decrypted using the deck as it exists after the decryption of the previous message. (The first uses the deck provided, of course.) 5.1 Why a Circular Linked List? We use Circular Linked List here for a few reasons . Good practice solving and thinking for the upcoming exam. The wrap around nature of the Circular Linked List allows us to not worry about Steps 1 and 2. Linked Lists are ideal for these kinds of manipulations, as we can move entire sections of the list around much, much quicker than an ArrayList. For example, in step 3, we can split our deck into multiple smaller decks and recombine them very quickly. 5.2 Output Your output will be just the decrypted messages lists of characters without spaces or punctuation 5.3 Want to Learn More? .The original Solitaire algorithm is described on this web page: http://www.schneier.com/solitaire.html Answer to the Self Test: After Step 1: 23 26 28 9 12 15 18 21 24 2 1 27 4710 13 16 19 22 25 3 5 8 11 14 17 20 6 After Step 2 23 26 9 12 28 15 18 21 24 2 1 27 4710 13 16 19 22 25 3 5 8 11 14 17 20 6 After Step 3: 4 7 10 13 16 19 22 25 3 5 8 11 14 17 20 6 28 15 18 21 24 2 1 27 23 26 9 12 After Step 4: 14 17 20 6 28 15 18 21 24 2 1 27 23 26 947 10 13 16 19 22 25 3 5 8 11 12 After Step li The deck is the same as it was after step 4. The 15th card, the next keystream value, is9 4.1 Encryption To encode a message with Solitaire, remove all non-letters and convert any lower-case letters to upper-case. (If you wanted to be in line with traditional cryptographic practice, you'd also divide the letters into groups of five.) Convert the letters to numbers (A-1, B-2, etc.). Use Solitaire to generate the same number of values as are in the message. Add the corresponding pairs of numbers, modulo 26. Convert the numbers back to letters, and you're done Decryption is just the reverse of encryption. Start by converting the message to be decoded to numbers. Using the same card ordering as was used to encrypt the message originally, generate enough keystream values. (Because the same starting deck of cards was used, the same keystream will be generated.) Subtract the keystream values from the message numbers, again modulo 26. Finally, convert the numbers to letters and read the message Let's give it a try. The message to be sent is this Removing the non-letters and capitalizing gives us: The message has 16 letters, and 16 is not a multiple of 5.2 So, we'll pad out Dr. McCann is insane DRMCCANNISINSANE the message with X's. Next, convert the letters to numbers: DRMC C A N N I S I N S AN E X X X X 4 18 13 3 3 1 14 14 9 19 9 14 19 1 14 5 24 24 24 24 Rather than actually generating a sequence of 20 keystream values for this example, let's just pretend that we diod 21 6 2 19 15 18 12 23 23 5 1 7 14 6 13 1 26 16 12 20 Just add the two groups together pairwise. To get the modulo 26: If the sum of a pair is greater than 26, just subtract 26 from t. For example, 14 + 12 - 26, but 14 23-37 - 26 11. (Note that this isn't quite the result that the operator % would give you in Java. 4 18 13 3 3 1 14 14 9 19 9 14 19 1 14 5 24 24 24 24 + 21 6 2 19 15 18 12 23 23 5 1 7 14 6 13 1 26 16 12 20 25 24 15 22 18 19 26 11 6 24 10 21 7716 24 14 10 18 5 Assignment Use CircularLinkedList.java as a starting point to make a new program that reads in a 'deck' of 28 numbers (from a file, from a command line, or an array your choice), asks the user for one or more messages to decrypt, and decrypts them using the modified Solitaire algorithm described above. Note that if your program is decrypting multiple messages, all but the first should be decrypted using the deck as it exists after the decryption of the previous message. (The first uses the deck provided, of course.) 5.1 Why a Circular Linked List? We use Circular Linked List here for a few reasons . Good practice solving and thinking for the upcoming exam. The wrap around nature of the Circular Linked List allows us to not worry about Steps 1 and 2. Linked Lists are ideal for these kinds of manipulations, as we can move entire sections of the list around much, much quicker than an ArrayList. For example, in step 3, we can split our deck into multiple smaller decks and recombine them very quickly. 5.2 Output Your output will be just the decrypted messages lists of characters without spaces or punctuation 5.3 Want to Learn More? .The original Solitaire algorithm is described on this web page: http://www.schneier.com/solitaire.html Answer to the Self Test: After Step 1: 23 26 28 9 12 15 18 21 24 2 1 27 4710 13 16 19 22 25 3 5 8 11 14 17 20 6 After Step 2 23 26 9 12 28 15 18 21 24 2 1 27 4710 13 16 19 22 25 3 5 8 11 14 17 20 6 After Step 3: 4 7 10 13 16 19 22 25 3 5 8 11 14 17 20 6 28 15 18 21 24 2 1 27 23 26 9 12 After Step 4: 14 17 20 6 28 15 18 21 24 2 1 27 23 26 947 10 13 16 19 22 25 3 5 8 11 12 After Step li The deck is the same as it was after step 4. The 15th card, the next keystream value, is9 4.1 Encryption To encode a message with Solitaire, remove all non-letters and convert any lower-case letters to upper-case. (If you wanted to be in line with traditional cryptographic practice, you'd also divide the letters into groups of five.) Convert the letters to numbers (A-1, B-2, etc.). Use Solitaire to generate the same number of values as are in the message. Add the corresponding pairs of numbers, modulo 26. Convert the numbers back to letters, and you're done Decryption is just the reverse of encryption. Start by converting the message to be decoded to numbers. Using the same card ordering as was used to encrypt the message originally, generate enough keystream values. (Because the same starting deck of cards was used, the same keystream will be generated.) Subtract the keystream values from the message numbers, again modulo 26. Finally, convert the numbers to letters and read the message Let's give it a try. The message to be sent is this Removing the non-letters and capitalizing gives us: The message has 16 letters, and 16 is not a multiple of 5.2 So, we'll pad out Dr. McCann is insane DRMCCANNISINSANE the message with X's. Next, convert the letters to numbers: DRMC C A N N I S I N S AN E X X X X 4 18 13 3 3 1 14 14 9 19 9 14 19 1 14 5 24 24 24 24 Rather than actually generating a sequence of 20 keystream values for this example, let's just pretend that we diod 21 6 2 19 15 18 12 23 23 5 1 7 14 6 13 1 26 16 12 20 Just add the two groups together pairwise. To get the modulo 26: If the sum of a pair is greater than 26, just subtract 26 from t. For example, 14 + 12 - 26, but 14 23-37 - 26 11. (Note that this isn't quite the result that the operator % would give you in Java. 4 18 13 3 3 1 14 14 9 19 9 14 19 1 14 5 24 24 24 24 + 21 6 2 19 15 18 12 23 23 5 1 7 14 6 13 1 26 16 12 20 25 24 15 22 18 19 26 11 6 24 10 21 7716 24 14 10 18
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started