Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Assigning values to objects.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

    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);
        }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:
    // adds the rank of two cards together and stores the result into rank_total
    int rank_total = card1.rank + card2.rank;

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Assigning values to objects.

    Would this be the correct application?
    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;
     
        }

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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?

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Assigning values to objects.

    Quote Originally Posted by C++kingKnowledge View Post
    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.

    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:

    1. 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.
    2. 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.
    3. Extra letters don't cost anything here, so you might as well use them.

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Replies: 3
    Last Post: December 2nd, 2011, 10:53 AM
  2. assigning problem
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2011, 05:45 PM
  3. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  4. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  5. Assigning variable to an Array
    By sridevi in forum Collections and Generics
    Replies: 3
    Last Post: August 10th, 2009, 10:58 PM