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 5 of 5

Thread: Exercise 105 in Java textbook

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Exercise 105 in Java textbook

    I don't know what the objective is for this exercise. I think it over and over and I don't get it. I think it's because I'm tired, but it would be nice if I could have some help with my understanding. Here is the exercise:

    By entering suitable code in place of the comment in the following main method, create a deck of cards:
     
     
        public static void main( String[] args )
        {
          Card[] deck = new Card[ 52 ];
          String[] suits = { "spades", "hearts", "diamonds", "clubs"  };
     
          int i;
     
    for ( i = 0 ; i < suits.length ; i++ )
     
    {
     
      for ( int k = 2 ; k <= 14 ; k++ ) 
     
      {
     
        // your code goes here
     
     
     
      }
     
    }&#65279;
     
          for ( Card card : deck )
            System.out.println( card.name() );
        }

    Once again, I don't know what the objective of the exercise is. I mean I know its about a deck of cards, but not what it is in terms of programming terms.

    All help appreciated, ghostheadx


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Exercise 105 in Java textbook

    Quote Originally Posted by ghostheadx View Post
    I don't know what the objective of the exercise is. I mean I know its about a deck of cards, but not what it is in terms of programming terms.
    First, you need a Card class. I don't know (it depends on the requirements) if this class is given to you or if this class is up to you. I suspect the latter, so you have to create a Card class. And what should it contain, according to you? Which are the necessary informations about a card?
    And based on the final for-each loop, the Card class should have a name() method that, I imagine, returns an informative string about the card object.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,079
    My Mood
    Mellow
    Thanks
    244
    Thanked 752 Times in 738 Posts

    Default Re: Exercise 105 in Java textbook

    While andbin is technically correct, I believe the assignment is to simply complete the for loop so that 52 Card objects will be added to the Deck object, deck. Not that hard. Now, if the completed code is supposed to run so that you can test it, then you'll need to add some more code around it, but that should be obvious.

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 105 in Java textbook

    They gave me a card class under "show program details":

     
      public class Card
      {
        private String mySuit;
        private int myValue;
     
        public Card( String suit, int value )
        {
          mySuit = suit;
          myValue = value;
        }
     
        public String name()
        {

     
          String[] cardNames = 
            {
              "Deuce", "Three", "Four", "Five",
              "Six", "Seven", "Eight", "Nine", "Ten",
              "Jack", "Queen", "King", "Ace"
            };
     
          return cardNames[ myValue - 2 ] + " of " + mySuit;
        }
      }
     
      public class MainClass
      {
        public static void main( String[] args )
        {
          Card[] deck = new Card[ 52 ];
          String[] suits = { "spades", "hearts", "diamonds", "clubs"  };
     
          int i;
          for ( i = 0 ; i < suits.length ; i++ )
          {
            for ( int k = 2 ; k <= 14 ; k++ ) 
            {
              // your code goes here
     
            }
          } 
     
          for ( Card card : deck )
            System.out.println( card.name() );
        }
      }

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Exercise 105 in Java textbook

    Quote Originally Posted by ghostheadx View Post
    They gave me a card class under "show program details":
    Ok, the Card class is fine. In the part your code goes here you need to instantiate a Card object and assign it to an element in the deck array.
    What are your doubts? Think: the Card constructor receives 2 informations. Do you have these informations in the your code goes here part?
    And do you have a valid index that can go from 0 to 51 (for deck array)? Think again: can you use directly i and/or k or do you need another index variable?
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Java exercise
    By nhiap6 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 2nd, 2013, 11:04 AM
  3. java Bluej Exercise
    By kiara4 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 27th, 2012, 04:43 PM
  4. [SOLVED] Code from textbook not working and I don't know why--Event-driven programming
    By BloomingNutria in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 9th, 2012, 09:58 PM
  5. JAVA exercise
    By gmilan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 02:30 PM