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

1. Practice using class methods 2. Practice using class variables 3. Practice ob

ID: 3595041 • Letter: 1

Question

1. Practice using class methods 2. Practice using class variables 3. Practice object-oriented programming 2 Tasks 1. Create a Java project called DiceRoll. 2. Create a class inside your project called Dice. 3. Create a class inside your project called Roll. 4. Create a class inside your project called Main. 5. Add the following two attributes to your Dice class : (a) private int value (b) private int sides 6. Modify the two attributes so that they are initialized to have value 1. 7. Add the following methods to your Dice class : /** * Assigns ’s’ to the ’sides’ attribute, to denote the * number of sides of the die */ public void setSides(int s){ } /** * Returns the current value of the die */ public int getValue(){ } /** * Assigns to the ’value’ attribute a random integer * from 1 to ’sides’ 8. 9. 10. 11. */ public void roll(){ } Modify the functions above to adhere to their descriptions. In your Roll class, add the following attributes : (a) private Dice dieOne (b) private Dice dieTwo In the Roll class, create a default constructor that initializes the two Dice members to six- sided dice. (a) Hint : you will have to use the setSides(int) function for the Dice since sides is a private attribute ! Add the following methods to the Roll class : /** * Re-randomizes each of the member Dice objects */ public void roll(){ } /** * Returns the sum of the values of the two Dice objects */ public int getTotal(){ } Modify the above functions to function as described In the Roll class, add a non-default constructor which takes two int arguments and uses them to set the number of sides to the Dice member objects. Add a main function to the Main class that (a) Creates two objects of type DiceRoll i. For the first, construct it with the default constructor. ii. For the second, use the non-default constructor to specify a pair of dice that has one 8-sided and one 10-sided. (b) Re-rolls each pair of dice until their totals are the same. (c) Counts the number of 1. Practice using class methods 2. Practice using class variables 3. Practice object-oriented programming 2 Tasks 1. Create a Java project called DiceRoll. 2. Create a class inside your project called Dice. 3. Create a class inside your project called Roll. 4. Create a class inside your project called Main. 5. Add the following two attributes to your Dice class : (a) private int value (b) private int sides 6. Modify the two attributes so that they are initialized to have value 1. 7. Add the following methods to your Dice class : /** * Assigns ’s’ to the ’sides’ attribute, to denote the * number of sides of the die */ public void setSides(int s){ } /** * Returns the current value of the die */ public int getValue(){ } /** * Assigns to the ’value’ attribute a random integer * from 1 to ’sides’ 8. 9. 10. 11. */ public void roll(){ } Modify the functions above to adhere to their descriptions. In your Roll class, add the following attributes : (a) private Dice dieOne (b) private Dice dieTwo In the Roll class, create a default constructor that initializes the two Dice members to six- sided dice. (a) Hint : you will have to use the setSides(int) function for the Dice since sides is a private attribute ! Add the following methods to the Roll class : /** * Re-randomizes each of the member Dice objects */ public void roll(){ } /** * Returns the sum of the values of the two Dice objects */ public int getTotal(){ } Modify the above functions to function as described In the Roll class, add a non-default constructor which takes two int arguments and uses them to set the number of sides to the Dice member objects. Add a main function to the Main class that (a) Creates two objects of type DiceRoll i. For the first, construct it with the default constructor. ii. For the second, use the non-default constructor to specify a pair of dice that has one 8-sided and one 10-sided. (b) Re-rolls each pair of dice until their totals are the same. (c) Counts the number of 1. Practice using class methods 2. Practice using class variables 3. Practice object-oriented programming 2 Tasks 1. Create a Java project called DiceRoll. 2. Create a class inside your project called Dice. 3. Create a class inside your project called Roll. 4. Create a class inside your project called Main. 5. Add the following two attributes to your Dice class : (a) private int value (b) private int sides 6. Modify the two attributes so that they are initialized to have value 1. 7. Add the following methods to your Dice class : /** * Assigns ’s’ to the ’sides’ attribute, to denote the * number of sides of the die */ public void setSides(int s){ } /** * Returns the current value of the die */ public int getValue(){ } /** * Assigns to the ’value’ attribute a random integer * from 1 to ’sides’ 8. 9. 10. 11. */ public void roll(){ } Modify the functions above to adhere to their descriptions. In your Roll class, add the following attributes : (a) private Dice dieOne (b) private Dice dieTwo In the Roll class, create a default constructor that initializes the two Dice members to six- sided dice. (a) Hint : you will have to use the setSides(int) function for the Dice since sides is a private attribute ! Add the following methods to the Roll class : /** * Re-randomizes each of the member Dice objects */ public void roll(){ } /** * Returns the sum of the values of the two Dice objects */ public int getTotal(){ } Modify the above functions to function as described In the Roll class, add a non-default constructor which takes two int arguments and uses them to set the number of sides to the Dice member objects. Add a main function to the Main class that (a) Creates two objects of type DiceRoll i. For the first, construct it with the default constructor. ii. For the second, use the non-default constructor to specify a pair of dice that has one 8-sided and one 10-sided. (b) Re-rolls each pair of dice until their totals are the same. (c) Counts the number of 8. 9. 10. 11. */ public void roll(){ } Modify the functions above to adhere to their descriptions. In your Roll class, add the following attributes : (a) private Dice dieOne (b) private Dice dieTwo In the Roll class, create a default constructor that initializes the two Dice members to six- sided dice. (a) Hint : you will have to use the setSides(int) function for the Dice since sides is a private attribute ! Add the following methods to the Roll class : /** * Re-randomizes each of the member Dice objects */ public void roll(){ } /** * Returns the sum of the values of the two Dice objects */ public int getTotal(){ } Modify the above functions to function as described In the Roll class, add a non-default constructor which takes two int arguments and uses them to set the number of sides to the Dice member objects. Add a main function to the Main class that (a) Creates two objects of type DiceRoll i. For the first, construct it with the default constructor. ii. For the second, use the non-default constructor to specify a pair of dice that has one 8-sided and one 10-sided. (b) Re-rolls each pair of dice until their totals are the same. (c) Counts the number of

Explanation / Answer

In this answer, as you requested, I have created three classes Dice.java Roll.java and Main.java. In the Dice class, two integer variables value and sides are defined. A method to set the number of sides setSides(int s) and a method to fetch the value of value variable getValue() also defined. The roll() method will assign a random integer between 1 and the number of sides, using a Random object. In the Roll class, two Dice objects are created , appropriate constuctors are defined according to the requirements. The roll() in Roll class invokes the roll() of Dice objects. getTotal() method is defined to return the sum of values of both Dice. In the Main class, two Roll objects diceroll1 and diceroll2 are defined, and rolled again and again until both returned the same total; with the help of a while loop. Finally, the number of times looped to get both totals same is printed on the screen along with the total of final roll. You can find more details from the comments included.

//Dice.java file

import java.util.Random;

public class Dice {

      private int value=1; /*creating an integer variable value; initializing with 1*/

      private int side=1; /*creating an integer variable value; initializing with 1*/

      public void setSides(int s){/*method to set the sides of the dice*/

            side=s;

    }

      public int getValue(){ /*method to return the value */

            return value;

    }

      public void roll(){ /*method to roll the dice */

            Random random=new Random();

            value=random.nextInt(side-1)+1; /*assigning a random value to the value variable (between 1 and side)*/

    }

}

//Roll.java file

public class Roll {

      private Dice dieOne;

      private Dice dieTwo;

      public Roll() {

            dieOne=new Dice(); /*creating two Dice objects and setting their sides=6*/

            dieOne.setSides(6);

            dieTwo=new Dice();

            dieTwo.setSides(6);

      }

      public Roll(int s1,int s2){

            dieOne=new Dice(); /*creating two Dice objects and setting their sides as s1 and s2 respectively*/

            dieOne.setSides(s1);

            dieTwo=new Dice();

            dieTwo.setSides(s2);

      }

      public void roll(){ /*this method will roll the two dice*/

            dieOne.roll(); /*rolling dice to assign a random value */

            dieTwo.roll();

    }

      public int getTotal(){ /*method to return the sum of values of two dice*/

            return dieOne.getValue()+dieTwo.getValue();

    }

}

//Main.java file

public class Main {

      public static void main(String[] args) {

            Roll diceroll1 = new Roll();/*

                                                      * creating a new Roll object using default

                                                      * constructor

                                                      */

            Roll diceroll2 = new Roll(8, 10);/*

                                                            * creating a new Roll object using

                                                            * parameterized constructor; inputting

                                                            * each dice sides as 8 and 10

                                                            * respectively

                                                            */

            System.out.println("Rolling both dices until their totals are same");

            diceroll1.roll();

            diceroll2.roll();

            int counter=1; /*to store the number of times rolled*/

            while(diceroll1.getTotal()!=diceroll2.getTotal()){/*loops until both totals are same*/

                  diceroll1.roll();

                  diceroll2.roll();

                  counter++; /*incrementing the counter*/

            }

            System.out.println("Both dice objects returned the same total: "+diceroll1.getTotal()+" after rolling "+counter+ " times");

      }

}

//Output

Rolling both dices until their totals are same

Both dice objects returned the same total: 6 after rolling 13 times