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: Comparing two coinflips, in JFrame, lots of problems need advice!!!

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comparing two coinflips, in JFrame, lots of problems need advice!!!

    Hello all, thanks for coming!

    I'm having tremendous issues today with making a program to compare the results of two flipped coins. Users are to be prompted for a number of coinflips, and the program must compare the results of each flip. Heads always wins, so if P1 (player 1) is heads and P2 is tails then P1 wins. If P1 is Heads and P2 is Heads, then it would output a tie. Also, I have to keep a going track on the number of wins for each player and output this number after all coin flips are done.

    Right now I can't figure out how to compare the outputs of the two coinflips. I have them returned as a string value. There's only so much I can explain with words, but right now I'm really confused as to how I compare these coinflips, and then output them to the Jframe. Any and all help or advice is appreciated!!

    package coinflip;
     
    public class Coin
    {
       private final int HEADS = 0;
       private final int TAILS = 1;
     
       private int face;
     
       //-----------------------------------------------------------------
       //  Sets up the coin by flipping it initially.
       //-----------------------------------------------------------------
       public Coin ()
       {
          flip();
       }
     
       //-----------------------------------------------------------------
       //  Flips the coin by randomly choosing a face value.
       //-----------------------------------------------------------------
       public void flip ()
       {
          face = (int) (Math.random() * 2);
       }
     
       //-----------------------------------------------------------------
       //  Returns true if the current face of the coin is heads.
       //-----------------------------------------------------------------
       public boolean isHeads ()
       {
          return (face == HEADS);
       }
     
       //-----------------------------------------------------------------
       //  Returns the current face of the coin as a string.
       //-----------------------------------------------------------------
       public String toString()
       {
          String faceName;
     
          if (face == HEADS)
             faceName = "Heads";
          else
             faceName = "Tails";
     
          return faceName;
       }
    }

    package coinflip;
     
    public class CoinFlip
    {
       //-----------------------------------------------------------------
       //  Creates first coin object, and flips it. 
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          Coin myCoin = new Coin();
     
          myCoin.flip();
     
          System.out.println (myCoin);
     
          if (myCoin.isHeads()) {
    	   System.out.println ("You win.");
           }
          else {
    	   System.out.println ("Better luck next time.");
           }
       }
     
       //creates second coin object , and flips it. 
       {
           Coin newCoin = new Coin ();
     
           newCoin.flip();
     
           System.out.println (newCoin);
     
       }
    }

    package coinflip;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class CoinFlipPanel extends JPanel
    {
     
        private JButton flip;
        private JLabel resultone, resulttwo, instructions;
        private JTextField input; // number of times coins are flipped, and compared
     
     
        //-----------------------------------------------------------------
        //  Constructor: Sets up the GUI.
        //-----------------------------------------------------------------
        public CoinFlipPanel ()
        {
     
           instructions = new JLabel ("Enter the number of coinflips.");
     
            input = new JTextField(5); //input for number of times to flip
            input.setBounds(100,20,100,20); //input for number of times to flip
     
    	flip = new JButton ("Flip both coins!"); //flips both coins
    	flip.addActionListener (new CoinFlipPanel.ButtonListener());
     
    	resultone = new JLabel ("Player 1 coin:" );
            resulttwo = new JLabel ("Player 2 coin:" );
    	setPreferredSize (new Dimension(300, 40));
    	setBackground (Color.cyan);
        }
     
        //*****************************************************************
        //  Represents a listener for button push (action) events.
        //*****************************************************************
        private class ButtonListener implements ActionListener
        {
    	//--------------------------------------------------------------
    	//  Updates the counter and label when the button is pushed.
    	//--------------------------------------------------------------
    	@Override
    	public void actionPerformed (ActionEvent event)
    	{
     
    	}
        }
    }
     
    /// if coin 1 < coin 2, player 1 wins. 
    // if coin 1 = coin 2, play 1 ties.
    // if coin 1 > coin 2, player 1 loses.
    // Heads = 0 
    // tai;s = 1
    Thanks for reading. ANY advice is appreciated!!! also pls let me know if anything is unclear.
    Last edited by mindfcked; October 20th, 2012 at 02:40 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Comparing two coinflips, in JFrame, lots of problems need advice!!!

    Can I get any feedback at all? How to make my post more user friendly so people will respond? something? haha

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Comparing two coinflips, in JFrame, lots of problems need advice!!!

    bumpbumpbump

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Comparing two coinflips, in JFrame, lots of problems need advice!!!

    Please don't bump your posts, especially when the post is only a few hours old - people are not on here 24/7, and doing so is inconsiderate to everyone else here asking questions whose question is as important as yours. Doing so could also make contributors think you have already received help, thus they might not even view your post. I would also recommend reading the links in my signature entitled Getting Help and SSCCE, in other words clarify your post to ask a specific question, which is much easier to respond to

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Comparing two coinflips, in JFrame, lots of problems need advice!!!

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/64085-comparing-two-coinflips-jframe-lots-problems-need-advice.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Question. Lots of Maths!
    By djl1990 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 28th, 2011, 10:57 AM
  2. Comparing Two Dates
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 27th, 2011, 02:09 PM
  3. Replies: 1
    Last Post: June 17th, 2011, 06:01 PM
  4. Comparing Strings?
    By wandertheverse in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 10:32 PM
  5. Problems with focussing JFrame
    By ozymann in forum AWT / Java Swing
    Replies: 3
    Last Post: June 30th, 2010, 12:40 PM