Question
USING JAVAFX ONLY: The instructions are below: INSTRUCTIONS: Sorting with nested classes and lambda expressions This program is a bit different from our others. Just
USING JAVAFX ONLY:
The instructions are below:
INSTRUCTIONS:
Sorting with nested classes and lambda expressions
This program is a bit different from our others. Just like the last program, you will start with the project that is attached to this assignment. Remember to rename the project. You will lose points if you do not. Also, remember that you need to submit an Eclipse project, not just Java files. You will lose points there as well.
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 dont 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:
Properly named project, good code (avoid redundancy), nice UI, etc. (20 pts)
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
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 isnt a simple problem. You can do this in an inner class, anonymous class or even a static method in the Card classor something else you think of.
Turn your program in by zipping your project directory (or export it from Eclipse) and submit it to D2L. Remember, the Eclipse project needs a new name that contains your last name. Do not leave it the same as it is now. You may leave the package names just as they are.
Remember, I will be grading each of these on whether they work and whether you did each as specified. Note that if you were really developing something like this you might opt for everything in anonymous classes, or lambda expressions, or inner classes. However, I want to see that you can do all of these, and do them where they are specified, so follow the instructions carefully.
OK, you are all adding up the points, and yes, 113 would be possible if everything is exactly correct.
Here are the classes for this program:
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
// list of cards being used private 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
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
deck.shuffle(); ArrayList
deck.shuffle(); hand = deck.deal(5); showHand(hand); }
}
HandDisplayWindow.java class:
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; import cards.model.Card; import cards.model.Deck;
public class HandDisplayWindow { private final int SUIT_SIZE = 13; private Image[] cardImages = new Image[4*SUIT_SIZE]; private Stage myStage; ListView
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
@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 javafx.application.Application; import javafx.stage.Stage; import cards.model.Deck;
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); }
}
I need to add the following buttons to this program, this is a necessary. It also needs to do what the buttons are suppose to do, which it states in the instructions what they are suppose to do.
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.
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 isnt a simple problem. You can do this in an inner class, anonymous class or even a static method in the Card classor something else you think of.
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