Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LoopWithDice.java : import java.util.*; public class LoopWithDice { /*Write a method inside this application called rollOdds() that takes 2 paramters: 1. die, a Die object

image text in transcribed

LoopWithDice.java :

import java.util.*;

public class LoopWithDice

{

/*Write a method inside this application called rollOdds() that takes 2

paramters:

1. die, a Die object

2. num, an integer

The method rolls the die num times and returns the number of times the

die landed on an odd face value. You MUST use a do-while loop for this

problem.

*/

public static int rollOdds(Die die,int num)

{

int total=0;//setup your counter

int count=0;//this var will count from [0-num[ (also called initial step)

do

{

die.roll();

if (die.getFaceValue() %2 ==1)

total++;

count++;//increment

}

while (count

return total;

}

public static void main(String[] args)

{

Scanner scan=new Scanner(System.in);

System.out.println("Enter the number of times to roll the die:");

int numOfTimes=scan.nextInt();

Die d1=new Die();

//here, let's invoke method rollOdds

int oddCount=rollOdds(d1,numOfTimes);

System.out.println("Total time the dia landed on an odd face: "+oddCount);

}

}

Die.java :

public class Die

{

private int faceValue; //private field using an access modifier called private

public Die()

{

//faceValue=(int)(Math.random()*6)+1;

roll();

}

public Die(int value)

{

faceValue=value;

}

public int getFaceValue()

{

return faceValue;

}

public void setFaceValue(int value)

{

faceValue=value;

}

public void roll()

{

faceValue=(int)(Math.random()*6)+1;

}

public String toString()

{

return "Die with face: "+faceValue;

}

}

Coin.java :

public class Coin

{

private final int HEADS = 0;

private final int TAILS = 1;

private int face;

public Coin()

{

flip();

}

public void flip()

{

face = (int) (Math.random() * 2);

}

public boolean isHeads()

{

return (face == HEADS);

}

public String toString()

{

String faceName;

if (face == HEADS)

faceName = "Heads";

else

faceName = "Tails";

return faceName;

}

}

1. Using the Die class from class work Coin class from chapter 5, design and implement a class called DieAndCoin, containing a Die and a Coin objects as attributes. Include a default constructor, methods to set and get each attribute, a toString method that returns a string containing face value of both the die and coin objects, for example: 5, HEADS. Also, include a method play() that rolls the die and flips the coin and a method win() that returns true if the face value of the die is even and the face of the coin is TAILS and false, otherwise. 2. Write an application TestApplication that uses the DieAndCoin class to create an object, called pair. The program uses pair to "play" a number of rounds entered by the user, e.g. 5 rounds, and prints to the screen the number of "wins". For example, if the following is pair info for 4 rounds of play: 3, TAILS 2, TAILS 4, HEADS 1, HEADS 6, TAILS the program will print: 2 wins

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Types of Interpersonal Relationships?

Answered: 1 week ago

Question

Self-Disclosure and Interpersonal Relationships?

Answered: 1 week ago