Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me solve this? The question and code is below. I keep receiving an error, Static Error: This class does not have a

Can you help me solve this? The question and code is below. I keep receiving an error, Static Error: This class does not have a static void main method accepting String[].

Write a toString method for the Deck class. It should return a single string that represents the cards in the deck. When it's printed, this string should display the same results as the print method in Section 13.1. Hint: You can use the + operator to concatenate strings, but it is not very efficient. Consider using java.lang.StringBuilder instead; see Section 10.11.

Deck.java

import java.util.Arrays; import java.util.Random;

/** * A deck of playing cards (of fixed length). */ public class Deck {

private Card[] cards;

/** * Constructs a standard deck of 52 cards. */ public Deck() { this.cards = new Card[52]; int index = 0; for (int suit = 0; suit <= 3; suit++) { for (int rank = 1; rank <= 13; rank++) { this.cards[index] = new Card(rank, suit); index++; } } }

/** * Constructs a deck of n cards (all null). */ public Deck(int n) { this.cards = new Card[n]; }

/** * Gets the internal cards array. */ public Card[] getCards() { return this.cards; }

/** * Displays each of the cards in the deck. */ public void print() { for (Card card : this.cards) { System.out.println(card); } }

/** * Randomly permutes the array of cards. */ public void shuffle() { }

/** * Chooses a random number between low and high, including both. */ private static int randomInt(int low, int high) { return 0; }

/** * Swaps the cards at indexes i and j. */ private void swapCards(int i, int j) { }

/** * Sorts the cards (in place) using selection sort. */ public void selectionSort() { }

/** * Finds the index of the lowest card * between low and high inclusive. */ private int indexLowest(int low, int high) { return 0; }

/** * Returns a subset of the cards in the deck. */ public Deck subdeck(int low, int high) { Deck sub = new Deck(high - low + 1); for (int i = 0; i < sub.cards.length; i++) { sub.cards[i] = this.cards[low + i]; } return sub; }

/** * Combines two previously sorted subdecks. */ private static Deck merge(Deck d1, Deck d2) { return null; }

/** * Returns a sorted copy of the deck using selection sort. */ public Deck almostMergeSort() { return this; }

/** * Returns a sorted copy of the deck using merge sort. */ public Deck mergeSort() { return this; }

/** * Reorders the cards (in place) using insertion sort. */ public void insertionSort() { } /** * Returns a string representation of the deck. */

@Override

public String toString() { StringBuilder builder = new StringBuilder("Deck : "); for(Card card : this.cards) { builder.append("Card : Rank: ").append(card.getRank()).append(" Suite: ").append(card.getSuit()).append(" "); } return builder.toString(); } }

Card.java

/** * A standard playing card. */ public class Card {

public static final String[] RANKS = { null, "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

public static final String[] SUITS = { "Clubs", "Diamonds", "Hearts", "Spades"};

private final int rank;

private final int suit;

/** * Constructs a card of the given rank and suit. */ public Card(int rank, int suit) { this.rank = rank; this.suit = suit; }

/** * Returns a negative integer if this card comes before * the given card, zero if the two cards are equal, or * a positive integer if this card comes after the card. */ public int compareTo(Card that) { if (this.suit < that.suit) { return -1; } if (this.suit > that.suit) { return 1; } if (this.rank < that.rank) { return -1; } if (this.rank > that.rank) { return 1; } return 0; }

/** * Returns true if the given card has the same * rank AND same suit; otherwise returns false. */ public boolean equals(Card that) { return this.rank == that.rank && this.suit == that.suit; }

/** * Gets the card's rank. */ public int getRank() { return this.rank; }

/** * Gets the card's suit. */ public int getSuit() { return this.suit; }

/** * Returns the card's index in a sorted deck of 52 cards. */ public int position() { return this.suit * 13 + this.rank - 1; }

/** * Returns a string representation of the card. */ public String toString() { return RANKS[this.rank] + " of " + SUITS[this.suit]; }

}

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago