The goal of this assignment is to demonstrate inheritance, polymorphic behavior
ID: 3845964 • Letter: T
Question
The goal of this assignment is to demonstrate inheritance, polymorphic behavior and using multiple classes.
The assignment simulates rolling a single traditional six-sided with two different implementations: one that requires an explicit call to roll the die and a many-sided implementation.
1. Implement a version of the Die class that automatically rolls the die when created
a. Class name is DieVer2
b. The constructor is: public DieVer2()
c. The public methods are public void roll() and public int getNumber()
2. Implement a many-sided die class that takes the number of sides as a constructor parameter and whether to roll in the constructor.
a. Class name is ManySidedDi e
b. The constructor is: public ManySidedDie(int sides, boolean roll)
c. The public methods are public void roll() and public int getNumber()
To create an instance of a class, we use the keyword new
An abstract method must be implemented by the subclass ( polymorphic behavior)
We are introducing a new UML construct in this assignment called the Sequence Diagram
bdd [Package] Class Model [week04] week 04: Die MAX NUMBER nt 8 Tread On MIN NUMBER nt 1 readOnly. NO NUMBER nt 0 Tread On number nt random Random getNumbert) :int void week04: Die Ver2 MAN NUMBER nt 8 Tread On MIN NUMBER nt 1 freadOn number int random Random week 04: Many SidedDie maxNumber nt 6 MIN NUMBER nt 1 freedon NO NUMBER nt 0 Tread On number nt random Random getNumber() :int Many SidedDie(int, boolean) void test Abstract TestCase m stream PrintStream m testName String Abstract String) String getName0 results String run0 boolean nun Testo :boolean setPrint Stream(PrintStream) :void trace(String) void week 04 week04: TestCaseDie week 04 TestCaseDieMany {root TestCaseDieV2 m runs int 10 results) :String results) :String run Testo :boolean run results() :String Test0 boolean runTest boolean TestCaseDieV20 TestCase DieMany0 test: TestEngine m tests ArrayListExplanation / Answer
import java.util.*;
public abstract class Die {
int max_number=6;
int min_number=1;
int number=0;
Random random=new Random();
Die(){
}
public abstract int getnumber();
public abstract void roll();
}
class DieVer2 extends Die{
int max_number=6;
int min_number=1;
int number=0;
DieVer2(){
super();
}
public int getnumber(){
return this.number;
}
public void roll(){
this.number=random.nextInt(max_number)+min_number;
}
}
class ManySidedDie extends DieVer2{
int max_number=6;
int min_number=1;
int number=0;
boolean rolls;
int side;
ManySidedDie(int side,boolean rolls){
super();
this.rolls=rolls;
this.side=side;
}
public void roll(){
if(side==1){
this.max_number=1;
this.min_number=1;
}else if(side==2){
this.max_number=2;
this.min_number=1;
}else if(side==3){
this.max_number=3;
this.min_number=1;
}else if(side==4){
this.max_number=4;
this.min_number=1;
}else if(side==5){
this.max_number=5;
this.min_number=1;
}else if(side==6){
this.max_number=6;
this.min_number=1;
}
if(rolls==true){
this.number=random.nextInt(this.max_number)+this.min_number;
}else
System.out.println("cannot role:");
}
public int getnumber(){
return this.number;
}
}
allmain.java
import java.util.*;
public class allmain {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
ManySidedDie m=new ManySidedDie(3,true);
DieVer2 d=new DieVer2();
m.roll();
d.roll();
System.out.println("number is:"+m.getnumber());
System.out.println("number is:"+d.getnumber());
///dynamic method dispatch calling sublcass method from super class
//refrence
Die dm=new DieVer2();
dm.roll();
System.out.println("number is:"+dm.getnumber());
Die dm2=new ManySidedDie(4,false);
dm2.roll();
System.out.println("number is:"+dm2.getnumber());
}
}
output:
number is:1
number is:2
number is:4
cannot role:
number is:0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.