In java please 1. add an instance method that determines if both dice are even.
ID: 3879408 • Letter: I
Question
In java please
1. add an instance method that determines if both dice are even.
2.add an instance method that determines if both dice are odd.
k You must not change the declaration of any method. import stdlib.StdOut; import stdlib.StdRandom; public class PairOfDice t private int diel, die2; public PairOfDice() roll); public void roll) die1 = StdRandom.uniform (1, 7); die2 = StdRandom.uniform (1, 7); public boolean isDoubles) return die1 == die2; public int sum() return diel + die2 ToDo 1 // add an instance method that determines if both dice are even //hint boolean ToDo 2 // add an instance method that determines if both dice are odd //hint booleanExplanation / Answer
Note : Before running these programs, add the jar file stdlib.jar to the project folder .
Steps to add stdlib.jar to the project
Step1:Right click the project name in eclipse
Step2:Build Path ->Add external Archives
Step3: Select the location of the stdlib.jar(download from internet) from your computer
Step4:click on Open
---------------------------------------------------------------------------------------------------
/*
* The java class TestDie that creates an instance
* of PairOfDice and calls the methods isEven
* and isOdd and print if dice values are even
* or odd.
* */
//TestDie.java
public class TestDie
{
public static void main(String[] args)
{
//create an instance of PairOfDice class
PairOfDice pair=new PairOfDice();
//print die1 and die2 values
System.out.println(pair.toString());
//calling isEven method to check if
//both die1 and die2 are even
if(pair.isEven())
System.out.println("Both die values are even");
else
System.out.println("Both die values are not even");
//calling isOdd method to check if
//both die1 and die2 are odd
if(pair.isOdd())
System.out.println("Both die values are odd");
else
System.out.println("Both die values are not odd");
}
}
------------------------------------------------------------------------------
//PairOfDice.java
public class PairOfDice
{
//declare integer variables
private int die1, die2;
//constructor
public PairOfDice() {
roll();
}
//method roll
public void roll()
{
die1=StdRandom.uniform(1, 7);
die2=StdRandom.uniform(1, 7);
}
public boolean isDoubles()
{
return die1==die2;
}
public int sum()
{
return die1+die2;
}
/*The method isEven that return true
* if both the die values are even other
* wise returns false*/
public boolean isEven()
{
//% is modulo operator
//if die1%2 returns the remainder after division
return die1%2==0 && die2%2==0;
}
/*The method isOdd that return true
* if both the die values are odd other
* wise returns false*/
public boolean isOdd()
{
return (die1%2==1) && (die2%2==1);
}
//Helper method that prints die1 and die2 values
public String toString() {
return "die1= "+die1+", die2="+die2;
}
}
------------------------------------------------------------------------------
Sample Output:
die1= 3, die2=4
Both die values are not even
Both die values are not odd
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.