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: Dice Game Program toString() method help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dice Game Program toString() method help

    I was working on this program based off of some college CS courses assignments just to learn and I have it all working except one thing I can't figure out. The assignment says to create a toString() method in the Die class that will return the numbers that came up when the dice was rolled twice. I know quite a bit of java and this is just bugging me that I don't know how to properly do this. Please help me out.

    For example, the output should look like: "You rolled a six and a four".


    Die class:

    public class Die {
     
        private int die;
        Random dice;
     
        public Die() {
     
            this.die = 0;
     
            dice = new Random();
     
        }
     
        public int roll() {
     
            die = dice.nextInt(6) + 1;
     
            return die;
     
        }
     
        public boolean equals(int die, int die2) {
     
            if (die == die2) {
                return true;
            } else {
                return false;
            }
     
        }
     
     
    }


    Main Method:

    public class DoublesGame {
     
        public static void main(String[] args) {
     
            double value = 10.00;
            double bet = 0;
            Currency usd = Currency.getInstance("USD");
            NumberFormat format = NumberFormat.getCurrencyInstance(java.util.Locale.US);
            format.setCurrency(usd);
     
            Die obj = new Die();
            Scanner ui = new Scanner(System.in);
     
            while (value > 0) {
     
                System.out.println("You have " + format.format(value) + " " + usd.getCurrencyCode());
                System.out.println("How much would you like to bet? ");
                bet = ui.nextDouble();
     
     
                if (obj.equals(obj.roll(), obj.roll()) == true) {
                    System.out.println("You win " + bet * 2);
                    value += bet*2;
                } else {
                    System.out.println("You lose " + bet);
                    value -= bet;
                }
     
            }
     
            System.out.println("Better Luck Next Time, Jack!");
     
     
     
        }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Dice Game Program toString() method help

    Quote Originally Posted by JonSnow View Post
    ... toString() method in the Die class that will return the numbers that came up when the dice was rolled twice....
    I'm not sure what kind of dice game you have created that is based on tossing a single die twice and paying off 2-to-1 if you get a "doubles" (first toss equal to second toss). I would like to be the House for this game (since the odds are 6-to-1 against the player and the payoff is only 2-to-1).

    Anyhow...

    Your Die class consists of a single die, not two. If you want a toString() method for the class that reports results for two tosses, there must be a way (in the class) to keep track of two tosses.

    Philosophically, I dislike having a class named "Die" that has two "die" members (since the plural of die is dice), but for purposes of building on what you already have, maybe you could have two members, die1 and die2.

    (Or maybe you could create a new class named Dice that has two Die members that you have already defined.)

    Either way, maybe you could have a "roll2" method that gets two random numbers: One sets the value for die1 and the other for die2. This method could return the sum of the two dice values so that the game can be played.

    Then the toString() method could create an output String based on the values of die1 and die2.

    Then read up on how craps is played and flesh out your game to take into account snake-eyes, boxcars, craps, etc. Or you could come by my place and I'll show you how.. We can use my dice.

    Bring cash. No checks or credit cards, please.



    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dice Game Program toString() method help

    @Zaphod-

    I could not agree more this "game" is terrible as far as an actual game goes. I was following an assignment for a CS class CS 110: Program 7 Assignment for Programming Fundamentals 1 and there is the link if you don't believe me..lol And I thought of creating two roll methods, but I wanted to follow the assignment word for word. It just intrigued me that the professor thought it was possible to print " You have rolled a six and four" with the current design the way it is. Thanks for the good advice on craps, I have no doubt you are right.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Dice Game Program toString() method help

    Well the simplest way would be to have a switch/case statement for each number (1-6). If the die is a 6, then it assigns the string "six" to an output variable.

    To avoid confusion, I wouldn't call the method toString(), since this is already a Java method in the API.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Dice Game Program toString() method help

    Quote Originally Posted by JonSnow View Post
    ... It just intrigued me that the professor thought it was possible to print " You have rolled a six and four"
    But the toString() method in the assignment just returns the String "one", "two", "three", "four", "five", or "six" depending on the integer value of the die 1, 2, 3 4, 5, 6. It does not keep track of two tosses of the die and report both values.

    Why not just make an array of Strings containing {"one", "two",...,"six"} and have the toString() method return one of those elements depending on the value of the die?

    It seems to me (because of the requirement of the equals() method) that your game will have two different instances of Die objects. In fact, since you have now posted a link to the assignment, I see the following:
    "The DoublesGame main method uses two Die objects and uses the roll, equals, and toString methods correctly."
    Toss them both using each object's roll() method. Craft a message using the toString() method of the two Die objects, and then compare the objects for equality to determine whether this play is a winner.

    Bottom line: Read the entire assignment before starting to write code. Make sure you understand the assignment. Use any helpful hints that were given.

    Post-bottom-line comment (if that's allowed):
    The reason that I jumped on craps as the game is that whenever anyone says "dice game" to me, I automatically think "Craps! Let's play!" The Doubles game of your assignment would be a fair game if the payoff were 6-to-1 rather than 2-to-1. You could probably make a lot of money on the Street by offering 4-to-1 or 5-to 1-odds. (Assuming, of course, that the other players were idiots. That's what most street games and all casino games assume: The players are idiots.)

    I am always disappointed when educators create assignments like this that could actually be educational (by making it a fair game and maybe even explaining why the odds are what they are) but come up with an exercise in programming that has no real pedagogical payoff beyond the programming language aspects.



    Cheers!

    Z

Similar Threads

  1. Help with 3 guess Dice game
    By tabmanmatt in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2012, 07:42 PM
  2. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 02:25 AM
  3. Need to write a public instance method for a simple dice program
    By akira25 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 9th, 2012, 04:46 AM
  4. Leap Year/Calendar Program. toString() method error.
    By jerryg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 20th, 2012, 06:59 PM
  5. Dice Game help and Arrays
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 10:08 PM