• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class question

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a practice exam our comp sci teacher has given us in preperation for an up and comming test, I was wondering if this class I have written answers the question I am asked(constructive critisim only please):


Write a Card class that represents playing cards in a deck of cards. You need to represent the cards in a standard 52-card deck (no jokers). Include

a constructor Card(n) that can create each of the 52 possible cards
an instance method getSuit( ) that returns a string representing the card's suit
an instance method getFace( ) that returns a string representing the card's face value
an instance method toString( ) that returns values like "ace of spades" and "two of clubs."

Recall that the four suits in a deck of cards are spades, hearts, diamonds, clubs, and the thirteen face values are ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, and king. [Note: we are only asking you to define one class here � no inheritance or interfaces are needed.]
---------------------------------------------------------------------------

------------------------------------------------------------------------
-sorry im not sure how to clear up the way the arrays were copied in, i know they look messy.
Thanks, Kp

//JAM -- I edited this to include the [CODE] and [/CODE] tags -- that should improve the readibility of the arrays.
[ March 07, 2005: Message edited by: Joel McNary ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, from your description, you're not following directions. You're not providing a constructor that takes one int (I'm assuming "n" represents an int).
 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok I will fix that, I just wanted to be able to control witch card number and suit i got.

-Kp
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks pretty good. It certainly seems to meet the requirements. I've got some comment, but they are mostly nit-picky things:

1). You probably want to declare your arrays as static (and probably final...). That way, there's only one copy of them, rather than one copy per instance. If you created 52 cards, you would wind up with 52 Face arrays and 52 Suit arrays.

2). By convention, names of variables and methods start with lowercase letters -- except for static final variables, which are all caps with underscores. Thus, if you leave Face and Suit as instance variables, they should be named "face" and "suit". But if you make them static, they should be named "FACE" and "SUIT". (I'd probably name them "FACE_VALUES" and "SUIT_VALUES" myself, but that's just me. I like to type. )

3). "n" and "k" are nice, short variable names that are quite non-descriptive. I would have named them "suit" and "value" myself. And I would have named the parameters to the constructor "aSuit" and "aValue", to differentiate them from the suit and value in my object (not that you code currently has that problem, though...)

4). Your suits are inconsistant. "Heart" and "Spade" are singular, while "Diamonds" and "Clubs" are plural. I would have made them all plural. You also seem to have an extra suit -- "Spaids"

5). Double check. Some teachers/professors might take off for the fact that "two" was specified in the instructions, while you have "Deuce." If you go with "Deuce", why not use "Trey" instead of three? If they're really picky, you might be taken off for the fact that lowercase was specified, but you've used capitalized your values and suits. While I'm being picky, you're instructor may not be. But then again, s/he might. (I note that the specified constructor only seems to take one parameter. While you can write a constructor like that in this case, the two-parameter constructor makes more sense.

6). You may find it useful to create other static variabled, like


Then, when you create the Ace of Spades, the reader of your code will have no problem knowing which card you are creating:

This is not important for the function of the program, but it would help when reading the program of writing code that used the card class.
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, Kev P, welcome to JavaRanch! We strongly encourage the use of complete last names. (You can use an initial for a first name, but not the last...) Please read the JavaRanch naming policy and then Change your display name. You don't have to use your real name, of course (even though we prefer it), but it should be some sort of complete last name. Thanks!
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hadn't read James's post when I wrote my response, but I'd stick with the two-argument constructor. (Actually, I'd check with the prof to see if he will insist on the one-argument constructor or not....) The two argument constructor makes more sense and reduces the amount of obtuse conding that you would have to do.

For a one argument constructor, you would need either a big switch statement (generally frowned upon...) or some math involving the Math.ceil() function, dividing the parameter by 13.0 (note the ".0!") and taking modulus(13) of the parameter. Actually writing that method is an exercise left to the reader.

[EDIT]
Hmmm.... I think that a random suit would be a bad idea. That seems further from the requirements to me. Everytime you look at the suit, it could change! Pretty easy to get flushes that way.....
[ March 07, 2005: Message edited by: Joel McNary ]
 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Joel I sent you a PM.

-Kp
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So not to keep other readers in the dark, Kev's PM essentially stated that, while he liked the two-parameter constructor, he was going to use the single parameter constructor. So my response to him was to try to use the parameter in such a way that the suit and value could both be specified. Since we try to keep as much on the boards a possible, here is my response:


Well, try writing a parameter that encompases both value and suit. Say 1-52. Divide the parameter by 13.0 and take the ceiling of that -- there's your suit. (Math.ceil(1/13.0) == 1.0, Math.ceil(13/13.0) == 1.0, Math.ceil(14/13.0) == 2.0).

Then use the modulus operator to get the value. 1%13 == 1, 14%13 == 1 (Both aces), but 13%13 == 0! You would either need an if statement or some more obtuse math, like ((n-1)%13)+1, which would give you the value you seek.


So, a parameter of 37 would have a suit of Math.ceil(37/13.0), which is 3.0 (Diamonds), and a value of ((37-1)%13)+1, which is (36%13) + 1, which is 10 + 1, which is 11, which is a Jack. So 37 is the Jack of Diamonds.
[ March 07, 2005: Message edited by: Joel McNary ]
 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im trying to understand math.ceil but am having a little trouble, hers the overview of the math method:

ceil
public static double ceil(double a)Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer. Special cases:
If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
If the argument value is less than zero but greater than -1.0, then the result is negative zero.
Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).

Parameters:
a - a value.
Returns:
the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer.
-Kp
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get the same result using the integer division and modulus operators. You don't need Math.ceil(). I won't divulge how, since it seems like that's what your instructor wants you to figure out on your own. Otherwise, they wouldn't have told you to do that type of constructor. Good luck.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel McNary:
Double check. Some teachers/professors might take off for the fact that "two" was specified in the instructions, while you have "Deuce." If you go with "Deuce", why not use "Trey" instead of three? If they're really picky, you might be taken off for the fact that lowercase was specified, but you've used capitalized your values and suits.

While this may seem overly picky, keep in mind that the professor may be testing this using a program of their own that will simply mark wrong the line that says "Deuce" when it expects "two".

One of my professors that did this would simply give you a flat out zero without looking at your program if the output failed his formatting requirements. Everyone whined when he told them about this rule, and whined even more when several people got nailed for failing to head his warning, but no one ever goofed up an assignment due to wrong formatting again.

When you're dealing with computers processing I/O, you must specifically code any leniency you want to provide, and that's more work.

[ I know no one complained about Joel's comments -- I'm just being preemptive. ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I actually have a comment: stick to zero-based arrays. Everyone seeing an array will expect it to be zero-based, so it's best to keep it that way. You can still have your face value field store values in [1,13], but you can convert that to [0,12] when accessing the array element. The same applies to the suit array.
 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you please give a small example David, cause I understand the idea of having a zero based array but could you please show me how to impeliment that?

-Kp
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kev Peterson:
could you please give a small example David, cause I understand the idea of having a zero based array but could you please show me how to impeliment that?

Yes, that was a bit nebulous, since all arrays in Java are zero-based.

In your code you assign a bogus value to the first element of the array (element 0). Then, you use the face value [1,13] as an index into the array. My suggestion is to 1) remove the bogus first element entirely and 2) use the face value [1,13] in "some expression" that turns it into [0,12] when using it as an index into the array.

I leave it up to you to find an expression that does that (don't look too hard or you've missed it). Here's an arbitrary example, though.As with most arbitrary code, I'd never actually code it like this, but it's as close to hinting as I could come up with.
 
Kevin Peterson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, I belive this new code would work, fairly the same for both getSuit() and getFace() so ill just show one method:

public String getSuit(){
Random r = new Random();
int s = 1 +(r.nextInt(5)+1);// s can never = 0 and if
if(s == 5){ // it will cause the array to go
s--; // out of bounds so I just catch
} // this and subtract 1
String cardSuit = Suit[s];
return(cardSuit);
}

- I got rid of the null type at the two arrays zero positions, and this is my way of patching it up, could it be done more efficently.

-Kp

-Kp
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A note first about expressing ranges since I didn't mention it before (math-head making assumptions). In a range of numbers, you have the first number and the last number -- x,y -- and on either side of this you put a parenthesis to mean the range does not include the number or a square bracket to mean it does include the number.Okay, on with the show...

The expression "1 + r.nextInt(5) + 1" is in the range [2,6]. The 5 gets converted into a 4, so the resulting range is [2,4] + [6]. Thus, you still have a problem if you get 4 or 6 (ArrayIndexOutOfBoundsException) and your first two suits are ignored since s never equals 0 or 1.

Since nextInt(n) returns a value in [0,n), you can simply use nextInt(4) to get a number in [0,3], the bounds of your array.

If you have a field (say suit) that holds a number in [1,4], you can convert it to an appropriate index by using (suit - 1). That's what I was hinting at.

To summarize, drop the if block and change the initial expression toThis assumes that your array looks likeThis is in keeping with your original declarations, but I highly recommend taking Joel's advice into consideration.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may be helpful, this may not be helpful. But I know if one of my teachers gave me an exam like that you would want to replicate the card deck so it that it resembles a real card deck. You may want to add a boolean [] in there.

This may also be harmful since sometimes you shouldn't add stuff when your not supposed to. this is kinda modeled after the game of bridge but you get the idea, im not talking about the actual dealing, just the representation of a real deck.



Sorry for not sticking to the conventions, and yeah, sorry if this post was unneccessary.
[ March 09, 2005: Message edited by: Sean Magee ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic