Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* This game is meant for two or more players. In the game, the * players take tu

ID: 3760782 • Letter: #

Question

* This game is meant for two or more players. In the game, the * players take turns flipping a coin. Before the coin is flipped, the * players should guess if the coin will land face up or face down. * If a player guesses correctly, then that player is awarded a point. If the player guess * incorrectly, than that player will lose a point. The first player to score five points wins. * * Write a program that simulates the game being played by two players. Use coin class to simulate * the coin and a player class to simulate the players. * */

The program language is Java and here is the coin class

package CoinDemo;

import java.util.Random;

class Coin {

    private String sideUp;

    /**
     * Default constructor
     */
    public Coin() {
        // initialize sideUp
        toss();
    }

    /**
     * This method will simulate the tossing of a coin. It should set the
     * sideUp field to either "heads" or "tails".
     * @return
     */
    public int toss() {

        Random rand = new Random();

        // Get a random value, 0 or 1.
        int value = rand.nextInt(2);

        if (value == 0) {
            this.sideUp = "Heads";
        } else {
            this.sideUp = "Tails";
        }
       return value;
    }

    /**
     *
     * @return The side of the coin facing up.
     */
    public String getSideUp() {
        return sideUp;
    }
}

Explanation / Answer

import java.util.*;

class Coin
{
   private String sideUp;

   public Coin()
   {

       int a=toss();
   }

   public int toss()
   {
       Random rand = new Random();

       int value = rand.nextInt(2);
       if (value == 0)
       {
           this.sideUp = "Heads";
       }
       else
       {
           this.sideUp = "Tails";
       }
       return value;
   }

   public String getSideUp()
   {
       return sideUp;
   }
}

class coin_main
{
   public static void main(String args[])
   {
       Scanner scan = new Scanner(System.in);      

       Coin o=new Coin();
       String s1;
       String s=o.getSideUp();
      
       System.out.println("Enter your Guess");
       s1= scan.nextLine();
       if(s.equals(s1))
       {
           System.out.println("U win");
       }
       else
       {
           System.out.println("U loss");
       }
   }
}