Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Any help would be appreciated :) Starter code below: package assignment2; import java.util.Random; public class Deck { public static String[] suitsInOrder = {clubs, diamonds, hearts,
Any help would be appreciated :)
Starter code below:
package assignment2; import java.util.Random; public class Deck { public static String[] suitsInOrder = {"clubs", "diamonds", "hearts", "spades"}; public static Random gen = new Random(); public int numOfCards; // contains the total number of cards in the deck public Card head; // contains a pointer to the card on the top of the deck /* * TODO: Initializes a Deck object using the inputs provided */ public Deck(int numOfCardsPerSuit, int numOfSuits) { /**** ADD CODE HERE ****/ } /* * TODO: Implements a copy constructor for Deck using Card.getCopy(). * This method runs in O(n), where n is the number of cards in d. */ public Deck(Deck d) { /**** ADD CODE HERE ****/ } /* * For testing purposes we need a default constructor. */ public Deck() {} /* * TODO: Adds the specified card at the bottom of the deck. This * method runs in $O(1)$. */ public void addCard(Card c) { /**** ADD CODE HERE ****/ } /* * TODO: Shuffles the deck using the algorithm described in the pdf. * This method runs in O(n) and uses O(n) space, where n is the total * number of cards in the deck. */ public void shuffle() { /**** ADD CODE HERE ****/ } /* * TODO: Returns a reference to the joker with the specified color in * the deck. This method runs in O(n), where n is the total number of * cards in the deck. */ public Joker locateJoker(String color) { /**** ADD CODE HERE ****/ return null; } /* * TODO: Moved the specified Card, p positions down the deck. You can * assume that the input Card does belong to the deck (hence the deck is * not empty). This method runs in O(p). */ public void moveCard(Card c, int p) { /**** ADD CODE HERE ****/ } /* * TODO: Performs a triple cut on the deck using the two input cards. You * can assume that the input cards belong to the deck and the first one is * nearest to the top of the deck. This method runs in O(1) */ public void tripleCut(Card firstCard, Card secondCard) { /**** ADD CODE HERE ****/ } /* * TODO: Performs a count cut on the deck. Note that if the value of the * bottom card is equal to a multiple of the number of cards in the deck, * then the method should not do anything. This method runs in O(n). */ public void countCut() { /**** ADD CODE HERE ****/ } /* * TODO: Returns the card that can be found by looking at the value of the * card on the top of the deck, and counting down that many cards. If the * card found is a Joker, then the method returns null, otherwise it returns * the Card found. This method runs in O(n). */ public Card lookUpCard() { /**** ADD CODE HERE ****/ return null; } /* * TODO: Uses the Solitaire algorithm to generate one value for the keystream * using this deck. This method runs in O(n). */ public int generateNextKeystreamValue() { /**** ADD CODE HERE ****/ return 0; } public abstract class Card { public Card next; public Card prev; public abstract Card getCopy(); public abstract int getValue(); } public class PlayingCard extends Card { public String suit; public int rank; public PlayingCard(String s, int r) { this.suit = s.toLowerCase(); this.rank = r; } public String toString() { String info = ""; if (this.rank == 1) { //info += "Ace"; info += "A"; } else if (this.rank > 10) { String[] cards = {"Jack", "Queen", "King"}; //info += cards[this.rank - 11]; info += cards[this.rank - 11].charAt(0); } else { info += this.rank; } //info += " of " + this.suit; info = (info + this.suit.charAt(0)).toUpperCase(); return info; } public PlayingCard getCopy() { return new PlayingCard(this.suit, this.rank); } public int getValue() { int i; for (i = 0; ipackage assignment2; public class SolitaireCipher { public Deck key; public SolitaireCipher (Deck key) { this.key = new Deck(key); // deep copy of the deck } /* * TODO: Generates a keystream of the given size */ public int[] getKeystream(int size) { /**** ADD CODE HERE ****/ return null; } /* * TODO: Encodes the input message using the algorithm described in the pdf. */ public String encode(String msg) { /**** ADD CODE HERE ****/ return null; } /* * TODO: Decodes the input message using the algorithm described in the pdf. */ public String decode(String msg) { /**** ADD CODE HERE ****/ return null; } }Instructions and Starter Code As mentioned in the section before we will use a circular doubly linked list to represent a deck of cards. The starter code contains two files with five classes which are as follows: Deck - This class defines a deck of cards. Most of your work goes into this file. This class contains three nested classes: Card, PlayingCard, and Joker. SolitaireCipher - This class represents a stream cipher that uses the Solitaire algorithm to generate the keystream and then encode/decode messages. Please note that we defined all the members of the classes public for testing purposes. In reality, for better coding style, most of those methods and all of the fields should have been kept private. Methods you need to implement For this assignment you need to implement all of the methods listed below. See the starter code for the full method signatures. Your implementations must be efficient. For each method below, we indicate the worst case run time using 0() notation. Deck Deck(int numOf CardsPerSuit, int numOf Suits) : creates a deck with cards from Ace to numOf CardsPerSuit for the first numOfSuits in the class field suitsInOrder. The cards should be ordered first by suit, and then by rank. In addition to these cards, a red joker and a black joker are added to the bottom of the deck in this order. For example, with input 4 and 3, and suitsInOrder as specified in the file, the deck contains the following cards in this specific order: AC 2C 3C 4C AD 2D 3D 4D AH 2H 3H 4H RJ BJ The constructor should raise an IllegalArgumentException if the first input is not a number between 1 and 13 (both included) or the second input is not a number between 1 and the size of the class field suitsInOrder. Remember that a deck is a circular doubly linked list so make sure to set up all the pointers correctly, as well as the instance fields. Deck.Deck (Deck d) : creates a deck by making a deep copy of the input deck. Hint: use the method getCopy from the class Card. Disclaimer: this is not the correct way of making a deep copy of objects that contain circular references, but it is a simple one and good enough for our purposes. Deck.addCard (Card c): adds the input card to the bottom of the deck. This method runs in (1) Deck.shuffle(): shuffles the deck. There are different ways of doing this, but for this assign- ment you will need to implement an algorithm that uses the Fisher-Yates shuffle algorithm. The algorithm runs in O(n) using O(n) space, where n is the number of cards in the deck. To perform a shuffle of the deck follow the steps: - Copy all the cards inside an array - Shuffle the array using the following algorithm: for i from n-1 to 1 do j
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