Modify the existing Dice , Die , CrookedDie1 and CrookedDie2 classes as provided
ID: 3592975 • Letter: M
Question
Modify the existing Dice, Die, CrookedDie1 and CrookedDie2 classes as provided, so that they satisfy the described changes.
Add an AbstractDie class, with:
- private int field lastRoll,
- public getter int getLastRoll()
- A no-arg, do-nothing constructor
- A protected setter for its single field, so subclasses can change it
- An abstract method void roll()
- toString() that returns "lastRoll: " + value of lastRoll
- equals(Object obj) that returns true exactly when obj is "castable" to AbstractDie, and has the same value for getLastRoll()
Make Die a concrete subclass of AbstractDie. Implement its roll() in the usual "fair" way, and have its toString() return: "Fair Die - " + its parent's toString() value.
Make CrookedDie1 a concrete subclass of AbstractDie. Implement its roll() to always set the lastRoll field to 3, have its toString() return: "CrookedDie1 - " + its parent's toString() value.
Make CrookedDie2 a concrete subclass of AbstractDie. Implement its roll() to do the "circular" pattern 1,2,3,4,5,6,1..., starting with the value of its initial roll.
Change Dice's code so that all type references to Die are changed to AbstractDie. You'll still be calling new Die() in Dice(), so leave this constructor call unchanged.
The Classes:
public class Die
{
private int lastRoll;
public Die()
{
this.roll();
}
public int getLastRoll()
{
return this.lastRoll;
}
public void setLastRoll(int lastRoll)
{
this.lastRoll = lastRoll;
}
public void roll()
{
setLastRoll((int) (Math.random() * 6 + 1));
}
public String toString()
{
return "A Die object with lastRoll == " + this.getLastRoll();
}
}
public class Dice
{
private Die die1;
private Die die2;
private int lastRoll;
public Dice()
{
die1 = new Die();
die2 = new Die();
this.roll();
}
public Dice(Die d1, Die d2)
{
die1 = d1;
die2 = d2;
}
public void roll()
{
die1.roll();
die2.roll();
this.lastRoll = die1.getLastRoll() + die2.getLastRoll();
}
public int getLastRoll()
{
return this.lastRoll;
}
public String toString()
{
return "A Dice object, with die1 (" + die1 + "), die2 (" + die2 + ")";
}
}
public class CrookedDie1 extends Die
{
public CrookedDie1()
{
}
public int getLastRoll()
{
return 3;
}
public String toString()
{
return "A CrookedDie1 object, subclassing... " + super.toString();
}
}
public class CrookedDie2 extends Die
{
public CrookedDie2()
{
}
public int getLastRoll()
{
if (super.getLastRoll() == 6)
return 1;
else
return super.getLastRoll() + 1;
}
public void roll()
{
}
public String toString()
{
return "A CrookedDie2 object, subclassing..." + super.toString();
}
}
Explanation / Answer
AbstractDie.java
public abstract class AbstractDie {
private int lastRoll;
public int getLastRoll() {
return lastRoll;
}
public AbstractDie() {
}
protected void setLastRoll(int lastRoll)
{
this.lastRoll=lastRoll;
}
public abstract void roll();
@Override
public String toString() {
return "LastRoll :" + lastRoll;
}
public boolean equals(Object obj)
{
if(!(obj instanceof AbstractDie))
{
return false;
}
else
{
AbstractDie ad=(AbstractDie)obj;
if(this.lastRoll!=ad.getLastRoll())
{
return false;
}
else
return true;
}
}
}
__________________
Die.java
public class Die extends AbstractDie {
@Override
public void roll() {
setLastRoll((int) (Math.random() * 6 + 1));
}
@Override
public String toString() {
return "Fair Die - " + super.toString();
}
}
_________________
CrookedDie1.java
public class CrookedDie1 extends AbstractDie {
@Override
public void roll() {
setLastRoll(3);
}
@Override
public String toString() {
return "CrookedDie1 - "+super.toString();
}
}
________________
CrookedDie2.java
public class CrookedDie2 extends AbstractDie {
static int i=1;
@Override
public void roll() {
setLastRoll(i);
i++;
if(i==6)
{
i=1;
}
}
}
_________________
Dice.java
public class Dice {
private AbstractDie die1;
private AbstractDie die2;
private int lastRoll;
public Dice() {
die1 = new Die();
die2 = new Die();
this.roll();
}
public Dice(Die d1, Die d2) {
die1 = d1;
die2 = d2;
}
public void roll() {
die1.roll();
die2.roll();
this.lastRoll = die1.getLastRoll() + die2.getLastRoll();
}
public int getLastRoll() {
return this.lastRoll;
}
public String toString() {
return "A Dice object, with die1 (" + die1 + "), die2 (" + die2 + ")";
}
}
_________________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.