简体   繁体   中英

Coin Flip Java Program

Here is the Java code from Lewis/Loftus Book. I am having difficult in understanding the function of two method and how they are invoked. Hope you can elaborate it for me. I am a new to programming and still in learing phase. Below is the code from the book.

My learning is myCoin object is created when the new operation call Coin() construction. Invoking Coin() constructor invokes flip() method. Which then calculate the face. I guess the code myCoin.flip also does the same as described above. Is the myCoin.flip a redundant code here.

System.out.println(myCoin) print head or tail. How is this possible? Because myCoin is object variable that contain information about head/tail. It also have different method associated. How does it print only the face.

How does the codes isHead and toString work. In the main program toString method is not invoked and it print Tail. What is the function of boolean isHead() method. What it does.

import java.util.Random;

class Coin
{
   private final int HEADS = 0;
   private final int TAILS = 1;

   private int face;


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



   public class CoinFlip





   {
      //-----------------------------------------------------------------
    // Creates a Coin object, flips it, and prints the results.
    //-----------------------------------------------------------------
     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.");

      }
    }

The "face" variable is an instance variable for your Object. Each of your Coin objects will have a "face." The isHeads() checks the value of "face" and compares it to HEADS. "face" was assigned either a 0 or a 1 when it was created. If the method returns true, "face" stores the value of '1'. The toString() creates a String variable "faceName" and then checks the value of your "face" variable of your coin. If it's '1', faceName with be assigned the word "Heads" and vice versa. The method will return a String.

Try this after creating the object to print. Ex:

Coin myCoin = new Coin();
System.out.println(myCoin.toString());

I hope this helps!

System.out.println(myCoin)

Is actually

System.out.println(myCoin.toString())

Since your toString() is as follow:

    public String toString()
   {
      String faceName;

      if (face == HEADS)
     faceName = "Heads";
  else
     faceName = "Tails";

  return faceName;
   }

How does this work?

It returns the string "Heads" or "Tails" depending on whether face == HEADS or face != HEADS

What does isHead() do?

Lets look at your code

    public boolean isHeads ()
   {
     return (face == HEADS);
   }

So what this means is

is face == HEADS?

If yes return the boolean true //face == HEADS

Otherwise return false //face != HEADS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM