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

I am having trouble developing a java program. It is a coin class that i will us

ID: 3707275 • Letter: I

Question

I am having trouble developing a java program. It is a coin class that i will use in two different test programs. I will post the class requirements below. Thanks

Coin Class Requirements: This Coin class only has five fields of information. One data attribute indicates what side of the coin is currently facing up. Notice in the UML diagram that this data is an integer data type. This integer value will hold the values of 0 (zero) for “heads” or 1 (one) for “tails”. Two other data attributes are integer counter values that hold the amount of times a coin object’s head or tail side has appeared face up after a simulated toss. There are also two named constants, which hold the integer values that represents “heads” and “tails”. This class has two constructor methods and six general service methods. The functionality of these methods are the following: No-Arg Constructor Method: The no-arg constructor for this class will initialize the Coin object setting the counter fields to 0 and the sideUp field to a default value of 0 for “heads”. String Page 2 of 5 Parameterized Constructor Method: The class also has a parameterized constructor that sets the counter values to 0 and the sideUp to the integer value that is provided through the parameter. In this parameterized constructor, the parameter value needs to be validated to insure that it is either 0 for “heads” or 1 for “tails”. If it is not, then the sideUp value will be set to “heads” by default. toss() Method: The toss() method simulates the tossing of the coin by utilizing a Random object that produces a random integer between 0 and 1 (HINT: Random class nextInt(n-1) method in Chapter 04 Sec. 4.11). The random number of 0 represents “heads” and the random number of 1 represents “tails”. The result of the randomly generated coin face up value will need to be stored in the sideUp field. In addition, the appropriate side up counter field value will need to be incremented. isHeads() Method: This method returns the Boolean TRUE value if the sideUp is “heads” and FALSE otherwise. getSideUp() Method: This method returns the integer value stored in the sideUP field. getSideCnt() Method: This method returns the integer amount from the side counter field based upon which side is requested via the parameter integer value. For example: if the parameter value is 0 for “heads” then the heads counter field value is returned. The parameter value needs to be validated to be either 0 for “heads” or 1 for “tails”; otherwise, this method needs to return a -1 (negative 1). resetSideCnts() Method: This method assigns the heads and tails sideUp counter variables back to a starting value of 0. toString() Method: This method returns a String representation of the Coin object. If the Coin object has a sideUp value of 0 (zero) then the String “Heads” is returned; otherwise the String “Tails” is returned.

Explanation / Answer


Given below is the code for Coin class based on the description in the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

import java.util.Random;

public class Coin {
private int sideUp;
private int headsCnt, tailsCnt;
public static int HEADS = 0;
public static int TAILS = 1;

public Coin()
{
headsCnt = 0;
tailsCnt = 0;
sideUp = 0;
}

public Coin(int side)
{
headsCnt = 0;
tailsCnt = 0;
sideUp = side;
if(sideUp != HEADS && sideUp != TAILS)
sideUp = HEADS;
}


public void toss()
{
Random random = new Random();
sideUp = random.nextInt(2);
if(sideUp == HEADS)
headsCnt++;
else
tailsCnt++;
}

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

public int getSideCnt(int side)
{
if(side == HEADS)
return headsCnt;
else if(side == TAILS)
return tailsCnt;
else
return -1;
}

public void resetCnts()
{
headsCnt = 0;
tailsCnt = 0;
sideUp = HEADS;
}

public String toString()
{
if(isHeads())
return "Heads";
else
return "Tails";
}
}