Assigning values to objects.
I am having some trouble understanding how to assign values to objects. I am trying to assign it so that method handScore will return the four cards in the method. I want to make it so that Ace of hearts = 1, five of diamonds = 5, jack of clubs = 10, etc
Code :
package cards;
/**
*
* @author Logan
*/
class Card {
int suit, rank;
public Card () {
this.suit = 0; this.rank = 0;
}
public Card (int suit, int rank){
this.suit = suit; this.rank = rank;
}
public static void printCard (Card c) {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
}
public static void buildDeck () {
int index = 0;
Card[] deck = new Card [52];
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
deck[index] = new Card (suit, rank);
index++;
}
}
}
public static int handScore () {
Card threeOfClubs = new Card (0, 3);
Card aceOfHearts = new Card (3, 1);
Card fiveOfDiamonds = new Card (2, 5);
Card jackofHearts = new Card (3, 10);
return threeOfClubs+aceOfHearts+fiveOfDiamonds+jackOfHearts;
}
public static void main(String[] args) {
Card card = new Card (1, 11);
printCard (card);
}
}
Re: Assigning values to objects.
Adding two arbitrary objects together doesn't inherently make sense (what does a JLabel added to another JLabel mean?).
You need to extract the value you're interested in and operate on those values.
Here's a small example of how to access a field from an object:
Code java:
// adds the rank of two cards together and stores the result into rank_total
int rank_total = card1.rank + card2.rank;
Re: Assigning values to objects.
Would this be the correct application?
Code :
public int handScore () {
Card threeOfClubs = new Card (0, 3);
Card aceOfHearts = new Card (3, 1);
Card fiveOfDiamonds = new Card (2, 5);
Card jackofHearts = new Card (3, 10);
int total = threeOfClubs.rank + aceOfHearts.rank + ;
return total;
}
Re: Assigning values to objects.
ur first problem would be the constructor u declared with no parameter... instead of assigning the values like u did use ur second constructor and pass 0 as the parameters... what exactly is the point of ur handScore method? did u intend on it returning the same value every time it is called or did u want it to generate different results based on the user's input?
Re: Assigning values to objects.
Quote:
Originally Posted by
C++kingKnowledge
ur first problem would be the constructor u declared with no parameter... instead of assigning the values like u did use ur second constructor and pass 0 as the parameters...
What's wrong with this? Why is there a problem with his two constructors, one a default constructor which assigns default values to his fields, and the other a parameter accepting constructor that directly assigns the parameter values to the fields? They look good to me.
Quote:
what exactly is the point of ur handScore method? did u intend on it returning the same value every time it is called or did u want it to generate different results based on the user's input?
Now this is a good question since it speaks to his hard-coding the Card objects within the method making it a fairly useless method.
One word of unasked for advice: Please avoid unnecessary abbreviations in your posts here. I recommend this for several reasons:
- Programming is an exercise in precision. When you communicate here (or anywhere) about programming issues and questions, you want this communication to be as clear as possible to avoid any chance for ambiguity. I'd say at least half the answers here are requests for clarification. Let's avoid that.
- For many here, English is not their first (or second or third) language. It's hard enough for them to understand what people are posting here much less if it's couched in obscure and non-standard abbreviations.
- Extra letters don't cost anything here, so you might as well use them.
Re: Assigning values to objects.
It is just part of my assignment that I have to use the handScore method to figure out the "score" of the current hand of the beholder, I just wanted to test by using my own arrays and then modify it. I just wanted to get the 'value' which is defined in another method for example; ace of spaces = 1, king of clubs = 10, and 4 of spades = 4. In my handScore, I am trying to add and return a 'hand' of cards. I just need to know how to add these hands and return the total.