Random Card Class

Create Card Class

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 30231 Views - Last Post: 23 February 2008 - 12:42 PM Rate Topic: -----

#1 sumguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-February 08

Random Card Class

Post icon  Posted 09 February 2008 - 03:17 PM

I have to design and implement a class called Card that represnts a standard playing card. Each card has a suit and a face value. Create a program that deals 20 random cards. So i did the following, but my teacher says its not right?
Help Plz =)

//prgrm 4.6	 
//----------------------------------
import java.util.Random;

public class Card 
{
	public static void main(String[] args) 
	{
int a, b, c, d, e, f, g, h, i, j, k, l ,m , n, o, p ,q, r, s, t; // letters to start the random generator. 

System.out.println("This is a card deck using numbers as code.");
System.out.println ( "The following are the number codes for the cards face." );
System.out.println ("Any Joker is a 0. Clubs = 1-13; Hearts = 14-26; Spades = 27-39; Diamonds = 40-52."); //Number code
Random generator = new Random();

a= generator.nextInt(53); //random card
b= generator.nextInt(53); //random card
c= generator.nextInt(53); //random card
d= generator.nextInt(53); //random card
e= generator.nextInt(53); //random card
f= generator.nextInt(53); //random card
g= generator.nextInt(53); //random card
h= generator.nextInt(53); //random card
i= generator.nextInt(53); //random card
j= generator.nextInt(53); //random card
k= generator.nextInt(53); //random card
l= generator.nextInt(53); //random card
m= generator.nextInt(53); //random card
n= generator.nextInt(53); //random card
o= generator.nextInt(53); //random card
p= generator.nextInt(53); //random card
q= generator.nextInt(53); //random card
r= generator.nextInt(53); //random card 
s= generator.nextInt(53); //random card
t= generator.nextInt(53); //random card

	
System.out.println("Joker = 0.\nAce = 1,14,27,40.\nNumbers = 2-10,15-23,28-36,41-49(includes:2,3,4,5,6,7,8,9,10)\nRoyals are: \nQueen = 11,24,37,50.\nKing = 12,25,38,51.\nJack = 13,26,39,52.");
//Calls for the random generated numbers.
System.out.println ("Random cards: " + a + "," + b + ","+ c + ","+ d + ","+ e + ","+ f + ","+ g + ","+ h + ","+ i + ","+ j + ","+ k + ","+ l + ","+ m + ","+ n + ","+ o + ","+ p + ","+ q + ","+ r + ","+ s + ","+ t );
System.out.println ("Just match the numbers to the number code above. *** EXAMPLE BELOW. ***");
System.out.println ("If the Random Number is 40, it is Ace of Diamonds.");	
		}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Random Card Class

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: Random Card Class

Posted 09 February 2008 - 03:59 PM

You can't elaborate any further on what your teacher said besides "it is wrong"? (btw it is but that is besides the point).

How far are you along in the class? Do you know about enums? Have you worked with generics? Let us know how far along you are and what you have to work with so that we can give you a good solution.

One solution (all be it a bit more advanced perhaps for you) can be found at the site below. Take a look at it and perhaps it will give you an idea of which direction to go.

Java Pages, enum description with Card class example

Let us know the deal. Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#3 sumguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-February 08

Re: Random Card Class

Posted 10 February 2008 - 07:28 PM

Thank you for your response, and i still have yet to get a response from my teacher on what should be fixed, or what hes looking for....

I have not yet learned ENUMS, and this is part of the chapter that is on Writing Classes.

I have learned Arrays, if that might help?

Ok, i will repsond later on wat he is lookin for.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: Random Card Class

Posted 10 February 2008 - 07:31 PM

Well keep in mind that you will in all honesty need two files. One that defines a card and one that is the main program which uses the card class. The card itself is going to have the two data members. One for face and one for suit. The example link I provided shows you that, but they of course use enums to represent the face and suit.

Just let us know what your teacher says and then we can get you moving in the right direction. :)
Was This Post Helpful? 0
  • +
  • -

#5 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Random Card Class

Posted 10 February 2008 - 08:39 PM

where do you go to school?
Was This Post Helpful? 0
  • +
  • -

#6 sumguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-February 08

Re: Random Card Class

Posted 10 February 2008 - 10:59 PM

Odd Question...why?
Was This Post Helpful? 0
  • +
  • -

#7 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Random Card Class

Posted 10 February 2008 - 11:01 PM

because i want to stalk you.......

i had the exact same project and were wondering if you go to UT Austin.
Was This Post Helpful? 0
  • +
  • -

#8 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Random Card Class

Posted 10 February 2008 - 11:16 PM

i wouldn't normally go out giving it this easily because your not learning anything by grabbing it but i'm going to sleep .....

so please understand what its doing instead of copying my code to turn in.

import java.util.*;
/** 
 * File Name: Card.java
 * Description: A class that represent a card out of a deck of cards
 * Name: Tyler Lee
 * UT EID: TJL262
 * Course Name: CS 303E
 * Unique Number: 54720
 * Date Created: 3/25/2007
 * Date Last Modified: 3/26/2007
 */
public class Card
{
// intializes two variables for the card
	private int suit;
	private int value;

// constructor of the card that creates a random suit and value for the card
	public Card()
	{
	 Random num = new Random();
	 suit = num.nextInt(4);
	 value = num.nextInt(13)+1;
	}

// constructor of the card that takes an int representing suit and value
	public Card(int Card_suit, int Card_value)
	{
		suit = Card_suit;
		value = Card_value;
	}

// returns the numeric value of the card	
	public int getValue()
	{
		return value;
	}

// returns the numeric coded value of the card' suit
	public int getSuit()
	{
		return suit;
	}

public String getSuitAsString()
{
	// Return a String representing the card's suit.
	// (If the card's suit is invalid, "??" is returned.)
	switch ( suit ) {
	case 3: return "Spades";
	case 2: return "Hearts";
	case 1: return "Diamonds";
	case 0: return "Clubs";
	default: return "??";
}
}

public String getValueAsString()
{
	// Return a String representing the card's value.
	// If the card's value is invalid, "??" is returned.
	switch ( value )
	{
		case 1: return "Ace";
		case 2: return "2";
		case 3: return "3";
		case 4: return "4";
		case 5: return "5";
		case 6: return "6";
		case 7: return "7";
		case 8: return "8";
		case 9: return "9";
		case 10: return "10";
		case 11: return "Jack";
		case 12: return "Queen";
		case 13: return "King";
		default: return "??";
	}
}

// overrides toString method,
public String toString()
{
	// Return a String representation of this card, such as
	// "10 of Hearts" or "Queen of Spades".
	return getValueAsString() + " of " + getSuitAsString();
} 

// Compares the value of the card, to the value, as an int, of another card
// returns true if value > other card, false if not
public boolean compareValue(int cardOther)
{
return value > cardOther;
}
}
	
	


Was This Post Helpful? 0
  • +
  • -

#9 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Re: Random Card Class

Posted 10 February 2008 - 11:42 PM

or may be your teacher is looking for Math.random()
Teachers are very specific to what they teach (LOL)
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5905
  • View blog
  • Posts: 12,808
  • Joined: 16-October 07

Re: Random Card Class

Posted 11 February 2008 - 05:55 AM

You have two things going on here. One is the random selection of 20 cards, the other is the name of the cards.

There is an implicit problem with the 20 card picker; duplicates. Are you allowed to have duplicates?

Are you supposed to be using arrays? How about ArrayLists? Are you supposed to have a Card class?

Here is how I'd solve this problem. Create a deck of cards in an array, 53 long. You do this by just making an array of int and loading the value same as index. I'd then randomly pick from the array, based on the number of cards left. Here's where an ArrayList would make the job easier.

For print out, make a method that gives output based on the rules you've been given. Since someone has already offered a Card class, I'll give the method as I think your instructor would like it. It could be more elegant, but I think this will make the most sense.

public String getCardName(int cardIndex) {
	//The rules: Any Joker is a 0. Clubs = 1-13; Hearts = 14-26; Spades = 27-39; Diamonds = 40-52."
	if ((cardIndex<0) || (cardIndex>52)) { return "BAD CARD"; }
	if (cardIndex==0) { return "Joker"; }
	int cardNum;
	String suitName;
	if (cardIndex<14) {
		cardNum = cardIndex;
		suitName = "Clubs";
	} else if (cardIndex<27) {
		cardNum=cardIndex-13;
		suitName = "Hearts";
	} else if (cardIndex<40) {
		cardNum=cardIndex-26;
		suitName = "Spades";
	} else {
		cardNum=cardIndex-39;
		suitName = "Spades";
	}
	String cardValueName;
	if (cardNum==1) {
		cardValueName = "Ace";
	} else if (cardNum==11) {
		cardValueName = "Jack";
	} else if (cardNum==12) {
		cardValueName = "Queen";
	} else if (cardNum==13) {
		cardValueName = "King";
	} else {
		cardValueName = String.valueOf(cardNum);
	}
	return cardValueName + " of " + suitName;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#11 sumguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-February 08

Re: Random Card Class

Posted 11 February 2008 - 06:45 PM

Wow thanks to all of you so much

I will post up letting you know what the results are.
Was This Post Helpful? 0
  • +
  • -

#12 sumguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-February 08

Re: Random Card Class

Posted 13 February 2008 - 10:29 PM

I have a QUESTION! , the codes that were provided by Capty99 and Baavgai, have compilation errors. The IDE says that there is a main method missing. So when i trued to add the public static void main etc..it gave me more errors. i was wondering if those codes you provided are supposed to be continuations.

PPLLLLZZZ HELP =)
Was This Post Helpful? 0
  • +
  • -

#13 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Random Card Class

Posted 13 February 2008 - 11:37 PM

well mine was, its a class representing a card.

you have to make some implement some of the methods to make it work.

its too late for me to troubleshoot so here ya go:

another class called deck

import java.util.*;
/** 
 * File Name: Deck.java
 * Description: A class that represents an array of cards
 * Name: Tyler Lee
 * UT EID: TJL262
 * Course Name: CS 303E
 * Unique Number: 54720
 * Date Created: 4/30/2007
 * Date Last Modified: 4/30/2007
 */
public class Deck
{
	private Card[] Deck; //Initialize Deck array
	private int top; // Keeps track of the top card on the deck (access with getTop)
	private int deckSize; // Keeps track of the number of cards in the deck (access with getDeckSize)
	private Card[] DeckTemp; // Used in shuffling method
	private Card[] DeckTemp2; // Used in shuffling method
	
	
	//Constructor for Deck that assigns each card of 52 in a deck, to the array
	public Deck()
	{
		Deck = new Card[52];
		top = 0;
		int d = 0;
		deckSize = 52;
		
		// p stands for the suit
		// i for the card in each suit
		// as cards are assigned, if one suit fills up, it moves on to the next one
		for(int p=3;p>=0;p--)
		{
			for(int i=1; i<=13; i++, d++)
			{
			Deck[d] = new Card(p, i);
			}
		}
	}
	
	// method for shuffling cards, randomly picks two, and trades their spots in the deck
	// repeats 500 times
	public void shuffle()
	{
		for(int count = 0; count<501; count++)
		{
		Random num = new Random();
		// picks two random numbers from the number left in the deck,
		// and then adds the 'top' so as to not include the cards
		// already removed from the deck
		int num1 = (num.nextInt(deckSize) + top);
		int num2 = (num.nextInt(deckSize) + top);
		DeckTemp = new Card[1];
		DeckTemp2 = new Card[1];
		DeckTemp[0] = Deck[num1];
		DeckTemp2[0] = Deck[num2];
		Deck[num2] = DeckTemp[0];
		Deck[num1] = DeckTemp2[0];
		}	  
	}
	
	// pulls the card off the top of the deck and returns its value
	// moves the marker marking the top card to the next int,
	// decreases deckSize by 1
	public Card dealCard()
	{
		top += 1;
		deckSize -= 1;
		return Deck[top];
	}
	
	// returns the topcard of the deck
	public Card getTop()
	{
		System.out.println(Deck[top]);
		return Deck[top];
	}
	
	// returns the deckSize
	public int getDeckSize()
	{
		System.out.println(deckSize);
		return deckSize;
	}
	
	// overrides the toString method
	// prints every remaining card in the deck
	public String toString()
	{
	String strRepr = "";
	for(int i=0; i<deckSize; i++){
		strRepr += Deck[i].toString();
		strRepr += "\n".toString();
	}
	return strRepr;
}

public Object clone()
{
Deck newest = new Deck();
return newest;
}

public boolean equals(Object other)
{
if(other instanceof Deck){
Deck otherDeck = (Deck) other;
}return true;
}}
[code]

after looking at these three pieces of code i realize this wasn't my final product, there are some weird things going on like the deck class prints the deck size instead of returning it. but you can fix that if you want.

and another class called decktest 
this actually runs everything


[code]
public class deck_test
{
public static void main(String[] args){
Deck awesome = new Deck();
System.out.println("Card on top of deck");
awesome.getTop();
System.out.println("Deck Size");
awesome.getDeckSize();
System.out.println("Shuffling....");
awesome.shuffle();
System.out.println("Card on top of deck");
awesome.getTop();
System.out.println("Shuffling....");
awesome.shuffle();
System.out.println("Card on top of deck");
awesome.getTop();

}
}



after looking at this code i realize its a little bit older than i thought. maybe from my first semester. the methods print the statements instead of return them. rookie mistake that should be fixed, but it works so you can see how it works.

and finally a class called deck test
public class deck_test
{
public static void main(String[] args){
Deck awesome = new Deck();
System.out.println("Card on top of deck");
awesome.getTop();
System.out.println("Deck Size");
awesome.getDeckSize();
System.out.println("Shuffling....");
awesome.shuffle();
System.out.println("Card on top of deck");
awesome.getTop();
System.out.println("Shuffling....");
awesome.shuffle();
System.out.println("Card on top of deck");
awesome.getTop();

}
}


Was This Post Helpful? 0
  • +
  • -

#14 sumguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-February 08

Re: Random Card Class

Posted 13 February 2008 - 11:41 PM

My teacher wants me to possibly use for loop to tell what card is being chosen, rather then a table for users to find them selves. (see first post if confused)
Was This Post Helpful? 0
  • +
  • -

#15 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Random Card Class

Posted 14 February 2008 - 12:03 AM

then just do a for loop.

in pseudocode:

for( int i = 0; i < 20; i++){
call the method that shows the card on top.
call the method that shuffles it.
}
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2