Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By gcalvin

Thread: How to write constructor?

  1. #1
    federer is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default How to write constructor?

    My teach dinged me for not providing a constructor, also I was supposed use toString () ??? please help me revise this so that it meets their standards

    Java Code:
    import java.util.Random;
    
    public class Card
    {
    
    public String cardGeneration()
    {
    
    Random generator = new Random();
    
    int x; // random integer representing suit
    int y; // random integer for card value
    String mysuit, mynumber; //assigning names, these will be changed 
    int suits = 4, numbers = 13; //depending what card is picked
    
    
    x = generator.nextInt(suits); // random integer between 0 & 3 inclusive
    
    if (x == 0)
    mysuit = "Clubs";
    else if (x == 1)
    mysuit = "Diamonds";
    else if (x == 2)
    mysuit = "Hearts";
    else if (x == 3)
    mysuit = "Spades";
    else
    mysuit = "NA";
    
    y = generator.nextInt(numbers) + 1; // add 1 so that y is between 1 & 13 inclusive
    
    if (y == 1)
    mynumber = "Ace";
    else if (y == 2)
    mynumber = "Two";
    else if (y == 3)
    mynumber = "Three";
    else if (y == 4)
    mynumber = "Four";
    else if (y == 5)
    mynumber = "Five";
    else if (y == 6)
    mynumber = "Six";
    else if (y == 7)
    mynumber = "Seven";
    else if (y == 8)
    mynumber = "Eight";
    else if (y == 9)
    mynumber = "Nine";
    else if (y == 10)
    mynumber = "Ten";
    else if (y == 11)
    mynumber = "Jack";
    else if (y == 12)
    mynumber = "Queen";
    else if (y == 13)
    mynumber = "King";
    else
    mynumber = "NA";
    
    String result = mynumber + " of " + mysuit; 
    return result;
    
    
    } 
    
    
    }

    please help me! I am stupid :(
    Last edited by federer; 11-06-2011 at 07:53 PM.

  2. #2
    federer is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: How to write constructor?

    I tried doing this:

    String result = mynumber + " of " + mysuit;
    return toString ();

    It compiled but gave me an error :(

    How do I incorporate the "toString" method?

  3. #3
    federer is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: How to write constructor?

    cuz my instructor said to use the "toString" method and I used return result instead...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: How to write constructor?

    A few pointers:

    You should make mysuit and mynumber instance variables.
    All that code could be moved into the constructor or have the constructor call that method.
    Move the last two lines into the toString method.

    In future if you get errors post the full messages here. We don't read minds.

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to write constructor?

    You've created a method called cardGeneration that defines some values and returns a String representing those values. Basically, your instructor wants you to break that into two methods. The first is a constructor, which is a special method that is called when you create a new object. It has no return value and it must have the same name as your class.

    Java Code:
    public Card() {
        ...
    }
    As Junky said, instead of using local variables, you should change mynumber and mysuit to instance variables. Then when some other code does this:

    Java Code:
    Card myCard = new Card();
    The Card() method gets called and the object is initialized.

    And then you'll create a method called toString() that does the same thing as lines 61 and 62 of your code. Actually, every class inherits a toString() method from the Object class. Your instructor wants you to override it.

    Java Code:
    public String toString() {
        String result = mynumber + " of " + mysuit;
        return result;
    }
    Get in the habit of using standard Java naming conventions!

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default Re: How to write constructor?

    Think a little more about the nature of a Card. Does a real Card have a random suit and rank? Cards are often chosen at random from a deck, but a Card has a particular suit and rank from the moment it is printed until the moment it is destroyed. You should have a Card class and a Deck class. A typical Deck class might have a collection of Card objects -- one of each rank/suit combination. Then the Deck class should have a shuffle() method and a deal() method that returns a Card.
    pbrockway2 and kjkrum like this.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

Similar Threads

  1. Replies: 2
    Last Post: 02-24-2011, 02:14 AM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 08:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •