Hello,
I'm still learning java and recently I had an assignment for the card class. I was asked how to make a method where only 1 dice is rolled and I have to option to pick 1 out of the 3 dice. "When you roll a Dice object, three dice are rolled. How can you roll just one of the die in the Dice object? How can you specify which die to roll?"
" What parameter should this method accept? i.e. does any value need to be sent to the method? (Hint: yes, there should be one parameter and it should be an integer) Think about your answers to these questions, then write an instance method, rollOneDie, that allows the user to roll a single die."
Here is the code:
public class Dice { // declare the instance variables - do not add additional // variable private int die1; private int die2; private int die3; /** * Name: default constructor * Description: intialize the three instance * variables with values of 0. * */ public Dice() { die1 = 0; die2 = 0; die3 = 0; } /** * method name: toString method * description: create and return a String with * the values of the three instance variables * * @return String with the values of the * three instance variables */ public String toString() { String str = new String(die1 + " " + die2 + " " + die3); return str; } /** * method name: getdie1, getdie2, getdie3 * description: return the values stored in die1, die2, and die3 instance variables * @return The values stored in the instance variables die1, die 2, and die 3 */ public int getdie1() { return die1; } public int getdie2() { return die2; } public int getdie3() { return die3; } /** * method name: getDie1 * description: returns the integer value stored in the * die1 instance variable * * @return the integer value stored in the die1 instance * variable */ // public int getDie1() { //return die1; //} /** * method name: roll method * description: "roll" all three dice - give random integer * values between 1 and 6 to each die1, die2, and die3 */ public void roll() { Random rand = new Random(); // give die1 a random value between 1 and 6 die1 = rand.nextInt(6) + 1; // give die2 a random value between 1 and 6 die2 = rand.nextInt(6) + 1; // give die3 a random value between 1 and 6 die3 = rand.nextInt(6) + 1; } /** * Name; calcTotalRoll method Description:this method will *calculate the total of the three dice *@return the sum of the dice */ public int calcTotalRoll() { return (die1 + die2 + die3); } /** * Name:threeOfAKind method * Description: this method will determine if all three * of the dice have the same value * @return true if they have the same value, false otherwise */ public String threeOfAKind() { String s = new String(); s = "Not three of a kind."; if(die1 == die2 && die2 == die3 && die3 == die1) { s = "Three of a kind."; } return s; } /** *Name:findHighestDiemethod * Description: this method will determine which of the three dice have the * highest value if two die have the same value, the first occurrence of the value * should be used * @return the value of the highest die */ public int findHighestDie( ) { int highest; if ((die1 >= die2) && (die1 >= die3)) highest = die1; else if (die2 >= die3) // if code gets to this part, die1 is not highest so it’s either // die2 or die3 highest = die2; else highest = die3; return highest; } public void rollOneDie() { } }
--- Update ---
[code =Java]
public class Lab5App {
public static void main(String[] args) {
// declare myDice reference variable and instantiate
// a new Dice object
Dice myDice = new Dice();
// output the state of the object
System.out.println("After instantiation: " + myDice.toString());
// get and display the value of die1, die2, and die3
int val = myDice.getDie1();
System.out.println("\n" + "Value of die 1: " + val);
System.out.println("Value of die 2: " + val);
System.out.println("Value of die 3: " + val);
// roll the Dice (call the roll method)
myDice.roll();
// output the state of the object after rolling the Dice
System.out.println("\n" + "After rolling the dice: " + myDice);
//Output the sum of the three dice
int calcTotalRoll = myDice.calcTotalRoll();
System.out.println("\n" + "The roll total is " + calcTotalRoll);
// output whether the values of die1, die2, and die3 are the same value
String threeOfAKind = myDice.threeOfAKind();
System.out.println("\n" + threeOfAKind);
int findHighestDie = myDice.findHighestDie();
System.out.println("\n" + "The highest value of the three dice is " + findHighestDie );
System.out.println("\n" + "Rolling die 1 only: ");
System.out.println("Rolling die 2 only: ");
System.out.println("Rolling die 3 only: ");
}
}
[/code]