Question: Write code in Java Objective: Use an abstract class to create a class hierarchy. Use an interface to create another class hierarchy. Instructions Create an
Write code in Java
Objective:
Use an abstract class to create a class hierarchy.
Use an interface to create another class hierarchy.
Instructions
- Create an abstract class AbstractDieRoller:
public abstract class AbstractDieRoller { public int rollDie(int sides) {return ThreadLocalRandom.current.nextInt(1, sides + 1); } } Now extend the AbstractDieRoller to create the D6DieRoller class, and the D20DieRoller class. The D6DieRoller class will have a rollDie() method that returns a random number as in the range of 1-6. (The lowest number possible is 1, and the max number possible is 6.) Likewise, the D20DieRollerClass will have a rollDie() method that returns a random number in the ranger 1-20.
Now, in the Assessment class (which will have the main method),
- Create a D6DieRoller object and call its rollDie() method 6 times.
- Print out each die roll.
- Create a D20DieRoller object, and call its dieRoller() method 10 times, printing out each
random number.
2. Now to use an interface:
public interface PickBestOf2RollsChooseable { int getHighestOf2Rolls(); // roll the die twice, return the highest of the 2 results } Create a D6ReRoller class and a D20ReRoller class. Here is how to declare the D6ReRoller class:
public class D6ReRoller extends D6DieRoller implements PickBestOf2RollsChooseable {....}
Now, in the Assessment class (which will have the main method),
- create a D6ReRoller object and call its getHighestOf2Rolls() method 6 times.
- Print out each die roll.
- Create a D20ReRoller object, and call its getHighestOf2Rolls () method 10 times, printing out each random number.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
