Question: ************Need Help with Comparable Interfaces and static nested Comparator Interfaces.NEED DONE ASAP!!!!!!!************* I need to implement the Comparable interface in the Card Class. I need

************Need Help with Comparable Interfaces and static nested Comparator Interfaces.NEED DONE ASAP!!!!!!!*************

I need to implement the Comparable interface in the Card Class. I need to sort the cards LOWEST to HIGHEST based on SUIT WITHIN RANK for example: 2 of clubs, 2 of diamonds, 2 of hearts, 2 of spades, 3 of clubs, 3 of diamonds and so on. Then I need to add a STATIC NESTED CLASS to the CARD CLASS that implements the COMPARATOR interface. It needs to sort the cards LOWEST to HIGHEST based on RANK WITHIN SUIT for example: 2 of clubs, 3 of clubs, 4 of clubs, 5 of clubs and so on.

The CARD.JAVA CLASS is below:

CARD.JAVA CLASS:

import java.util.Comparator;

public class Card

{

public final int SUIT_SIZE = 13;

public static final int CLUB = 0;

public static final int DIAMOND = 1;

public static final int HEART = 2;

public static final int SPADE = 3;

private int suit; // clubs = 0, diamonds = 1, hearts = 2, spades = 3

private int rank; // deuce = 0, three = 1, four = 2, ..., king = 11, ace = 12

private boolean isFaceUp = true; // not used for our program

// create a new card based on integer 0 = 2C, 1 = 3C, ..., 51 = AS

public Card(int card)

{

rank = card % SUIT_SIZE;

suit = card / SUIT_SIZE;

}

public int getRank()

{

return rank;

}

public int getSuit()

{

return suit;

}

public boolean isFaceUp()

{

return isFaceUp;

}

public void flip()

{

isFaceUp = !isFaceUp;

}

// represent cards like "2H", "9C", "JS", "AD"

public String toString() {

String ranks = "23456789TJQKA";

String suits = "CDHS";

return ranks.charAt(rank) + "" + suits.charAt(suit);

}

}

After I get the Comparable and Comparator working, in the UI, I need to add the Following to my INTERFACE:

DEAL BUTTON: IMPLEMENT THE DEAL BUTTON ACTION HANDLER USING AN INNER CLASS (NOT LOCAL). YOU MAY WANT TO PULL SOME CODE FROM THE SHOW METHOD THAT IS ALREADY THERE. OR BETTER, PUT THE COMMON CODE INTO A CLASS AND CALL IT.

FISH SORT BUTTON: SORT THE CODE IN A WAY THAT WOULD BE USEFUL PLAYING GO FISH. FOR EXAMPLE, WITH ALL OF THE CARDS GROUPED TOGETHER. IMPLEMENT THE ACTION HANDLER USING A LAMBDA EXPRESSION IN THE UI. THE CODE IN THE LAMBDA EXPRESSION SHOULD DEPEND ON THE CARD CLASS FOR THE ACTUAL SORTING (FOR EXAMPLE THE NATURAL ORDER). IT MUST NOT CALL ANY OTHER METHOD IN THE UI CLASS.

SPADES SORT BUTTON: ORDER THE CARDS IN SUCH A WAY THAT THEY WOULD BE USEFUL IN PLAYING SPADES, FOR EXAMPLE WITH THE CARDS SORTED BY RANK WITHIN SUIT AND THE SUITS IN THE ORDER PROVIDED BY THE NESTED CLASS IN #2 ABOVE. YOU ACTUALLY WANT TO REVERSE THIS SORT BEFORE DISPLAYING. IMPLEMENT THE ACTION HANDLER USING AN ANONYMOUS CLASS IN THE UI. THE CODE IN THE ANONYMOUS CLASS SHOULD DEPEND ON THE CARD CLASS FOR THE ACTUAL SORTING FOR EXAMPLE, THE COMPARATOR THAT YOU ADDED ABOVE.

REVERSE TOGGLE BUTTON: IF THIS TOGGLE BUTTON IS CHECKED, THE ORDER OF THE SORT IS REVERSED, FOR EXAMPLE: AS, AH, 8H, 8D, 8C BECOMES 8C, 8D, 8H, AH,AS. CLICKING THE TOGGLE BUTTON SHOULD HAVE NO EFFECT. HOWEVER, PUSHING THE SPADES SORT BUTTONS SHOULD RESULT IN A REVERSED ORDERING IF THE TOGGLE BUTTON IS SELECTED. UNCHECKING THE BUTTON AND PUSHING SPADES SORT SHOULD RESULT IN THE ORIGINAL SORT. NOTE THAT THIS DOES NOT MERELY REVERSE THE ORDER; IF THE BUTTON IS CHECKED PUSHING THE SPADES SORT BUTTON ANY NUMBER OF TIMES SHOULD ALWAYS RESULT IN THE SAME ORDERING. YOU DO NOT HAVE TO APPLY THIS TO THE FISH SORT. THIS ACTION CAN BE ACCOMPLISHED IN A NUMBER OF WAYS. SOME ARE SIMPLER THAN OTHERS, SOME MORE EFFICIENT.

HERE ARE THE REST OF THE CLASSES FOR THE PROGRAM:

DECK.JAVA CLASS:

import java.util.ArrayList;

import java.util.Random;

public class Deck

{

// list of cards still in the deck

private ArrayList deck = new ArrayList<>();

// list of cards being used

private ArrayList used = new ArrayList<>();

// used to shuffle the deck

Random dealer = new Random();

public Deck()

{

// builds the deck

for (int i = 0; i < 52; i++) {

deck.add(new Card(i));

}

}

public ArrayList deal(int handSize)

{

ArrayList hand = new ArrayList<>();

// do we need more cards? If so, shuffle

if (deck.size() < handSize) {

shuffle();

}

for (int i=0; i < handSize; i++) {

Card next = deck.remove(deck.size() - 1);

hand.add(next);

used.add(next);

}

return hand;

}

public void shuffle()

{

deck.addAll(used);

for (int i=0; i < deck.size() - 1; i++) {

int swap = dealer.nextInt(deck.size() - i) + i;

if (swap != i) {

Card tmp1 = deck.get(i);

Card tmp2 = deck.get(swap);

deck.set(i, tmp2);

deck.set(swap, tmp1);

}

}

}

// just used for testing

public static void showHand(ArrayList hand)

{

for (Card c : hand) {

System.out.printf(" %s",c);

}

System.out.println();

}

// just used for testing

public static void main(String args[])

{

Deck deck = new Deck();

deck.shuffle();

ArrayList hand = deck.deal(5);

showHand(hand);

deck.shuffle();

hand = deck.deal(5);

showHand(hand);

}

}

HANDDISPLAYWINDOW.JAVA CLASS:

import cards.model.Card;

import cards.model.Deck;

import javafx.geometry.Orientation;

import javafx.scene.Scene;

import javafx.scene.control.ListCell;

import javafx.scene.control.ListView;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.BorderPane;

import javafx.stage.Stage;

public class HandDisplayWindow

{

private final int SUIT_SIZE = 13;

private Image[] cardImages = new Image[4*SUIT_SIZE];

private Stage myStage;

ListView listView = new ListView<>();

Deck deck;

int handSize;

public HandDisplayWindow(Stage stage, int size)

{

handSize = size;

myStage = stage;

myStage.setTitle("Card Hand");

BorderPane pane = new BorderPane();

Scene scene = new Scene(pane);

myStage.setScene(scene);

listView.setCellFactory(param -> new ListCell() {

private ImageView imageView = new ImageView();

@Override

public void updateItem(Card card, boolean empty)

{

super.updateItem(card, empty);

if (empty) {

setGraphic(null);

} else {

// determine the index of the card

int index = card.getSuit() * SUIT_SIZE + card.getRank();

imageView.setImage(cardImages[index]);

imageView.setPreserveRatio(true);

imageView.setFitWidth(50);

setGraphic(imageView);

}

}

});

listView.setOrientation(Orientation.HORIZONTAL);

pane.setCenter(listView);

myStage.setHeight(150);

myStage.setWidth(68 * handSize);

loadImages();

}

private void loadImages()

{

String resourceDir = "file:resources/cardspng/";

char[] suits = { 'c', 'd', 'h', 's' };

char[] rank = { '2', '3', '4', '5', '6', '7', '8', '9', '0', 'j', 'q', 'k', 'a' };

int slot = 0;

// load images

for (int s = 0; s < 4; s++) {

for (int r = 0; r < SUIT_SIZE; r++) {

String path = resourceDir + suits[s] + rank[r] + ".png";

cardImages[slot] = new Image(path);

slot++;

}

}

}

public void show(Deck deck)

{

this.deck = deck;

if (deck != null)

listView.getItems().setAll(deck.deal(handSize));

myStage.show();

}

}

MAINAPPLICATION.JAVA CLASS:

import cards.model.Deck;

import javafx.application.Application;

import javafx.stage.Stage;

public class MainApplication extends Application

{

@Override

public void start(Stage primaryStage) throws Exception

{

HandDisplayWindow ui = new HandDisplayWindow(primaryStage, 13);

Deck deck = new Deck();

deck.shuffle();

ui.show(deck);

}

public static void main(String[] args)

{

Application.launch(args);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!