Random Card Class

Create Card Class

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7016
  • View blog
  • Posts: 14,666
  • Joined: 16-October 07

Re: Random Card Class

Posted 14 February 2008 - 06:12 AM

The String getCardName(int cardIndex) I offered is stand alone. It can be used as you see fit. It's primary goal is to obey the rules of your orginal project. It's not intended to be a complete solution, rather a method that could be part of your solution.

Your program can be written many different ways. I know how I'd do it. However, it's ulitimately up to you how you make your program work.
Was This Post Helpful? 0
  • +
  • -

#17 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 14 February 2008 - 05:43 PM

I'm confused. Also, the code that Capty99 write...about the for loop.

Am i suppposed to call the random or what, because my code is based on numerics. So would i have to go back and type it all out?
Was This Post Helpful? 0
  • +
  • -

#18 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 15 February 2008 - 12:31 PM

If someone could help me, i would greatly appreciate it =)
Was This Post Helpful? 0
  • +
  • -

#19 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7016
  • View blog
  • Posts: 14,666
  • Joined: 16-October 07

Re: Random Card Class

Posted 15 February 2008 - 04:38 PM

capty99 has given you enough to get you there, I think. Ok, the code has some bugs, but nothing major.

Here's what I think is a simple skeleton of the problem. I've filled it what I already gave you and also added to the result that was eluded to. :P

There are three methods here that you MUST provide. All the answers are available in this thread, if you choose to look.

import java.util.Random;

class Deck {
	public static final int DeckSize = 54; // includes two jokers, standard deck
	public static final int BadCard = -1; // value returned if requested index out of range

	// constructor
	// the deck will be in order after this call
	// positions 0 and 53 are jokers
	public Deck() {
		// FILL THIS IN
	}
	
	//this puts the deck in a random order
	public void shuffle() {
		// FILL THIS IN
	}
	
	// Id is the actual value at this position
	// 0-52, some indicator of a bad value should be returned
	// returns -1 if out of range
	public int getCardId(int index) {
		// FILL THIS IN
	}
	
	
	// this is the card name
	// I offered code for this before
	// here we'll do it a little cleaner
	public String getCardName(int index) {
		int cardId = this.getCardId(index);
		if (cardId==Deck.BadCard) { return "BAD CARD"; }
		if (cardId==0) { return "Joker"; }
		cardId--;
		return getCardValue(cardId) + " of " + getCardSuit(cardId);
	}

	private String getCardValue(int cardId) {
		int val = cardId%13 + 1;
		if (val==1) { return "Ace"; }
		if (val==11) { return "Jack"; }
		if (val==12) { return "Queen"; }
		if (val==13) { return "King"; }
		return String.valueOf(val);
	}
		
	private String getCardSuit(int cardId) {
		int val = cardId/13;
		if (val==0) { return "Clubs"; }
		if (val==1) { return "Hearts"; }
		if (val==2) { return "Spades"; }
		if (val==3) { return "Diamonds"; }
		return "";
	}
}

public class CardMain {
	public static void main(String[] args){
		// make a new instance of class Deck
		Deck deck = new Deck();
		// check that our deck is loaded properly
		// for(int i=0; i<deck.DeckSize; i++) { System.out.println(i + ": " + deck.getCardName(i)); }

		// make it random
		deck.shuffle();
		System.out.println("Twenty Random Cards");
		// show the list
		for(int i=1; i<=20; i++) {
			System.out.println(i + ": " + deck.getCardName(i));
		}
	}
}




Good luck.
Was This Post Helpful? 0
  • +
  • -

#20 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 18 February 2008 - 11:47 PM

so wat exactly do i put in the part that says...

//FILL THIS IN

ahh w/e i'm never going to learn this.
Was This Post Helpful? 0
  • +
  • -

#21 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

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

Re: Random Card Class

Posted 19 February 2008 - 12:02 AM

sumguy, this site doesn't give out code, you have to do some work on your own.

we actually gave you entirely way too much to begin with.


you have a class, that class will contain methods for shuffling, and a constructor for building the deck up. etc.... you have to put those methods actions in how you would imagine them.

for example, a deck constructor will build a deck of 52 cards.
so for the shuffle method, you know you have a deck of 52 cards, in real life a shuffle is just randomizing the order of the cards. so figure out a way to do that with your shuffle method.

if your deck is an array of 52 cards, randomize the items in the array.
Was This Post Helpful? 0
  • +
  • -

#22 viXen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 08

Re: Random Card Class

Posted 23 February 2008 - 12:37 AM

So, I am doing a very similar assignment as the one that you all have posted. Mine is not quite as elaborate, as we are not supposed to use Strings or Arrays. Just a letter and a number from 1-13 and that part of the code is provided.

Here's my issue, I am trying to use a random number 1-4 for the suit, obvoiusly, but I can't make it stop using 0. How do I do that, or do I just need to loop it around to pick a number != 0.

We are also supposed to use get and set methods. I hate them. I could probably do this thing w/out them, but that would be of no help to me.

Here's what I have for this part - I realize I need to make a seperate class for it, but this is my process, I will get to that later.

public class Card
{
public static void main(String[] args)
{

Random suit = new Random(4);
final int LIMIT = 4;

System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
}
}

thanks a bundle
Was This Post Helpful? 0
  • +
  • -

#23 viXen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 08

Re: Random Card Class

Posted 23 February 2008 - 12:42 PM

View PostviXen, on 23 Feb, 2008 - 12:37 AM, said:

So, I am doing a very similar assignment as the one that you all have posted. Mine is not quite as elaborate, as we are not supposed to use Strings or Arrays. Just a letter and a number from 1-13 and that part of the code is provided.

Here's my issue, I am trying to use a random number 1-4 for the suit, obvoiusly, but I can't make it stop using 0. How do I do that, or do I just need to loop it around to pick a number != 0.

We are also supposed to use get and set methods. I hate them. I could probably do this thing w/out them, but that would be of no help to me.

Here's what I have for this part - I realize I need to make a seperate class for it, but this is my process, I will get to that later.

public class Card
{
public static void main(String[] args)
{

Random suit = new Random(4);
final int LIMIT = 4;

System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
System.out.println(suit.nextInt(LIMIT));
}
}

thanks a bundle

I got it figured out thx anyway.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2