Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help filling in the areas, unsure of how to do the code, JAVA. I've highlited what needs completing. import java.util.ArrayList ; import java.util.Random ;

Need help filling in the areas, unsure of how to do the code, JAVA. I've highlited what needs completing. 

import java.util.ArrayList ; import java.util.Random ; /** This program reviews how to manipulate an ArrayList, and how to use methods from the Die class. The expected output is: Here are 3 throws: 4 5 2 Here are 2 throws: 4 3 Here are 5 throws: 5 3 5 5 5 Here is 1 throw: 2 Here are 7 throws: 2 2 4 1 5 3 1 */ public class DieReview { public static void main(String[] args) { Random random = new Random(1) ; Die die = new Die(6, random) ; //-----------Start below here. To do: approximate lines of code = 3 // 1. make an arraylist called values to hold Integer objects //2. put into the arraylist 3, 2, 5, 1, 7 ; values.add(2) ; // values.add(5) ; // values.add(1) ; // values.add(7) ; // //3. for each value in values //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. if (value != 1) System.out.print("Here are " + value + " throws: ") ; else System.out.print("Here is 1 throw: ") ; //-----------Start below here. To do: approximate lines of code = 2 // 4. for i from 1 to value //5. throw the die and save the number returned as n //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. System.out.print(n + " ") ; } System.out.println() ; } } } /** This class models a die that, when cast, lands on a random face and presents one side up, which is its value. The value is from 1 to the number of sides which is set in the constructor, i.e., 1, 2, ..., sides see DieTester.java for the todo region */ class Die { private Random random ; private int sides ; private int sideUp ; /** Constructs a die with a given number of sides @param sides the number of sides, e.g., 6 for normal die */ public Die(int sides) { this.sides = sides ; random = new Random() ; sideUp = 0 ; } /** Constructs a die with a given number of sides and gives the random object @param sides the number of sides, e.g., 6 for normal die @param random the random number generator */ public Die(int sides, Random random) { this.sides = sides ; this.random = random ; sideUp = 0 ; } /** Simulates a throw of a die @return the face of the die */ public int cast() { sideUp = 1 + random.nextInt(sides) ; return sideUp ; } /** Gets the value of the side that is up @return the value of the side that is up */ public int getSideUp() { return sideUp ; } /** Gives a String representation of the Die @return the sideUp as a string */ public String toString() { return "" + sideUp ; } } 

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

What is Accounting?

Answered: 1 week ago

Question

Define organisation chart

Answered: 1 week ago

Question

What are the advantages of planning ?

Answered: 1 week ago

Question

Which form of proof do you find most persuasive? Why?

Answered: 1 week ago