Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This game is meant for two or more players. In this game, the players take turns flipping a coin. Before the coin is flipped, players

This game is meant for two or more players. In this game, the players take turns flipping a coin. Before the coin is flipped, 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 a player guesses incorrectly, then that player will lose a point. The first player to score five points is the winner.

Write a new CoinPlayer class to simulate the players of this coin game. This CoinPlayer class should have fields for the player's name, guess and points as well as the appropriate constructor, accessor and mutator methods. The CoinPlayer class should also have a method called makeGuess which will randomly select a guess of heads or tails for the player (to be used to create a computer player when not enough human players are available).

Write a main method program that simulates the game being played by two players (one a human player, the other a computer player). Use the Coin class provided below.

import java.util.Random;

/** Coin class */

public class Coin { private String sideUp; // The side facing up /** The constructor randomly sets the side of the coin that is facing up: heads or tails. */ public Coin() { // Call the toss method to set the // initial state of sideUp. toss(); } /** The toss method simulates the tossing of the coin. After the method executes, the sideUp field will be randomly set to either "heads" or "tails". */ public void toss() { // Create a random object. Random rand = new Random(); // Get a random value, 0 or 1. int value = rand.nextInt(2); // Set the value of sideUp. // 0 = "heads" or 1 = "tails" if (value == 0) sideUp = "heads"; else sideUp = "tails"; } /** The getSideup method @return The side of the coin facing up. */ public String getSideUp() { return sideUp; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

l Discuss several ways to manage a surplus of human resources.

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago