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: Tossing Coins for a Dollar Java Program?!?!

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

    Default Tossing Coins for a Dollar Java Program?!?!

    I've seem to have hit a roadblock. I'm stuck!

    The question is we have to write a class named Coin. The Coin class should have the following field:

    A string named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up.

    The Coin class should have the following methods:

    A no-arg constructor that randomly determines the side of the coin that is facing up ("heads" or "tails") and initializes the sideUp field accordingly.
    A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up ("heads" or "tails") and sets the sideUp field accordingly.
    A method named getSideUp that returns the value of the sideUp field.

    Now we have to create a game program using the Coin class. The program should have three instances of the Coin class: one representing a quarter; one representing a dime, and one representing a nickel.

    When the program begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. when a coin is tossed, the value of the coin is added to your balance if it lands heads-up. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that land tails-up. The game is over when you balance reaches on dollar or more. If your balance is exactly one dollar, you win the game. You lose if your balance exceeds one dollar.

    I think I've completed the Coin class but the game program is giving me problems. I got it to add the balances when they land on heads but it only does it one time. I'm trying to figure out how to input a loop but I haven't had any luck. Can I get some ideas?

    CoinClass1.JPGCoinClass2.JPGGameProgram1.JPGGameProgram2.JPGGameProgram3.JPG


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Tossing Coins for a Dollar Java Program?!?!

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers, including a few "house rules" that will help you feel more confident as a new user of this forum.

    "A picture is worth a thousand words," doesn't apply here. Post your code as text, using code or highlight tags as explained in the link above. Pictures of the code requires someone to type it all over again if they want to run the code in their own environment.

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

    Default Re: Tossing Coins for a Dollar Java Program?!?!

    import java.text.DecimalFormat;
    /**
     *
     * @author Yuridia
     */
    public class a1main 
    {
        public static void main(String[] args)
        {
            double balance = 0.00;
            final double winnings1 = .25;
            final double winnings2 = .10;
            final double winnings3 = .05;
     
            Coin coin = new Coin();
            Coin coin2 = new Coin();
            Coin coin3 = new Coin();
     
                coin.toss();
                coin2.toss();
                coin3.toss();
     
            System.out.printf("The quarter landed on " + coin.getSideUp() + ".\n");
            System.out.printf("The dime landed on " + coin2.getSideUp() + ".\n");
            System.out.printf("The nickel landed on " + coin3.getSideUp() + ".\n");
     
            {
             if ("heads".equals(coin.getSideUp()))
             {
                coin.addWinnings(winnings1);
                System.out.printf("Awarding " + winnings1 + " cents to balance.\n");
     
                System.out.printf("Your current balance is now " + coin.getBalance() + ".\n");
     
                if ("heads".equals(coin2.getSideUp()))
                {
                    coin.addWinnings(winnings2);
                    System.out.printf("Awarding " + winnings2 + " cents to balance.\n");
     
                    System.out.printf("Your current balance is now " + coin.getBalance() + ".\n");
     
                    if ("heads".equals(coin3.getSideUp()))
                    {
                        coin.addWinnings(winnings3);
                        System.out.printf("Awarding " + winnings3 + " cents to balance.\n");
     
                        System.out.printf("Your current balance is now " + coin.getBalance() + ".\n");
     
                    }
                }
     
             }}
     
            {
             if (balance > 1.00)
             {
             System.out.printf("Your ending balance is " + balance + " . You lose");
             }
     
             if (balance == 1.00)
             {
             System.out.printf("Your ending balance is $1.00. YOU WIN!!");
             }
             }}}


    --- Update ---

    lol! I don't think did that right!
    Last edited by GregBrannon; January 29th, 2014 at 06:54 PM. Reason: Fixed it. Notice the '/' to end the tag.

Similar Threads

  1. Add a dollar sign to a number
    By Tedstriker in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 4th, 2013, 08:31 PM
  2. Dollar Rate
    By kanna39 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 3rd, 2012, 05:50 AM
  3. [SOLVED] Least Coins Needed For Amount class
    By mwardjava92 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2011, 04:36 PM
  4. Trying to Give Coins For Change
    By bengregg in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 27th, 2011, 04:21 PM