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

Thread: HELP! Constructor and "toString" method...

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP! Constructor and "toString" method...

    Instructor wants 1. The class is divided into multiple methods. 2. The toString method is included in the Card class to return the face and suit.
    3. Needs a constructor?

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

    Can someone please help me make these corrections?

    Thanks!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: HELP! Constructor and "toString" method...

    How to write constructor?

    Duplicate post!
    Improving the world one idiot at a time!

Similar Threads

  1. How do I use the repaint method to "undraw"? (I am a noob)
    By racecar333 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 31st, 2011, 06:40 PM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  4. "showMessageDialog" method in swing package
    By Delmi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 02:52 PM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM