Question: Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE MUST BE RECURSIVE. Do not use loops (while, do/while,
Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE MUST BE RECURSIVE. Do not use loops (while, do/while, or for). Do not declare any variables outside of a method. You may declare local variables inside a method. Complete the following method: public static String decompress(String compressedText): Decompress the input text, which has been compressed using the RLE algorithm (previous hw assignment): Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of: the number of characters in the sequence the repeating character If a character does not repeat, it should be left alone. For example, consider the following string: qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT After applying the RLE algorithm, this string is converted into: q9w5e2rt5y4qw2Er3T In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w" characters. "5e" represents 5 consecutive lowercase "e" characters, etc. You may assume that the character counts will be single-digit numbers (a character will not repeat more than 9 times consecutively). Hint #1: remember that characters are represented by numeric codes. You can decrement a character variable as follows: char c = '7'; c--; // c will now hold the character '6' Hint #2: You probably will not need to use this hint for this problem. However, a fast way to convert a digit character into the numeric value of the digit is to subtract the character code for the digit zero: char c = '7'; // this has the character code 55, not 7 int x = c - '0'; // this produces the number 7 public class StringRec { // DO NOT DECLARE ANY VARIABLES HERE // (you may declare local variables inside methods) public static String decompress(String compressedText) { return null; // replace this line with your code } }
public class Deck { /** * An array of 52 or 54 cards. A 54-card deck contains two Jokers, * in addition to the 52 cards of a regular poker deck. */ private Card[] deck; /** * Keeps track of the number of cards that have been dealt from * the deck so far. */ private int cardsUsed; /** * Constructs a regular 52-card poker deck. Initially, the cards * are in a sorted order. The shuffle() method can be called to * randomize the order. (Note that "new Deck()" is equivalent * to "new Deck(false)".) */ public Deck() { this(false); // Just call the other constructor in this class. } /** * Constructs a poker deck of playing cards, The deck contains * the usual 52 cards and can optionally contain two Jokers * in addition, for a total of 54 cards. Initially the cards * are in a sorted order. The shuffle() method can be called to * randomize the order. * @param includeJokers if true, two Jokers are included in the deck; if false, * there are no Jokers in the deck. */ public Deck(boolean includeJokers) { if (includeJokers) deck = new Card[54]; else deck = new Card[52]; int cardCt = 0; // How many cards have been created so far. for ( int suit = 0; suit <= 3; suit++ ) { for ( int value = 1; value <= 13; value++ ) { deck[cardCt] = new Card(value,suit); cardCt++; } } if (includeJokers) { deck[52] = new Card(1,Card.JOKER); deck[53] = new Card(2,Card.JOKER); } cardsUsed = 0; } /** * Put all the used cards back into the deck (if any), and * shuffle the deck into a random order. */ public void shuffle() { for ( int i = deck.length-1; i > 0; i-- ) { int rand = (int)(Math.random()*(i+1)); Card temp = deck[i]; deck[i] = deck[rand]; deck[rand] = temp; } cardsUsed = 0; } /** * As cards are dealt from the deck, the number of cards left * decreases. This function returns the number of cards that * are still left in the deck. The return value would be * 52 or 54 (depending on whether the deck includes Jokers) * when the deck is first created or after the deck has been * shuffled. It decreases by 1 each time the dealCard() method * is called. */ public int cardsLeft() { return deck.length - cardsUsed; } /** * Removes the next card from the deck and return it. It is illegal * to call this method if there are no more cards in the deck. You can * check the number of cards remaining by calling the cardsLeft() function. * @return the card which is removed from the deck. * @throws IllegalStateException if there are no cards left in the deck */ public Card dealCard() { if (cardsUsed == deck.length) throw new IllegalStateException("No cards are left in the deck."); cardsUsed++; return deck[cardsUsed - 1]; // Programming note: Cards are not literally removed from the array // that represents the deck. We just keep track of how many cards // have been used. } /** * Test whether the deck contains Jokers. * @return true, if this is a 54-card deck containing two jokers, or false if * this is a 52 card deck that contains no jokers. */ public boolean hasJokers() { return (deck.length == 54); } } // end class Deck
import java.util.ArrayList; public class Hand { private ArrayList hand; // The cards in the hand. /** * Create a hand that is initially empty. */ public Hand() { hand = new ArrayList(); } /** * Remove all cards from the hand, leaving it empty. */ public void clear() { hand.clear(); } /** * Add a card to the hand. It is added at the end of the current hand. * @param c the non-null card to be added. * @throws NullPointerException if the parameter c is null. */ public void addCard(Card c) { if (c == null) throw new NullPointerException("Can't add a null card to a hand."); hand.add(c); } /** * Remove a card from the hand, if present. * @param c the card to be removed. If c is null or if the card is not in * the hand, then nothing is done. */ public void removeCard(Card c) { hand.remove(c); } /** * Remove the card in a specified position from the hand. * @param position the position of the card that is to be removed, where * positions are starting from zero. * @throws IllegalArgumentException if the position does not exist in * the hand, that is if the position is less than 0 or greater than * or equal to the number of cards in the hand. */ public void removeCard(int position) { if (position < 0 || position >= hand.size()) throw new IllegalArgumentException("Position does not exist in hand: " + position); hand.remove(position); } /** * Returns the number of cards in the hand. */ public int getCardCount() { return hand.size(); } /** * Gets the card in a specified position in the hand. (Note that this card * is not removed from the hand!) * @param position the position of the card that is to be returned * @throws IllegalArgumentException if position does not exist in the hand */ public Card getCard(int position) { if (position < 0 || position >= hand.size()) throw new IllegalArgumentException("Position does not exist in hand: " + position); return (Card)hand.get(position); } /** * Sorts the cards in the hand so that cards of the same suit are * grouped together, and within a suit the cards are sorted by value. * Note that aces are considered to have the lowest value, 1. */ public void sortBySuit() { ArrayList newHand = new ArrayList(); while (hand.size() > 0) { int pos = 0; // Position of minimal card. Card c = (Card)hand.get(0); // Minimal card. for (int i = 1; i < hand.size(); i++) { Card c1 = (Card)hand.get(i); if ( c1.getSuit() < c.getSuit() || (c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) ) { pos = i; c = c1; } } hand.remove(pos); newHand.add(c); } hand = newHand; } /** * Sorts the cards in the hand so that cards of the same value are * grouped together. Cards with the same value are sorted by suit. * Note that aces are considered to have the lowest value, 1. */ public void sortByValue() { ArrayList newHand = new ArrayList(); while (hand.size() > 0) { int pos = 0; // Position of minimal card. Card c = (Card)hand.get(0); // Minimal card. for (int i = 1; i < hand.size(); i++) { Card c1 = (Card)hand.get(i); if ( c1.getValue() < c.getValue() || (c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) ) { pos = i; c = c1; } } hand.remove(pos); newHand.add(c); } hand = newHand; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
