Question: :Define an abstract class called Die that has one protected int variable called face. It should also have a constructor that sets the face to
- :Define an abstract class called Die that has one protected int variable called face. It should also have
- a constructor that sets the face to 1,
- a getter and a setter for face,
- a toString method that returns the String version of face
- an equals method that returns true if the faces of two dice are same
- an abstract method called roll that returns an int.
- Define a class called NSidedDie that is derived from Die. This should have, (in addition to the parent methods and variable):
- an int variable called numSides (number of sides)
- a constructor that takes the number of sides as parameter, and sets face to 1 along with setting numSides.
- a constructor that does not have any parameters, but sets numSides to 6 and face to 1
- a roll method that sets face to a random int between 1 and numSides (both inclusive), and also returns it.
- a getter for numSides, but no setter. numSides can only be set through the constructor.
- a toString method that returns the number of sides and face. e.g. 10-sided die showing 7
- Define a driver class for testing the NSidedDie class. In the main method of this class:
- ask the user to input the number of dice, say 'n'
- create an n-sized array of Die objects (not NSidedDie objects)
- ask the user to enter number of sides for each die, one by one
- as the user enters this info, create appropriate NSidedDie objects and store their references in the array
- using a for or a for:each loop, print out the array. All of the faces should show 1
- using a for or a for:each loop, roll each NSidedDie.
- using a for or a for:each loop, print out the array. All of the faces should show appropriate different random values.
please answer in most simple way possible, thank you.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
