D
David
I have to write a program that prints 20 random cards (suit,
faceValue). I have to have a driver program and write a constructor
with my own methods. I am fairly new at java, and this concept is
confusing to me at the moment. So far, I have written both the driver
and constructor programs. However, they do not work. It compiles and
runs, but every card says "Spade null". I think it has something to
do with my random number generator. Please help. Here's my driver
program:
***********************************************************
import java.util.Random;
class CardGenerator
{
public static void main (String [] args)
{
// Use to get random numbers
Random generator = new Random();
// Print out to screen
System.out.println ("Dealing 20 random cards: ");
// repeat this loop 20 times to get 20 cards
for (int i=1; i<=20; i++)
{
// declare new object
Card myCard = new Card (1,1);
// get random numbers for suit and faceValue
int suit = generator.nextInt(4) +1;
int faceValue = generator.nextInt(13) +1;
// get number from my methods
suit = myCard.getSuit();
faceValue = myCard.getFaceValue();
// print card
System.out.println (myCard);
}
}
}
************************************************************
Here's the program with the constructor and my methods:
************************************************************
// import classes
import java.util.Random;
class Card
{
// declare variables
private int suit;
private int faceValue;
String cardType;
String suitType;
// constructor
public Card (int suit, int faceValue)
{
// call methods
getSuit();
getFaceValue();
}
// gets value for suit
public int getSuit()
{
return suit;
}
// gets value for faceValue
public int getFaceValue()
{
return faceValue;
}
// decides the suit and card according to the random number
picked
public String toString()
{
// if statements to get suit type
if (suit == 1)
suitType = "Heart";
else if (suit == 2)
suitType = "Diamond";
else if (suit == 3)
suitType = "Club";
else
suitType = "Spade";
// switch statement to get card type
switch (faceValue)
{
case 1:
cardType = "Ace";
break;
case 2:
cardType = "2";
break;
case 3:
cardType = "3";
break;
case 4:
cardType = "4";
break;
case 5:
cardType = "5";
break;
case 6:
cardType = "6";
break;
case 7:
cardType = "7";
break;
case 8:
cardType = "8";
break;
case 9:
cardType = "9";
break;
case 10:
cardType = "10";
break;
case 11:
cardType = "Jack";
break;
case 12:
cardType = "Queen";
break;
case 13:
cardType = "King";
break;
}
// returns string
return (suitType + "\t" + cardType);
}
}
******************************************************
If someone could help me, please do. Because I am stuck and lost
right now. Thanks.
David
faceValue). I have to have a driver program and write a constructor
with my own methods. I am fairly new at java, and this concept is
confusing to me at the moment. So far, I have written both the driver
and constructor programs. However, they do not work. It compiles and
runs, but every card says "Spade null". I think it has something to
do with my random number generator. Please help. Here's my driver
program:
***********************************************************
import java.util.Random;
class CardGenerator
{
public static void main (String [] args)
{
// Use to get random numbers
Random generator = new Random();
// Print out to screen
System.out.println ("Dealing 20 random cards: ");
// repeat this loop 20 times to get 20 cards
for (int i=1; i<=20; i++)
{
// declare new object
Card myCard = new Card (1,1);
// get random numbers for suit and faceValue
int suit = generator.nextInt(4) +1;
int faceValue = generator.nextInt(13) +1;
// get number from my methods
suit = myCard.getSuit();
faceValue = myCard.getFaceValue();
// print card
System.out.println (myCard);
}
}
}
************************************************************
Here's the program with the constructor and my methods:
************************************************************
// import classes
import java.util.Random;
class Card
{
// declare variables
private int suit;
private int faceValue;
String cardType;
String suitType;
// constructor
public Card (int suit, int faceValue)
{
// call methods
getSuit();
getFaceValue();
}
// gets value for suit
public int getSuit()
{
return suit;
}
// gets value for faceValue
public int getFaceValue()
{
return faceValue;
}
// decides the suit and card according to the random number
picked
public String toString()
{
// if statements to get suit type
if (suit == 1)
suitType = "Heart";
else if (suit == 2)
suitType = "Diamond";
else if (suit == 3)
suitType = "Club";
else
suitType = "Spade";
// switch statement to get card type
switch (faceValue)
{
case 1:
cardType = "Ace";
break;
case 2:
cardType = "2";
break;
case 3:
cardType = "3";
break;
case 4:
cardType = "4";
break;
case 5:
cardType = "5";
break;
case 6:
cardType = "6";
break;
case 7:
cardType = "7";
break;
case 8:
cardType = "8";
break;
case 9:
cardType = "9";
break;
case 10:
cardType = "10";
break;
case 11:
cardType = "Jack";
break;
case 12:
cardType = "Queen";
break;
case 13:
cardType = "King";
break;
}
// returns string
return (suitType + "\t" + cardType);
}
}
******************************************************
If someone could help me, please do. Because I am stuck and lost
right now. Thanks.
David