Class to Output the Suit of a Pack of Cards

classtooutputthesuitofapackofcards.java

// class to output the suit of a pack of cards

public class Card
{
 public int n, p;
 public String r;
 public String card=(" ");
 public String suit=(" ");
 public String cardsuit;

 //sets up and intialise the array with the values required

 String [] Cards={" Ace "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," 10 "," Jack "," Queen "," King "};

    // set up the suit of the card

    String [] Suit={" Hearts " , " Diamonds " , " Spades " , " Clubs "};

 // Constructors

 public Card() // default constructor with no parameters
 {
  setCard();
 }

 public Card(String r)
 {
  setRandomCard(r);
 }

 public Card(int n) // constructor with one parameter
 {
  setCard(n);
 }

 public Card(int n, int p) // overloaded constructor with two parameters
 {
  setCard(n,p);
 }

 // Methods

 // Set methods

 public void setCard()
 {
  for (n=0;n<Cards.length;n++)
  {
   card = (card + Cards[n]);
  }

  for (p=0;p<Suit.length;p++)
  {
   suit = (suit + Suit[p]);
  }

     cardsuit = card + "\n" + suit;
 }
 
 

 public void setCard(int n)
 {
  if(n>=0&&n<=12)
  {
   card = (Cards[n]);
  }
  else
  {
   System.out.println("You have entered an out of bounds number.\n Card number has been defaulted to 0 which is an Ace \n");
   n=0;
   card = (Cards[n]);
  }

  // Suit will be defaulted to Hearts, as only one number has been entered by the user
  suit = (Suit[0]);

  cardsuit = card + "of" + suit;

 }
 

 public void setCard(int n, int p)
 {
  if(n>=0&&n<=12)
  {
   card = (Cards[n]);
  }
  else
  {
   System.out.println("You have entered an out of bounds number.\n Card number has been defaulted to 0 which is an Ace \n");
   n=0;
   card = (Cards[n]);
  }

  if(p>=0&&p<=3)
  {
   suit = (Suit[p]);
  }
  else
  {
   System.out.println("You have entered an out of bounds number.\n The Suit has been defaulted to 0 which is Hearts \n");
   p=0;
   suit = (Suit[p]);
  }

  cardsuit = card + "of" + suit;
 }

 public void setRandomCard(String r)
 {
  int n = (int)(java.lang.Math.random() * 12);
  card = (Cards[n]);
  int p = (int)(java.lang.Math.random() * 3);
  suit = (Suit[p]);

  cardsuit = card +"of" + suit;
 }
 

 // Get Methods

 public String getCard()
 {
  return cardsuit;
 }
 

} // ends the class

Related:

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.