Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following should be called in the main method at the end. The program should shuffle the cards, then do the following steps shown below.

The following should be called in the main method at the end. The program should shuffle the cards, then do the following steps shown below.

shuffle() #step 1 swapAJoker(); //value 27 #step 2 moveBJoker(); //value 28 #step 3 tripleCutDeck(); #step 4 doStep4(); #step 5 key= doStep5(); return key;

I have posted the code to do other parts below

image text in transcribed

import java.util.Iterator; import java.util.*;

public class CircularLinkedList implements Iterable {

// Your variables Node head; Node tail; int size; // BE SURE TO KEEP TRACK OF THE SIZE

// implement this constructor public CircularLinkedList() { this.head = null; this.tail = null; this.size = 0; }

public int size() { return size; } // I highly recommend using this helper method // Return Node found at the specified index // be sure to handle out of bounds cases

private Node getNode(int index){ Node current = head; for (int i = 0; i

// attach a node to the end of the list public boolean add(E item) { this.add(size, item); return true;

}

// 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) { if(index size){ throw new IndexOutOfBoundsException("Wrong index"); }

Node temp = new Node(item); //Scenario: Adding the very first item if(size == 0) { this.head = temp; this.tail = temp; } //Scenario: Adding a new head else if(index == 0){ temp.next = head; head.prev = temp; head = temp; head.prev=tail; tail.next=head; } //Scenario: Adding a new tail else if(index == size) { temp.prev = tail; tail.next = temp; tail = temp; tail.next=head; head.prev=tail; } //Scenario: Any other case else{ Node before = this.getNode(index - 1); temp.next = before.next; temp.prev = before; before.next = temp; temp.next.prev = temp; }

size++; }

// 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) { E removed = null; if(index = size) { throw new IndexOutOfBoundsException(""); }

if (size == 1) { removed = head.item; head = null; tail = null; } else if( index == 0) { removed = head.item; head = head.next; head.prev = tail; tail.next=head; } else if(index == size -1) { removed = tail.item; tail = tail.prev; tail.next = head;

} else { Node before = getNode(index -1); removed = before.next.item; before.next = before.next.next; before.next.prev = before; }

size--; return removed; }

// Turns your list into a string // Useful for debugging public String toString() { Node current = head; 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 iterator() { return new ListIterator(); }

// provided code // read the comments to figure out how this works and see how to use it // 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 implements Iterator {

Node nextItem; Node prev; int index;

@SuppressWarnings("unchecked") //Creates a new iterator that starts at the head of the list public ListIterator() { nextItem = (Node) head; 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 }

}

private static class Node {

E item; Node next; private Node prev;

public Node(E item) { this.item = item; }

}

//public int size(); //public E get(int index); //public boolean add(E item); //public void add(int index, E item); //public E remove(int index); //public String toString();

public static void main(String[] args) { // Your assignment doesn't say which 28 numbers it needs to be. // So I guess just number them 1..28? int[] numbers = new int[] {23,26,28,9,12,15,18,21,24,2,1,27,4,7,10,13,16,19,22,25,3,5,8,11,14,17,20,6}; // We need to create an actual linked list object and store it into a variable. CircularLinkedList myLinkedList = new CircularLinkedList(); // Then, I think, use a for loop over the numbers array, and add each element // individually to myLinkedList for (int i=0 ; i { // Now we just add the ith number. myLinkedList.add(numbers[i]); } // At this point, myLinkedList should be populated with the numbers. // Create an int variable named 'jokerIndex' for (int i=0 ; i int value; value = myLinkedList.get(i); if(value == 27){ // set jokerIndex to the value in i // and break out of the for loop. } } System.out.println(myLinkedList); // myLinkedList.remove(jokerIndex); After the for loop, you'll need these lines to // myLinkedList.add(jokerIndex + 1, 27); reinsert the joker, effectively swapping it as we discussed. } }

3 Example As usua an example will really help make sense of the algorithm. Let's say that this is the original ordering of our half deck of cards 1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 27 2 5 8 11 14 17 20 23 26 Step 1 Swap 27 with the value following it. So, we swap 27 and 2 1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26 Step 2 Move 28 two places down the list. It ends up between 6 and 9: 1 4 7 10 13 16 19 22 25 3 6 28 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26 Step 3 Do the triple cut. Everything above the first joker(28 in this case) goes to the bottom of the deck, and everything below the second (27) goes to the top 5 8 11 14 17 20 23 26 28 9 12 15 18 21 24 2 27 1 4 7 10 13 16 19 22 25 3 6 Step 4 The bottom card is 6. The first 6 cards of the deck are 5, 8, 11, 14, 17, and 20 They go just ahead of 6 at the bottom end of the deck: 23 26 28 9 12 15 18 21 24 2 27 1 4710 13 16 19 22 25 3 5 8 11 14 17 20 6 Step 5 The top card is 23. Thus, our generated keystream value is the 24thcard, whiclh is 11 3 Example As usua an example will really help make sense of the algorithm. Let's say that this is the original ordering of our half deck of cards 1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 27 2 5 8 11 14 17 20 23 26 Step 1 Swap 27 with the value following it. So, we swap 27 and 2 1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26 Step 2 Move 28 two places down the list. It ends up between 6 and 9: 1 4 7 10 13 16 19 22 25 3 6 28 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26 Step 3 Do the triple cut. Everything above the first joker(28 in this case) goes to the bottom of the deck, and everything below the second (27) goes to the top 5 8 11 14 17 20 23 26 28 9 12 15 18 21 24 2 27 1 4 7 10 13 16 19 22 25 3 6 Step 4 The bottom card is 6. The first 6 cards of the deck are 5, 8, 11, 14, 17, and 20 They go just ahead of 6 at the bottom end of the deck: 23 26 28 9 12 15 18 21 24 2 27 1 4710 13 16 19 22 25 3 5 8 11 14 17 20 6 Step 5 The top card is 23. Thus, our generated keystream value is the 24thcard, whiclh is 11

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

Step: 3

blur-text-image

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions