I am having some difficulty proceeding with a java assignment for this week, the
ID: 3813094 • Letter: I
Question
I am having some difficulty proceeding with a java assignment for this week, the goal is to create a Coin object, a Coin Toss Simulator, and a 'FlipRace' a program where two coins are flipped until one lands on the heads side three times consecutively. More instructions are at the link so as to be precise about the requirements, link is below:
https://www.scribd.com/document/344412803/Prog-Assign-08-Instr
Here is my code so far for the Coin class which is only part of the assignment:
import java.util.Random;
public class Coin {
Random outcome = new Random();
/**
* Declaration of constants
*/
private int HEADS = 0;
private int TAILS = 1;
/**
* Declaration of outcome field 'sideUp' and counter variables for both
* heads and tails, 'headsCnt' and 'tailsCnt' respectively
*/
private int sideUp;
private int headsCnt;
private int tailsCnt;
/**
* This is the no-argument constructor method for the Coin object, it sets
* the initial side of the coin to face heads up, and declares that the
* initial total for heads outcomes and tails outcomes are both zero
*/
public Coin(){
sideUp = 0;
headsCnt = 0;
tailsCnt = 0;
}
public Coin(int outcome, int headsCounter, int tailsCounter){
sideUp = outcome;
headsCnt = headsCounter;
tailsCnt = tailsCounter;
}
public int toss(){
outcome = random.nextInt(2-1);
if(outcome == 0)
headsCnt++;
}
Explanation / Answer
CoinTossSimulator.java:
import java.util.Random;
class Coin {
/**
* Declaration of constants
*/
private int HEADS = 0;
private int TAILS = 1;
/**
* Declaration of outcome field 'sideUp' and counter variables for both
* heads and tails, 'headsCnt' and 'tailsCnt' respectively
*/
private int sideUp;
private int headsCnt;
private int tailsCnt;
/**
* This is the no-argument constructor method for the Coin object, it sets
* the initial side of the coin to face heads up, and declares that the
* initial total for heads outcomes and tails outcomes are both zero
*/
public Coin()
{
sideUp = 0;
headsCnt = 0;
tailsCnt = 0;
}
/**
* This is the parameterized constructor method for the Coin object, it checks
* the parameter passed and sets the side accordingly, and declares that the
* initial total for heads outcomes and tails outcomes are both zero
*/
public Coin(int side)
{
if (side == 0 || side == 1)
sideUp = side;
else
sideUp = 0;
headsCnt = 0;
tailsCnt = 0;
}
/**
* This method is to geneate a random side and also increment the counter value.
*/
public void toss()
{
sideUp = (new Random()).nextInt(2);
if(sideUp == 0)
headsCnt++;
else
tailsCnt++;
}
/**
* This method returns the integer value of which side is up.
*/
public int getSideUp()
{
return sideUp;
}
/**
* This method returns the integer amount from the side counter field based upon
* which side is requested via the parameter integer value.
*/
public int getSideCnt(int side)
{
if(side == 0)
return headsCnt;
else if (side == 1)
return tailsCnt;
else
return -1;
}
/**
* This method assigns the heads and tails sideUp counter variables
* back to a starting value of 0.
*/
public void resetSideCnts()
{
headsCnt = 0;
tailsCnt = 0;
}
/**
* This method returns the Boolean TRUE value if the sideUp is "heads"
* and FALSE otherwise.
*/
public boolean isHeads()
{
if(sideUp == 0)
return true;
else
return false;
}
/**
* This method returns a String representation of the Coin object.
*/
public String toString()
{
if(sideUp == 0)
return "Heads";
else
return "Tails";
}
}
// Driver code to test
public class CoinTossSimulator
{
public static void main(String[] args)
{
Coin coin = new Coin(1); // Creating object with parameterized constructor
// Coin coin = new Coin(); // Creating object with no-arg constructor
System.out.println("The side initially facing up: " + coin.toString()); // Printing intial side
for(int i = 0; i < 20; i++)
{
coin.toss(); // Making toss in loop
System.out.println("Toss: " + coin.toString()); // Printing coin side
}
System.out.println("Heads occurred: " + coin.getSideCnt(0)); // Printing total number of occurences of heads
System.out.println("Tails occurred: " + coin.getSideCnt(1)); // Printing total number of occurences of tails
}
}
FlipRace.java:
import java.util.Random;
class Coin {
/**
* Declaration of constants
*/
public int HEADS = 0;
public int TAILS = 1;
/**
* Declaration of outcome field 'sideUp' and counter variables for both
* heads and tails, 'headsCnt' and 'tailsCnt' respectively
*/
public int sideUp;
public int headsCnt;
public int tailsCnt;
/**
* This is the no-argument constructor method for the Coin object, it sets
* the initial side of the coin to face heads up, and declares that the
* initial total for heads outcomes and tails outcomes are both zero
*/
public Coin()
{
sideUp = 0;
headsCnt = 0;
tailsCnt = 0;
}
/**
* This is the parameterized constructor method for the Coin object, it checks
* the parameter passed and sets the side accordingly, and declares that the
* initial total for heads outcomes and tails outcomes are both zero
*/
public Coin(int side)
{
if (side == 0 || side == 1)
sideUp = side;
else
sideUp = 0;
headsCnt = 0;
tailsCnt = 0;
}
/**
* This method is to geneate a random side and also increment the counter value.
*/
public void toss()
{
sideUp = (new Random()).nextInt(2);
if(sideUp == 0)
headsCnt++;
else
tailsCnt++;
}
/**
* This method returns the integer value of which side is up.
*/
public int getSideUp()
{
return sideUp;
}
/**
* This method returns the integer amount from the side counter field based upon
* which side is requested via the parameter integer value.
*/
public int getSideCnt(int side)
{
if(side == 0)
return headsCnt;
else if (side == 1)
return tailsCnt;
else
return -1;
}
/**
* This method assigns the heads and tails sideUp counter variables
* back to a starting value of 0.
*/
public void resetSideCnts()
{
headsCnt = 0;
tailsCnt = 0;
}
/**
* This method returns the Boolean TRUE value if the sideUp is "heads"
* and FALSE otherwise.
*/
public boolean isHeads()
{
if(sideUp == 0)
return true;
else
return false;
}
/**
* This method returns a String representation of the Coin object.
*/
public String toString()
{
if(sideUp == 0)
return "Heads";
else
return "Tails";
}
}
// Driver code to test
public class FlipRace
{
public static void main(String[] args)
{
Coin Coin1 = new Coin(1); // Creating parameterized object for Coin 1
Coin Coin2 = new Coin(); // Creating No-args object for Coin 2
boolean Coin1Flip, Coin2Flip; // Variables to check continuous winning
//First time running to set variables
Coin1.toss();
Coin2.toss();
// Print the current toss value
System.out.println("Coin 1: " + Coin1.toString() + " Coin 2: " + Coin2.toString());
Coin1Flip = Coin1.isHeads();
Coin2Flip = Coin2.isHeads();
// Infinite loop till Either win or Tie
while(true)
{
//Toss The coins
Coin1.toss();
Coin2.toss();
// Print the current toss value
System.out.println("Coin 1: " + Coin1.toString() + " Coin 2: " + Coin2.toString());
// Check for contious heads or tails. If changed, reset
if(Coin1Flip != Coin1.isHeads())
{
Coin1.resetSideCnts();
if(Coin1Flip)
{
Coin1.tailsCnt = 1;
Coin1Flip = false;
}
else
{
Coin1.headsCnt = 1;
Coin1Flip = true;
}
}
if(Coin2Flip != Coin2.isHeads())
{
Coin2.resetSideCnts();
if(Coin2Flip)
{
Coin2.tailsCnt = 1;
Coin2Flip = false;
}
else
{
Coin2.headsCnt = 1;
Coin2Flip = true;
}
}
// Check for winning condition
if(Coin1.getSideCnt(0) == 3 || Coin2.getSideCnt(0) == 3)
break;
}
//Check the result and print
if (Coin1.getSideCnt(0) == 3 && Coin2.getSideCnt(0) == 3)
System.out.println("It's a TIE!");
else if (Coin1.getSideCnt(0) == 3)
System.out.println("Coin 1 Wins!");
else
System.out.println("Coin 2 Wins!");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.