I have completed the program for assignment 5, but need help improving upon it i
ID: 3796863 • Letter: I
Question
I have completed the program for assignment 5, but need help improving upon it in assignment 6. I have included the instructions and the code for assignment 5, as well as the instructions for assignment 6. What I need help on is assignment 6.
Here are the instructions for assignment 5:
Introduction
In Assignment 4 you created a rudimentary war game. In this assignment we are going to use class construction to improve upon it a bit. You will create a Card class. The Card class will hold all of the information about an individual card. This means the cards value and suit.
Additionally, we will create an enumerated type to relate the suit of the card.
Step 1 - The Card Class
For this first step you might want to create a new project called Assignment5 and then copy the code from Assignment4 into it. You don't have to do this step but it is always a good plan to be able to roll back to a previous version. Once you have done this test your code and make sure it works like assignment 4.
Once again, here is the link to download the card images:
https://mega.nz/#!tBJD3YLZ!YjGYuP7d4-6Etq1J_i4o3XTNVqjqjyaIhIxkGgaSLGw
Create a class called Card. You will need to make sure you have imported Image, ImageView, and javafx.scene.control.*. In the field of the class you will need to create the following private variables:
A Label to hold the image
A reference to an Image to hold the card image and to place in the Label
An int to hold the value of the card
A String to hold the name of the path to the image
Create a private boolean method called loadCard that takes as an argument a String that represents the full path to the Image. This method should do the following
Create space for the Image reference you created in the field by calling the constructor that takes the path to the image
Set the graphic on the Label you created in the field. Don't forget you will need an ImageView
For now simply have this method return true. In the future we will use this for potential error handling.
Create a public void method called setImage that takes as an argument a String that represents the path to the image to load. This method should do the following:
Set the String you created in the field that holds the path to the String passed as an argument
Call the loadCard method passing it the path to the Image
Create a public method called getCard that simply returns the Label that was created in the field.
At this point you should have a card that can be tested.
Step 2 - Test The Card
Inside the Assignment5.java (or whatever you called the main file) you should be able to create Cards and use them for the game. This involves the following:
Replace the code that creates the image with a new Card. We haven't created constructors yet so you will need two lines of code to replace:
Image imgCardLeft = new Image("file:img\155.gif");
You will have to create a new Card and then call the setImage method to initialize the Card. You will then call the setGraphic method on the label this time you are going to supply it an image by calling the getCard method that is a member of the Card you just created.
Please note that you will have to do this for all of the Labels you have created. Also note that this is not quite ideal but we have abstracted some of the details. We will get to the rest as we go.
Once you have replaced all of the Labels with Cards you should test your game to make sure it still works the same as it did in assignment 4
Step 3 Features
To make the card complete we need to keep track of both the Suit and that Cards value. To begin with create a java file called Suit.java. Inside this file create an enumerated type called Suit. An enumerated type is a way to relate a numeric value to a name. Like the days of the week are numbered 1 to 7 and correspond to the values Monday - Sunday. The enumerated type should simply look like the following:
public enum Suit{Diamonds, Hearts, Spades, Clubs}
Inside the Card class create a private Suit variable
to hold the suit of the Card. You might want to consider calling this variable suit.
In addition create a private int variable to hold the value of the card. You might want to consider calling this variable value.
Create a private method called getCardValue that takes as an argument a String that holds the path to the Card.
Step 4 - getCardValue
The getCardValue method's purpose is to determine the suit of the card, and it's value. You are going to have to extract the number of the card from the path and convert it to a numeric type. Consider King, Queen, and Jack to be worth 10 and the Ace worth 11.
Using the numeric value of the card determine the actual value the card has. I will leave it to you to design an algorithm to do this but please do not have a switch statement that contains 52 cases. You should try to work out an algorithm that mathematically determines the value. You will undoubtedly need to use decisions but try to write the code as concise as possible.
In addition to this you need to determine the the suit of the card. You should set the value and suit created in the field from this method. The suit would be something like this:
this.suit = Suit.Spades
Once you have this method finished create a public accessor method called getValue that simply returns the value of the Card. Also, create an accessor method called getSuit that will return the suit of the card. In order for this to work when a Card is created you are also going to want to call this method from the loadCard method.
At this point you should have a working Card class that will keep track of the suit and the value. It is not ideal at this point but is getting better. The last thing to do is to update the game so that it uses the value of the card instead of the name of the card.
Step 5 - Modifying The Game
Currently the game keeps score by adding one to the side that has the higher value card. This value is simply determined by the name of the card. We have just created a method that will return the Card's value so we want to use this method to determine which side wins. You are going to have to modify the decisions inside the handle method of the mouse clicked event.
We also want to change the score. Instead of adding one to the side with the higher value we now want to add the value of the card to the score. This means if the Card value is 10 the score will go up by 10.
At this point your game should work with the Card Class. It should also have updated scoring.
Here is the code for Assignment 5:
import javafx.scene.text.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.image.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.Random;
import javafx.scene.paint.Color;
import javafx.geometry.*;
import javafx.scene.input.MouseEvent;
public class Lab4 extends Application {
//variables declared in Lab4 in order to be accessible from all the methods
public boolean rightsTurn = true;
int leftCounter = 0;
int rightCounter = 0;
int rightVal = 0;
int leftVal = 0;
//creating Textfields
TextField tf1 = new TextField();
TextField tf2 = new TextField();
//creating cards
Label lblCardLeft = new Label();
Label lblCardRight = new Label();
Label lblCardDeck = new Label();
Label testNum1 = new Label("");
Label testNum2 = new Label("");
// @Override
public void start(Stage primaryStage) {
//fonts and colors
Font font = new Font("Courier", 25);
Font font2 = new Font ("Courier New", 20);
Color clr = Color.web("#0076a3");
Color clr2 = Color.web("#FF0000");
// label for scores
Label lblscore = new Label();
lblscore.setFont(font);
lblscore.setTextFill(clr);
lblscore.setText("Score: ");
//labels for textfields
Label left = new Label("Left:" );
Label right = new Label("Right:");
//creating reset button
Button btnreset = new Button();
btnreset.setText("reset");
//Declaring variables used to assign image file name
int num;
String filename = null;
int num2;
String filename2 = null;
//reset action event. Resets all variables of game
btnreset.setOnAction (new EventHandler() {
@Override
public void handle(ActionEvent event) {
rightVal = 0;
leftVal = 0;
rightCounter = 0;
leftCounter = 0;
tf1.setText( "0");
tf2.setText("0");
rightsTurn = true;
// testNum1.setText("");
// testNum2.setText("");
resetCardImages(lblCardLeft,lblCardDeck,lblCardRight);
}
});
// Event Listener for when the center card is clicked
lblCardDeck.setOnMouseClicked(new EventHandler() {
@Override
public void handle(MouseEvent event) {
Random random = new Random();
if (rightsTurn == true) {
int num = random.nextInt(154 - 101 + 1) + 101;
rightVal = num;
String filename = Integer.toString(num) + ".gif";
lblCardRight.setGraphic(new ImageView("file:img\"+filename));
// testNum1.setText(filename);
// ^test variable used to display filename on the canvas
rightsTurn = false;
}
else {
int num2 = random.nextInt(154 - 101 + 1) + 101;
String filename2 = Integer.toString(num2) + ".gif";
leftVal = num2;
lblCardLeft.setGraphic(new ImageView("file:img\"+filename2));
// testNum2.setText(filename2);
// ^test variable used to display filename on the canvas
if (rightVal > leftVal) {
rightCounter = rightCounter + 1;
String rightScore = Integer.toString(rightCounter);
tf2.setText(rightScore);
}
else if (leftVal > rightVal) {
leftCounter = leftCounter + 1;
String leftScore = Integer.toString(leftCounter);
tf1.setText(leftScore);
}
else {
//nada
}
rightsTurn = !rightsTurn;
}
}
});
//Creating an image item and adding to a Label
Image imgCard = new Image("file:img\"+filename);
Label lblCard = new Label("",new ImageView(imgCard));
lblCard.setGraphic(new ImageView(imgCard));
// Creating BorderPane and GridPane layouts, setting GridPane to be top of BorderPane and middle
BorderPane root = new BorderPane(); //root layout
GridPane top = new GridPane(); //top layout for scores
GridPane middle = new GridPane(); //center layout for cards
root.setTop(top);
root.setCenter(middle);
root.setBottom(btnreset);
top.setHgap(20);
top.setVgap(10);
top.add(lblscore, 0, 0);
top.add(new Label("left:" ),0 ,2 );
top.add(new Label("right:" ),2 ,2 );
top.add(tf1,1, 2);
top.add(tf2,3, 2);
middle.add(lblCardLeft,0,0); //add card left
// middle.add(testNum2,0,1);
middle.add(lblCardDeck,1,0); //add card center
middle.add(lblCardRight,2,0); // add card right
// middle.add(testNum1,2,1);
middle.setHgap(20); //create space between cards
middle.setAlignment(Pos.CENTER); // determine where the cards will alighn
Scene scene = new Scene(root, 600, 450);
primaryStage.setTitle("Assignment 4");
primaryStage.setScene(scene);
primaryStage.show();
this.resetCardImages(lblCardLeft,lblCardDeck,lblCardRight);
}
public void resetCardImages(Label x, Label y, Label z) {
// reset the card images
Image imgCardLeft = new Image("file:img\155.gif");
x.setGraphic(new ImageView(imgCardLeft));
Image imgCardDeck = new Image("file:img\155.gif");
y.setGraphic(new ImageView(imgCardDeck));
Image imgCardRight = new Image("file:img\155.gif");
z.setGraphic(new ImageView(imgCardRight));
}
public static void main(String[] args) {
launch(args);
}
}
Here are the instructions for Assignment 6:
Assignment 6 - Constructors & Composition
Purpose
The purpose of this assignment is get you familiar with overloaded and default constructors. It is also geared towards the use of delegating constructors to create one code path. In addition this assignment will have you create a Deck class that is composed of an ArrayList of Cards.
Step 1 - The Constructors
In the Card class create an overloaded constructor that takes as an argument a String that represents the path to an individual Card. To keep one code path this constructor should call the setImage method which in turn calls the loadCard method
Create a default constructor that simply sets the Card to back of the card image. This should be image 155. Again you should keep one code path by delegating the constructor.
Step 2 -Modify The Game
Now that you have working constructors you can remove the call to setImage. Where the game is being reset you can simply create the Card by calling the default constructor. In the handle method you can use the overloaded constructor to load the card from the random number that was generated.
Before moving on, test to make sure that your game is not broken. If it is go back and see if you can fix whatever error you are having.
About Composition
In Object Oriented Programming there are differing types of object relationships. The first one we are going to discuss and probably the most simple is that of composition. Composition is also known as a "hasa" realtionship. It is nothing more than an object being composed of other objects. In other words having another object in the field of the class. In this next part of the assignment we are going to create a Deck class that is composed of an ArrayList that holds Cards. In other words, a Deck "hasa" ArrayList.
Step 3 - The Deck Class
Create a class called Deck. You will have to import
java.util.ArrayList
Java.util.Collection
Inside the field of the class create an int variable called index that will serve to keep track of the card to be dealt. Create a final int called LAST_CARD. A final variable is a constant and cannot be changed. Set this to a value like 45. We are going to use this value to determine when the Deck needs to be reshuffled.
We have not talked about generics yet so we will consider this a peek ahead. Create an ArrayList called deck that will hold cards. This will declared like this:
ArrayList<Card> deck = new ArrayList<Card>();
This declaration allows the ArrayList called deck to use Cards exclusively.
Step 4 Deck Methods
Create a public void method called shuffle that can be used to shuffle the deck. One of the nice things about generics in Java is that they have a common set of algorithms that can be used to perform some very useful tasks. One of those algorithms is called shuffle. This means to shuffle the deck all you need to do is call:
Collections.shuffle(deck);
inside your shuffle method.
Create a private void method called loadCards that takes as an argument the path to where the cards are stored. This path should not contain the name of the card. You are going to derive that. The loadCards method should use a loop that will add all 52 Cards to the ArrayList called deck. To add a new Card to the ArrayList you will simply use the add method and supply a new Card as an argument. It is a good thing we created the overloaded constructor.
Create a public method called deal that will return a Card. This method is an accessor so it does not need any arguments. Inside this method check to see if the index is greater than or equal to LAST_CARD. If it is then shuffle the deck and reset index.
Return the next Card in the deck by calling the get method that is part of deck. You need to supply the index as an argument. Also, don't forget to increment the index or you will always return the same Card.
Step 5 Deck Constructors
Create an overloaded constructor that takes the path to where the cards are stored. Again, this path should not contain the name of the Card. Because we want to keep one code path have this constructor call loadCards
Create a default constructor that sets the default path to "file:img\". Again, keep one code path by delegating the constructors.
At this point you should have a working Deck of Cards.
Step 6 Test The Deck
Inside the field of the game create an instance of the Deck class. You should be able to use the default constructor. Inside the handle method you will use the deal method of deck to get a card to use. This should be as simple as calling deal, getting the value of the Card, and setting the graphics on the Label.
At this point you should have a working game that incorporates the Deck.
Explanation / Answer
import javafx.scene.text.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.image.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.Random;
import javafx.scene.paint.Color;
import javafx.geometry.*;
import javafx.scene.input.MouseEvent;
public class Lab4 extends Application {
//variables declared in Lab4 in order to be accessible from all the methods
public boolean rightsTurn = true;
int leftCounter = 0;
int rightCounter = 0;
int rightVal = 0;
int leftVal = 0;
//creating Textfields
TextField tf1 = new TextField();
TextField tf2 = new TextField();
//creating cards
Label lblCardLeft = new Label();
Label lblCardRight = new Label();
Label lblCardDeck = new Label();
Label testNum1 = new Label("");
Label testNum2 = new Label("");
// @Override
public void start(Stage primaryStage) {
//fonts and colors
Font font = new Font("Courier", 25);
Font font2 = new Font ("Courier New", 20);
Color clr = Color.web("#0076a3");
Color clr2 = Color.web("#FF0000");
// label for scores
Label lblscore = new Label();
lblscore.setFont(font);
lblscore.setTextFill(clr);
lblscore.setText("Score: ");
//labels for textfields
Label left = new Label("Left:" );
Label right = new Label("Right:");
//creating reset button
Button btnreset = new Button();
btnreset.setText("reset");
//Declaring variables used to assign image file name
int num;
String filename = null;
int num2;
String filename2 = null;
//reset action event. Resets all variables of game
btnreset.setOnAction (new EventHandler() {
@Override
public void handle(ActionEvent event) {
rightVal = 0;
leftVal = 0;
rightCounter = 0;
leftCounter = 0;
tf1.setText( "0");
tf2.setText("0");
rightsTurn = true;
// testNum1.setText("");
// testNum2.setText("");
resetCardImages(lblCardLeft,lblCardDeck,lblCardRight);
}
});
// Event Listener for when the center card is clicked
lblCardDeck.setOnMouseClicked(new EventHandler() {
@Override
public void handle(MouseEvent event) {
Random random = new Random();
if (rightsTurn == true) {
int num = random.nextInt(154 - 101 + 1) + 101;
rightVal = num;
String filename = Integer.toString(num) + ".gif";
lblCardRight.setGraphic(new ImageView("file:img\"+filename));
// testNum1.setText(filename);
// ^test variable used to display filename on the canvas
rightsTurn = false;
}
else {
int num2 = random.nextInt(154 - 101 + 1) + 101;
String filename2 = Integer.toString(num2) + ".gif";
leftVal = num2;
lblCardLeft.setGraphic(new ImageView("file:img\"+filename2));
// testNum2.setText(filename2);
// ^test variable used to display filename on the canvas
if (rightVal > leftVal) {
rightCounter = rightCounter + 1;
String rightScore = Integer.toString(rightCounter);
tf2.setText(rightScore);
}
else if (leftVal > rightVal) {
leftCounter = leftCounter + 1;
String leftScore = Integer.toString(leftCounter);
tf1.setText(leftScore);
}
else {
//nada
}
rightsTurn = !rightsTurn;
}
}
});
//Creating an image item and adding to a Label
Image imgCard = new Image("file:img\"+filename);
Label lblCard = new Label("",new ImageView(imgCard));
lblCard.setGraphic(new ImageView(imgCard));
// Creating BorderPane and GridPane layouts, setting GridPane to be top of BorderPane and middle
BorderPane root = new BorderPane(); //root layout
GridPane top = new GridPane(); //top layout for scores
GridPane middle = new GridPane(); //center layout for cards
root.setTop(top);
root.setCenter(middle);
root.setBottom(btnreset);
top.setHgap(20);
top.setVgap(10);
top.add(lblscore, 0, 0);
top.add(new Label("left:" ),0 ,2 );
top.add(new Label("right:" ),2 ,2 );
top.add(tf1,1, 2);
top.add(tf2,3, 2);
middle.add(lblCardLeft,0,0); //add card left
// middle.add(testNum2,0,1);
middle.add(lblCardDeck,1,0); //add card center
middle.add(lblCardRight,2,0); // add card right
// middle.add(testNum1,2,1);
middle.setHgap(20); //create space between cards
middle.setAlignment(Pos.CENTER); // determine where the cards will alighn
Scene scene = new Scene(root, 600, 450);
primaryStage.setTitle("Assignment 4");
primaryStage.setScene(scene);
primaryStage.show();
this.resetCardImages(lblCardLeft,lblCardDeck,lblCardRight);
}
public void resetCardImages(Label x, Label y, Label z) {
// reset the card images
Image imgCardLeft = new Image("file:img\155.gif");
x.setGraphic(new ImageView(imgCardLeft));
Image imgCardDeck = new Image("file:img\155.gif");
y.setGraphic(new ImageView(imgCardDeck));
Image imgCardRight = new Image("file:img\155.gif");
z.setGraphic(new ImageView(imgCardRight));
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.