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: Coin

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

    Default Coin

    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 tis facing up (“heads” or “tails”) and sets the sideUp field accordingly.
    · A method named getSideUp that returns the vale of the sideUp

    The program demonstrates the Coin class. It should create an instance of the class and display the side that is initially facing up. Then use a loop to toss the coin 20 times. Each time the coin is tossed, display the side that is facing up. the program should keep count of the number of times heads is facing up and the number of times tails is facing up, and display those values after the loop finishes.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Coin

    Thank you for posting your assignment. It looks like a nice one. If you have a question that we can help you with, please feel free to ask.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Coin

    here is my code that i have so far. im not sure how to fix the problem
    import java.util.Random;

    public class Coin
    {
    private int sideUp;

    void Coin()
    {
    toss();
    }

    public void toss()
    {
    sideUp = (int)(Math.random());
    }

    public int getFace()
    {
    return sideUp;
    }

    public static void main(String[] args)
    {
    int heads, tails;
    int count = 0;
    do
    {
    count++;
    Coin.toss();
    if (sideUp <= 0.5)
    Coin = heads;
    else
    Coin = tails;

    System.out.println(heads + " " + tails);
    } while (count != 20);

    System.out.println("The coin showed heads " + count + " number of times.");
    }
    }

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Coin

    Quote Originally Posted by acl2011 View Post
    here is my code that i have so far. im not sure how to fix the problem...
    Please tell us more about this and any problems that you may be having. The better we can understand your current problems, the better we'll be able to help you.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Coin

    here are my compile errors

    ----jGRASP exec: javac -g Coin.java

    Coin.java:29: error: non-static method toss() cannot be referenced from a static context
    Coin.toss();
    ^
    Coin.java:30: error: non-static variable sideUp cannot be referenced from a static context
    if (sideUp <= 0.5)
    ^
    Coin.java:31: error: cannot find symbol
    Coin = heads;
    ^
    symbol: variable Coin
    location: class Coin
    Coin.java:33: error: cannot find symbol
    Coin = tails;
    ^
    symbol: variable Coin
    location: class Coin

Similar Threads

  1. [SOLVED] Please help me with the Coin flip rogram.
    By nikhilrudrangi in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2012, 10:10 PM
  2. Project Coin and java 7
    By Kerr in forum The Cafe
    Replies: 4
    Last Post: March 27th, 2011, 10:53 AM
  3. How to create a coin flip program
    By mrsaucy in forum Java Theory & Questions
    Replies: 1
    Last Post: December 20th, 2010, 05:31 AM
  4. Flipping a coin
    By jimboslice in forum Loops & Control Statements
    Replies: 2
    Last Post: October 18th, 2010, 11:13 AM
  5. Question about my coin toss program
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2010, 04:14 AM