Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

JAVA PROGRAMMING

Options
  • 06-11-2012 11:08am
    #1
    Registered Users Posts: 34


    Hi Guys,

    I'm having a bit of trouble with my programming assignment. I have to write a code to make a coin toss 3 times. If it shows up heads I need to print out True otherwise false. This is what I have so far.....

    CODE:

    import java.util.Random;
    import javax.swing.JOptionPane;

    public class Coin1
    {
    private String isHeads;
    private float result;

    public void Heads()
    {
    isHeads="true";
    showStatus();
    }

    public void Tails()
    {
    isHeads="false";
    showStatus();
    }
    private void showStatus()
    {
    JOptionPane.showMessageDialog(null, toString());
    }

    public String toString()
    {
    if (isHeads) = "true";
    {
    System.out.print("HEADS");
    }
    else
    System.out.print("TAILS");
    }
    }


    Any help at all will do. I just dont know where I'm going wrong.
    Thanks!
    Tagged:


Comments

  • Registered Users Posts: 1,501 ✭✭✭Delphi91


    mise1992 wrote: »
    Hi Guys,

    I'm having a bit of trouble with my programming assignment. I have to write a code to make a coin toss 3 times. If it shows up heads I need to print out True otherwise false...

    Not too sure if it's supposed to print out True if the coin comes up heads for all of the three tosses or just once. Either way, you need a counter in your program to keep tabs on the number of tosses. You also need to decide how to tell if the toss is heads or tails. You could set the random number generator to generate either a one or a two only - if it generates 1 then let that represent heads, otherwise it's tails.


  • Registered Users Posts: 5,141 ✭✭✭Yakuza


    I suggest a mod move this to the programming forum. EDIT - I see that's been done!

    I haven't programmed in Java for several years now, but I can see are several issues with the above code - is that a snippet or is it what you have in its entirety?

    If it's not a snippet, then there's no main() method, so the program won't even run.
    (if (isheads) = "true";
    
    shouldn't even compile in Java - there should be no ";" at the end of the if statement, the closing ) is too early and when testing for equality, you have to use "==" and not "="

    As a programming tip, you should use really use boolean instead of string variables for representing true/false states. This means you could write
    if (isheads)
    {
      //do something
    }
    else
    {
      //do something else
    }
    
    The code is more readable and makes it easier to work out what should be going on

    As Delphi mentions, your requirements are not clear - is it to print "true" once at the end if heads appears all 3 times, or to print true each time a dice is thrown (so it can happen 0, 1, 2 or 3 times)?

    If the latter, then here's some pseudocode for your main method (I don't think we're allowed do assignments directly):
    public static void main(String [ ] args)
    {
           //Declare a variable of class Random here, call it randomvar
    
           for(int i=0; i<3; i++) //a loop with 3 iteratations
           {
                 //arbitrarily saying if a random number between 0 and 1 
                 //is > 0.5, then we've got a head
    
                 if(randomvar.nextDouble() > .5)
                 {
                     //print out the "heads" message
                 }
           }
    }
    


  • Registered Users Posts: 1,996 ✭✭✭two wheels good


    I'm not going to try to remember java syntax - I'll only embarrass myself.

    The coinToss class
    - should not have public methods Heads(), Tails(). What are they for? There is no requirement for "calling" the toss. (Even so unlikely to want it here)
    - it needs a public method Toss() returning the result (heads, tails , I'd suggest)
    - it doesnt need a method getResult(). Otherwise the caller might get the result of the last toss not a new toss
    - I'd say the iteration of 3 is best done in the main() program or caller. THis allows the number of tosses to be changed easily.
    (In fact you could get some brownie points by prompting the user for the number of tosses)
    - Confine the class to dealing with one coin and one toss
    - and all the Random() stuff .. hide it away in the coinToss class


Advertisement