Question

JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main...

JAVAFX ONLY PROGRAM!!!!!

SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS.

DIRECTIONS ARE BELOW:

DIRECTIONS:

The main point of the exercise is to demonstrate your ability to use various types of nested classes. Of course, sorting is important as well, but you don’t really need to do much more than create the class that does the comparison. In general, I like giving you some latitude in how you design and implement your projects. However, for this assignment, each piece is very specific as to how I want you to implement things. There are a number of parts to this program. Make certain you accomplish the two additions to the Card class early-on since much of the rest of your program will depend on that work.

THE REQUIREMENTS:

Implement the Comparable interface in the Card class. (15 pts)

Sort the cards lowest to highest based on suit within rank, e.g.

2 clubs

2 diamonds

2 hearts

2 spades

3 clubs

3 diamonds

etc.

Add a static nested class to the Card class that implements the Comparator interface. (15 pts)

Sort the cards lowest to highest based on rank within suit, e.g.

2 clubs

3 clubs

4 clubs

5 clubs

etc.

In the UI, add the following to your interface.

Deal Button (10 pts)

Implement the Deal action handler using an inner class (not local). You may want to pull some of the code from the show method that is already there. Or better, put the common code into a class and call it.

Fish Sort Button (15 pts)

Sort the code in a way that would be useful playing Go Fish. I.e. 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 (i.e. the natural order). It must not call any other method in the UI class.

Spades Sort Button (15 pts)

Order the cards in such a way that they would be useful playing Spades; i.e. 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; i.e. the Comparator that you added above.

Reverse Toggle Button (10 pts)

If this toggle is checked, the order of the sort is reversed, e.g.

AS, AH, 8H, 8D, 8C

becomes

8C, 8D, 8H, AH, AS

Clicking the toggle 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, but see below. This action can be accomplished in a number of ways. Some are simpler than others, some more efficient.

EXTRA CREDIT:(WHICH I REALLY NEED TO DO):

Fish Sort and Reverse Toggle (3 pts)

Find a means to reverse sort that does not require reversing after sorting for the Fish Sort. That is, the sort itself should do the reverse. Do not add code outside the lambda expression. It should check whether the toggle button is checked and act appropriately. You must keep the Fish Sort action in a lambda expression. This may take some research.

Bridge Sort Button (10 pts)

This sort is similar to the Spade sort where all of the cards are grouped by suit. However, the groups of suits should then be sorted by how many are in each suit. E.g. if there are 2 spades, 5 hearts, 3 diamonds and 3 clubs, the hearts would be first, followed by the diamonds, clubs and spades in that order. This will take some thinking on your part; it isn’t a simple problem. You can do this in an inner class, anonymous class or even a static method in the Card class…or something else you think of.

SINCE I POSTED THE INSTRUCTIONS AND THE REQUIREMENTS ON WHAT HAD TO BE DONE AND THE EXTRA CREDIT WHICH I REALLY NEED TO DO WITH IT, HERE ARE THE FOUR CLASSES THAT I HAVE DONE. PLEASE HELP ME GET THIS DONE RIGHT AND AS SOON AS POSSIBLE USING JAVAFX ONLY!!!!

***Cards.Model package:

****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);

}

}

****deck.java class:

import java.util.ArrayList;

import java.util.Random;

public class Deck

{

// list of cards still in the deck

private ArrayList<Card> deck = new ArrayList<>();

// list of cards being used

private ArrayList<Card> 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<Card> deal(int handSize)

{

ArrayList<Card> 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<Card> 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<Card> hand = deck.deal(5);

showHand(hand);

deck.shuffle();

hand = deck.deal(5);

showHand(hand);

}

}

***Cards.View package:

****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<Card> 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<Card>() {

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);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

To recognize numeric (or identifier or error) tokens takes a bit more work than required for the fixed tokens. There are a coparseIfThenElse (Sin,Sout) :- parseIf (Sin,R), parseName (R,S), parseThen(S,T), parseName (T,U), parseElse(U,V), parseName(V,parseIfElse (Sin,Sout) :- eat (if,Sin,R), parseBexp (R,S), eat (then,S,T) parseFnExp(T,U), parseElsePart (U,V) eat (endif,V,SIf A.1 and A 2 are different predicates or n < m Then output Failed Else Initialize the substitution to empty, the stack to c

Add a comment
Know the answer?
Add Answer to:
JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • Using JAVA FX how would I combine the two programs below into one interface that allows...

    Using JAVA FX how would I combine the two programs below into one interface that allows the user to pick which specific program they would like to play. They should be able to choose which one they want to play and then switch between them if necesary. All of this must be done in JAVA FX code Black jack javafx - first game program package application; import java.util.Arrays; import java.util.ArrayList; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.application.Application; import javafx.geometry.Pos; import javafx.geometry.Insets;...

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT