Given the diagram of HiLowGame Fix Dice.java // Dice.java import java.util.Random; public class Dice { // attributes pri

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Given the diagram of HiLowGame Fix Dice.java // Dice.java import java.util.Random; public class Dice { // attributes pri

Post by answerhappygod »

Given the diagram of HiLowGame
Fix Dice.java
Given The Diagram Of Hilowgame Fix Dice Java Dice Java Import Java Util Random Public Class Dice Attributes Pri 1
Given The Diagram Of Hilowgame Fix Dice Java Dice Java Import Java Util Random Public Class Dice Attributes Pri 1 (69.4 KiB) Viewed 40 times
Given The Diagram Of Hilowgame Fix Dice Java Dice Java Import Java Util Random Public Class Dice Attributes Pri 2
Given The Diagram Of Hilowgame Fix Dice Java Dice Java Import Java Util Random Public Class Dice Attributes Pri 2 (39.59 KiB) Viewed 40 times
// Dice.java
import java.util.Random;
public class Dice
{
// attributes
private int die[];
private Random random;
/**
* default constructor
*/
public Dice() {
die
= new int[2];
random = new Random();
// rolling for the first time, uncomment it if you dont need
roll();
}
/**
* constructor with one argument
*
* @param numDice
* number of dice
*/
public Dice(int numDice)
{
die
= new int[numDice];
random = new Random();
// rolling for the first time
roll();
}
/**
* method to roll all dice and return the sum total
*/
public int roll()
{
int total = 0;
for (int i = 0; i
< die.length; i++) {
int value = rollDie();
total += value;
die = value;
}
return total;
}
/**
* method to roll a single die, return a value between 1-6
*/
private int rollDie()
{
// below statement will return a value between 1 and 6
[inclusive]
return random.nextInt(6) + 1;
}
/**
* method to fetch the array containing dice values
*
* @return the die array
*/
public int[]
getDieValues() {
return die;
}
/**
* returns true if there are any double-values amongst the
die-values. This
* is confusing when there are more than two dice. So I'm
checking for
* adjacent doubles; that is this method returns true if two
adjacent dice
* have the same value
*/
public boolean hasDoubles()
{
for (int i = 0; i
< die.length - 1; i++) {
if (die == die[i + 1]) {
// this and next die has same value
return true;
}
}
return false;
}
@Override
public String toString() {
String data = "";
for (int i = 0; i
< die.length; i++) {
data += die + " ";
}
return data;
}
}
// DiceClient.java
public class DiceClient
{
public static void main(String[]
args) {
//creating and testing Dice objects
Dice dice1=new Dice();
System.out.println("Dice1: "+dice1);
System.out.println("Rolling...");
int sum=dice1.roll();
System.out.println("Sum = "+sum+", values:
"+dice1);
System.out.println("Has doubles:
"+dice1.hasDoubles());
Dice dice2=new Dice(5);
System.out.println("Dice2: "+dice2);
}
}
3. Given the diagram of HiLowGame: HILOWGame constructora+HiLowGame(ul: Tossable, t2: Tossable) playo: vold Dice -value: Int MIN_VALUE in = 1 {readonly -maxvalue: int constructors +Dice(maxValue:int) +gelValue(): Int setValue(value:int): vold getMaxValue: Inc 2 tossediterus interface Tossable From previous question OneBahtCoin Randombice -value: int constructor+RandomDice econstructors+OneBantcoin constructor+RandomDice(max Value: int +getName(): String HI+getName(): String -getResulto:int *getResultoint *getMinValue(): Int get MinValue(): Int +gotMaxValue(); int random: vold randomo: vold toStringo String toStringo: String In this problem, we will use the RandomDice and Tossable that were implemented in the previous question to make a HilowGame. Also, we will create another Tossable class named OneBahtcoin which represents a coin that could be tossable and used by the HilowGame. Your job is to create two more classes: OneBahtcoin and HilowGame. 3.1. Class OneBahtcoin has only one instance variable named value which keeps the current value of the coin. If the value = 0 meaning it is tail, otherwise it is head. It has the following methods. Constructor: it initials a random value for the coin (0 or 1 (AKA tail or head) getName() returns "One Baht coin" getResult() retums the current value getMinValue() retums 0 getMaxValue() retums random() changes the value randomly (0 or 1) toString() returns a String in the following format: its name (current state=Head/Tail(value)] For instance, if it is tail it returns: One Baht coin [current state=Tail(0)] Note that we intentionally make the One BahtCoin class very simple in order to use it to demonstrate how HilowGame could work with any Tossable classes (i.e., RandomDice or OneBahtCoin).
1 2 class RandomDice extends Dice implements Tossable{ public RandomDice(int maxValue) { super(maxValue); } @Override public String getName() { return getMaxValue()+"-side die"; } 3 4- 5 6 7 80 9 10 11 12 13 14 15 16 17 180 19 20 21 22 230 24 25 26 27 28 29 30 @Override public int getResult() { return getValue(); } @Override public int getMinValue() { return MIN_VALUE; } @Override public int getMaxValue() { return super.getMaxValue(); } 31 32 33 1340 35 36 37 38 } @Override public void random() { int random = (int) (Math.random()*(1+getMaxValue() -MIN_VALUE)) +MIN_VALUE; setValue(random); } @Override public String toString() { return getMaxValue()+"-side die value="+getValue(); } 39
Below is the description of the interface Tossable for any item that can be tossed and later will be used for playing in a HiLowGame (next question). public interface Tossable { public String getName(); public int getResult(); public int getMinvalue(); public int getMaxValue(); public void random(); Also, given a Dice class to represents a normal dice with face I to maxValue where maxValue must be greater than MIN_VALUE and less equal to 10. Note that Dice has a constant minimum value defined as MIN_VALUE with value = 1. At the construction, the current value of dice is set to MIN_VALUE and can be later set to other possible value later on using setValue method. import java.util. Random; public class Dice { private int value; // current value of the dice public static final int MIN_VALUE = 1; // its minimum possible value private int maxvalue; // its maximum possible value public Dice(int maxValue) { // if the parameter naxValue is NOT a possible value set it to 6 if (maxValue <= MIN_VALUE || maxValue > 10) maxvalue = 6; this.maxValue - maxValue; // set current dice value to MIN_VALUE which is 1 value - MIN_VALUE; } public int getValue() { return value; > public void setValue(int value) { W not changing its value if the given parameter (value) is not valid if (value > MIN_VALUE && value <= maxValue) this.value = value; else System.out.println("Dice doesn't change its value!"); )
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply