0

I got dinged points for these:
Instructor wants 1. The class is divided into multiple methods, mentioned a Constructor instead? 2. The toString method is included in the Card class to return the face and suit.

What did I do wrong? How do I fix it? I am pretty lost and don't know where to even start...

import java.util.Random;

public class Card
{

public String cardGeneration()
{

Random generator = new Random();

int x; // random integer representing suit
int y; // random integer for card value
String mysuit, mynumber; //assigning names, these will be changed 
int suits = 4, numbers = 13; //depending what card is picked


x = generator.nextInt(suits); // random integer between 0 & 3 inclusive

if (x == 0)
mysuit = "Clubs";
else if (x == 1)
mysuit = "Diamonds";
else if (x == 2)
mysuit = "Hearts";
else if (x == 3)
mysuit = "Spades";
else
mysuit = "NA";

y = generator.nextInt(numbers) + 1; // add 1 so that y is between 1 & 13 inclusive

if (y == 1)
mynumber = "Ace";
else if (y == 2)
mynumber = "Two";
else if (y == 3)
mynumber = "Three";
else if (y == 4)
mynumber = "Four";
else if (y == 5)
mynumber = "Five";
else if (y == 6)
mynumber = "Six";
else if (y == 7)
mynumber = "Seven";
else if (y == 8)
mynumber = "Eight";
else if (y == 9)
mynumber = "Nine";
else if (y == 10)
mynumber = "Ten";
else if (y == 11)
mynumber = "Jack";
else if (y == 12)
mynumber = "Queen";
else if (y == 13)
mynumber = "King";
else
mynumber = "NA";

String result = mynumber + " of " + mysuit; 
return result;


    } 
}

Thanks!

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by adarshcu
0

Yes.
First, add suit and number to your card class. Move the generation part to a tester class.

Essentially programming should be a way such that the single functionality performed in a function. Here you could,

1.Have a method to generate random number separately.
2. Pass the random number from function 1 listed here to a function to get a suit.
3. Pass the second random number from function 1 listed here to a function to get a card number.

Also,override the toString method in card class. Its present for all classes in general,override it to display a card number and a suit.
Eg.

public String toString(){
//    Print number and suit;
}

You could use a constructor to initialize the values for your card. Consider using switch statements instead of the if-else's....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.