• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Need help writing constructor

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!

I only just recently started to learn Java (barely a month ago). I have had absolutely no experience with Java or programming of any kind, so if I make no sense then please bear with me

Anyway I am trying to come up with a constructor for this solitaire game I have to create.

This is the relevant code pertaining to my question...

public class Card
{
public final static int SPADES = 0;
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;

public final static int ACE = 1;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;

public final static int RED = 0;
public final static int BLACK = 1;

public final static int CARDS_IN_SUIT = 13;

/**
* Constructor for objects of class Card.
*
* @param suit the suit of the card (one of SPADES, HEARTS, DIAMONDS, CLUBS)
* @param numPips the number of pips (1 .. 13).
*/
public Card(int suit, int numPips)
{

}



I know how to create a very simple constructor like...

public class Boomerrang
{
int x;
int y;

public pineapple(int reqdX, int reqdY)
{
x = reqdX;
y = reqdY;
}




But I am unsure how to construct the parameter for the deck of cards above.

Any help would be much appreciated
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a good next step would be to actually create some variables to hold the suit and value for each instance. The class variables you would initialize in your constructor.

It might be even a better idea to consider first what functionality this Card class will have. What information will it hold? What will it do? Once you have answered those questions then you'll know what your variables will be. Then easy as one two three you can code your constructor.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont under stand, if you want to create the card...

have two variables... like so

int S, NP;
public card (int suit, int numPips)
{
S = suit;
NP = numPips;
}
public int getCard()
{
return S + "\t" + NP;
}


or you could have an enumerated type like so

enum FaceCards = {Ace, Jack, Queen, King};
enum NumCards = {One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten};

and you could do like FaceCards.Ace;

but there is something like ace is o, jack is 1.

but i can't remember how to call them.

or you could do like, i think, FaceCards(0);

and instead of having FaceCards and NumCard separate, have one enum value like.

enum Cards = {Ace, one, two, three, ... , King};

and then have two random int generators for 0 - 12;

and then it would pick two cards randomly and all you would have to do

is say like

do
{
System.out.println("Do you want to play a game of BlackJack?(Y or N)");
String Answer = scan.nextLine();//you have to create a scanner.

if (Answer.equalsIgnoreCase("y");
{
// this is where you would create your two random generators.
// and then print out the following.

System.out.println(Cards.randomNum1);
System.out.println(Cards.randomNum2);

}

}while(!answer.equalsIgnoreCase("N");

now if you want to make the application have a hit me option...

you'll have to do that on your own lol, im tired.

i hope any of this helped..

-Justin-
 
Richard Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public class Card
{
public final static int SPADES = 0;
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;

public final static int ACE = 1;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;

public final static int RED = 0;
public final static int BLACK = 1;

public final static int CARDS_IN_SUIT = 13;

private final int cardSuit;
private final int cardPips;

public Card(int suit, int numPips)
{
cardSuit = suit;
cardPips = numPips;

}


public int getSuit()
{
return cardSuit;
}


public int getColour()
{
return 0;
}


public int getNumber()
{
return cardPips;
}

}



Thanks for the help (BTW, this is for a solitaire game, not blackjack )

I think I have made a bit of progress, but whenever I call the methods public int getNumber() and public int getSuit(), I end up getting some value that makes no sense, like "56" or something. (I havent yet managed to figure out how to get the method public int getColour() working.)

Does anyone know what I am doing wrong?

The getNumber method has to return the number of pips on the card (a number in the range 1 to 13 inclusive).
The getSuit method has to return the suit (one of SPADES, HEARTS, DIAMONDS, CLUBS).
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

whenever I call the methods public int getNumber() and public int getSuit(), I end up getting some value that makes no sense, like "56" or something



As written getNumber() should return whatever you constructed a Card with. I added a main() method like this:

I got the output - test: suit=0 pips=8

Perhaps whatever code you are using to construct the Card is reponsible for getting strange values like 56.

I havent yet managed to figure out how to get the method public int getColour() working.



Use getSuit() and an if-statement.
 
Richard Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying I was thinking I would probably need to use an if statement for the getColor method, but hadnt gotten around to it yet as Im still stuck on the getSuit and getNumber methods.

I have a skeletal implementation of the class Card, which represents a playing card. I need to implement the constructor and a few methods.

The skeleton of the class I am provided with is this bit


I added a couple instance variables to it

Then I did a constructor which is

The methods I am having a problem with are the getSuit and the getNumber ones.
The code I have written for them are


Im still lost in the dark as to why my getNumber and getSuit methods are returning insane values.

Is my constructor right? Do I need to add something else to my methods?

Thanks
 
Peter Brockway
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There doesn't seem to be anything wrong with your constructor. As I said I did add a simple driver to call the constructor, then called the getXxx() methods and got perfectly sensible values.

So the question really is: what code are you using to construct the card with? (I mean the code where you call the constructor.)
[ May 02, 2006: Message edited by: Peter Brockway ]
 
Richard Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So the question really is: what code are you using to construct the card with? (I mean the code where you call the constructor.)

[ May 02, 2006: Message edited by: Peter Brockway ][/QB]


Im not entirely sure I know what you mean sorry. Do you mean the values I input when I create an object of the class card?
 
Peter Brockway
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do you mean the values I input when I create an object of the class card?


Yes. You say you get strange values like "56" when you call the getNumber()
method. Post some code to show this.
 
Richard Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heh, ok its working now. Seems like a good nights sleep cleared my mind and I realised what I was doing wrong this morning.

Thanks for all your help
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic