So I have to code a HiLo card game program in java and here are the instructions on how it works:
there are two classes Card and Deck
The Card Class contains 2 private variables int suit and int faceValue A Constructor which takes the 2 variables as parameters like this: + Card(int suit, int faceValue) Getters --> getSuit:int and getFaceValue:int A compareTo(Card card):int method A toString():String method
The Deck Class contains a private random variable of Random type private random : Random A no parameter constructor Deck() A deal method that takes no parameter and of Card type
The Card class has a suit which is a digit from 0 to 3 that represents the suit of a card: - 0 represents Spades (which is the highest ranked suit) - 1 represents Hearts (the second highest suit) - 2 represents Diamonds (the third highest suit) - 3 represents Clubs (the weakest suit)
The faceValue is a value between 1 and 13; 1 represents Ace, 11 is Jack, 12 is Queen and 13 is King.
The compareTo method compares the current instance (i.e., this) with the parameter card. If the two objects have the same suit and faceValue, the method returns 0. If the current instance is higher (higher value, or if they are the same value, higher suit), then the method returns a positive value (it can be +1 or any other positive value since the magnitude is not important, only the sign). If the current instance is lower, return a negative value.
The toString method returns a String representation of the card (e.g., “Ace of Spades”, “2 of Hearts”)
The Deck class has an instance of type Random which it uses when instantiating and dealing a new Card.
Now code an application class called HiLoApp which will: - instantiate a Deck object - deal and display the first card - ask the user to choose -1 if they think the next card will be lower, +1 if the next card will be higher, or 0 to quit the game o deal and display the next card o display whether the user chose correctly o count the number of attempts and correct guesses
The main method is to continuously carry out these steps until the user enters 0 to quit the game. At this point, display the total number of attempts and correct guesses made by the user.
So far I coded this for the Card class:
public class Card
{
private int suit;
private int faceValue;
//Constructor
public Card(int suit, int faceValue)
{
this.suit = suits;
this.faceValue = faceValue;
card = deal(); //???
}
public int getSuit()
{
return suit;
}
public int getFaceValue()
{
return faceValue;
}
public int compareTo(Card card)
{
}
public String toString
{
String cardName = null;
switch (faceValue)
{
case 2:
cardName = "Two";
break;
case 3:
cardName = "Three";
break;
case 4:
cardName = "Four";
break;
case 5:
cardName = "Five";
break;
case 6:
cardName = "Six";
break;
case 7:
cardName = "Seven";
break;
case 8:
cardName = "Eight";
break;
case 9:
cardName = "Nine";
break;
case 10:
cardName = "Ten";
break;
case 1:
cardName = "Ace";
break;
case 11:
cardName = "Jack";
break;
case 12:
cardName = "Queen";
break;
case 13:
cardName = "King";
break;
}
switch (suit)
{
case 0:
cardName += "Of Spades";
break;
case 1:
cardName += "Of Hearts";
break;
case 2:
cardName += "Of Diamonds";
break;
case 3:
cardName += "Of Clubs";
break;
}
return cardName;
}
The Deck Class:
import java.util.Random;
public class Deck
{
private Random random;
public Deck()
{
random = 0;
}
public Card deal()
{
while (num != 0)
{
random = new Random();
suit = random.nextInt(3);
faceValue = random.nextInt(13) + 1;
}
return //how to return suit and faceValue at the same time?
}
and The Application:
import java.util.Random;
import java.util.Scanner;
public class HiLo
{
public static void main(String [] args)
{
//to quit the game
int num;
//getting input from user
Scanner keyboard = new Scanner(System.in);
Deck deck1 = new Deck();
= deck1.deal()
My questions are: I don't understand where the card parameter in the compareTo method comes from. Also the reason why I am not able to fully code the program is because I don't understand how it works exactly. Where does the initial dealing of the first card happen? Does the card class have access to the Deck class in order to use the deal method? I see the deal method returns a Card class type but how can I return both the suit and the faceValue at the same time?
Aucun commentaire:
Enregistrer un commentaire