Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Random playing cards

Thread view: 
hdquimby@yahoo.com - 09 Mar 2005 21:57 GMT
I am a newbie in a jam.  I need to write a program which creates five
random playing cards, using a custom class to create the cards (suit &
face value).  I assume I would create something similar to the dice
example below.  Can someone please help? Thanks in advance to any
sympathetic soul...

Driver class:

//********************************************************************
//  RollingDice.java       Author: Lewis/Loftus
//
//  Demonstrates the creation and use of a user-defined class.
//********************************************************************

public class RollingDice
{
  //-----------------------------------------------------------------
  //  Creates two Die objects and rolls them several times.
  //-----------------------------------------------------------------
  public static void main (String[] args)
  {
     Die die1, die2;
     int sum;

     die1 = new Die();
     die2 = new Die();

     die1.roll();
     die2.roll();
     System.out.println ("Die One: " + die1 + ", Die Two: " + die2);

     die1.roll();
     die2.setFaceValue(4);
     System.out.println ("Die One: " + die1 + ", Die Two: " + die2);

     sum = die1.getFaceValue() + die2.getFaceValue();
     System.out.println ("Sum: " + sum);

     sum = die1.roll() + die2.roll();
     System.out.println ("Die One: " + die1 + ", Die Two: " + die2);
     System.out.println ("New sum: " + sum);
  }
}

Custom Die class:

//********************************************************************
//  Die.java       Author: Lewis/Loftus
//
//  Represents one die (singular of dice) with faces showing values
//  between 1 and 6.
//********************************************************************

public class Die

{
  private final int MAX = 6;  // maximum face value

  private int faceValue;  // current value showing on the die

  //-----------------------------------------------------------------
  //  Constructor: Sets the initial face value.
  //-----------------------------------------------------------------

  public Die()

  {

     faceValue = 1;

  }

  //-----------------------------------------------------------------

  //  Rolls the die and returns the result.

  //-----------------------------------------------------------------

  public int roll()
  {

     faceValue = (int)(Math.random() * MAX) + 1;

     return faceValue;
  }

  //-----------------------------------------------------------------
  //  Face value mutator.
  //-----------------------------------------------------------------

  public void setFaceValue (int value)

  {

     faceValue = value;

  }

  //-----------------------------------------------------------------
  //  Face value accessor.
  //-----------------------------------------------------------------

  public int getFaceValue()

  {

     return faceValue;

  }

  //-----------------------------------------------------------------
  //  Returns a string representation of this die.
  //-----------------------------------------------------------------

  public String toString()

  {

     String result = Integer.toString(faceValue);

     return result;

  }

}
Bjorn Abelli - 09 Mar 2005 22:46 GMT
<hdquimby@yahoo.com> wrote...

> I am a newbie in a jam.  I need to write a program which creates five
> random playing cards, using a custom class to create the cards (suit &
> face value).  I assume I would create something similar to the dice
> example below.  Can someone please help? Thanks in advance to any
> sympathetic soul...

I would take a somewhat different approach for this.

- Create a custom class Card, with suit & face value,
  but I wouldn't let it be "random" as in the Die example,
  instead I would...

- ...create 52 *explicit* instances of Card, with the values...
  Well, I guess you know what the values of a complete deck
  would be... ;-)

- Put all cards into an ArrayList deck...

- Iterate five times...

  int item = Math.random() * deck.size();
  Card c = (Card) deck.remove(item);

  ...and put them into your "hand" (which could be an array or
  ArrayList, depending on how you would use the cards later on)

// Bjorn A
hdquimby@yahoo.com - 09 Mar 2005 23:43 GMT
> I would take a somewhat different approach for this.

Unfortunately, this is for a class and I must follow the given
protocol, although I don't need to create a class like the Die class,
but something like it.  I am in an Intro class.  Thanks though.
Bjorn Abelli - 10 Mar 2005 02:32 GMT
<hdquimby@yahoo.com> wrote...

>> I would take a somewhat different approach for this.
>
> Unfortunately, this is for a class and I must follow the given
> protocol, although I don't need to create a class like the Die class,
> but something like it.  I am in an Intro class.  Thanks though.

Well, you didn't spell out what "protocol" you're given, so my approach
could very well work for the stated assignment: To "create" random cards,
each with a suit and face value...

I think it would be unfortunate of your teacher to not accept a solution
that is more OO, as OO design always tries to "mimic" the real world objects
as the first option (which in my world is 52 cards in a deck, from which
cards are drawn). Unlike dices a Card has a fixed value...

Very well, if the teacher insist on cards acting like dices, you can simply
use the same pattern, but create random face and suit values instead of just
a face value.

You will stumble upon another problem though; to check that the randomized
Card isn't already "created", in which case you have to "redo" the card
again, as I believe that there will *still* be only one Ace of Spades...

// Bjorn A
Frank - 10 Mar 2005 10:42 GMT
> <hdquimby@yahoo.com> wrote...
>
[quoted text clipped - 12 lines]
> as the first option (which in my world is 52 cards in a deck, from which
> cards are drawn). Unlike dices a Card has a fixed value...

I'm thinking he wanted a 'more OO' approach.

Perhaps if you throw in a full-fledged Deck-class there, with methods
like shuffle, draw and cardsRemaining, encapsulating that Cards-array
you already mentioned.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.