-4

I'm doing a code that wants me to take this code and instead of storing as an int, store it as a boolean

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 of 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; 
    }
}

I have this but don't know what to change the bottom else statement to. It says its wrong.

public class Coin2 {

    private final boolean HEADS = false; 
    private final boolean TAILS = true;

    private boolean face;

    //------------------------------------------
    // Sets up the coin by flipping it initially
    //------------------------------------------
    public Coin2 () {
        flip(); 
    }

    //-------------------------------------------------
    // Flips the coin by randomly choosing a face value.
    //-------------------------------------------------
    public void flip() {
        face = (boolean) (Math.random() < 2); 
    }

    //-----------------------------------------------------
    //Returns true of the current face of the coin is heads
    //-----------------------------------------------------
    public boolean isHeads () {
        return (face == false);
    }

    //-----------------------------------------------
    //Returns the current face of the coin as a string
    //------------------------------------------------
    public String toString() {
        String faceName;
        if (face == false);
            faceName = "Heads";
        else
            faceName = "Tails"; 
            return faceName; 
    }
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43

2 Answers2

2

Looks like you have a semicolon that should not be there on the if statment

if (face == false);
        faceName = "Heads";
        else faceName = "Tails"; 
    return faceName; 

Should read

if (face == false)
    faceName = "Heads";
else 
    faceName = "Tails"; 
return faceName; 

(also formatted for readability)

Andy Joiner
  • 5,932
  • 3
  • 45
  • 72
0

You will get more interesting flip results if you make this change:

public void flip() {
    face = Math.random() < .5; 
}

Because as random()s javadoc says:

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

The (boolean) cast wasn't needed so I took it out.

This next one is a nasty bug:

if (face == false);
        faceName = "Heads";
        else faceName = "Tails"; 
    return faceName; 

A semicolon at the end of an if is such a pain to spot and debug that most programmers follow a style guideline that helps keep it from happening. It looks like this:

if (face == false) {
    faceName = "Heads";
}
else {
    faceName = "Tails"; 
}
return faceName; 

Always use the curly braces and it should stop this extra semicolon nightmare from happening again. Also be careful with indentation. Messing it up will mess with your brain.

candied_orange
  • 7,036
  • 2
  • 28
  • 62