Question

In: Computer Science

Write a method that takes the sides of a dice and produces a random number between...

Write a method that takes the sides of a dice and produces a random number between 1 and the value of the dice sides.
use the following header:
public static int roll(int diceSides)

Add a main method to the class that tests the method by prompting for a int for dice sides and a color for the die,
invoking the method and printing the returned number and the color of the die.

SAMPLE RUN #1: java DiceRoll

Enter a number of sides: 20↵
Enter a color for the die: Red↵
The Red die rolled a 13↵

Solutions

Expert Solution

import java.util.*;

class A {
  
   public static int roll(int diceSides){
       Random rand = new Random();
       int n = rand.nextInt(diceSides)+1;
       return n;
   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter a number of sides: ");
       int n = sc.nextInt();
       System.out.print("Enter a color for the die: ");
       String s = sc.next();
       System.out.println("The "+s+" die rolled a "+roll(n));
   }
}

File Edit View Search Terminal Help nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$ javac A.java nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$ java A Enter a number of sides: 20 Enter a color for the die: Red The Red die rolled a 6 nikhil@nikhil-Vostro-15-3568:-/Desktop/CODES |

~/Desktop/CODE/A.java (CODE) - Sublime Text (UNREGISTERED), File Edit Selection Find View Goto Tools Project Preferences Help FOLD A.java * demo. cx demo.cpp * d.cpp x index.html * import java.util.*; class A { * public static int roll(int diceSides) { Random rand = new Random(); int n = rand.nextInt(diceSides); return n; public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number of sides: "); int n = sc.nextInt(); System.out.print("Enter a color for the die: "); String S = sc.next(); System.out.println("The "+5+" die rolled a "+roll(n)); 18 19 1 1 1 N N N N N Line 19, Column 2 Tab Size: 4 Java

THANK YOU!! PLEASE VOTE


Related Solutions

Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
1) Write a method that takes a string representing a positive binary number, and writes out...
1) Write a method that takes a string representing a positive binary number, and writes out the number in hex. (Extra credit opportunity. Write the method so that the input can be a binary number in two's compliment form.) 2) Write a method that takes an integer and writes out its value out in hex. You cannot use string formatting characters. 3) Write an encodeString method that encodes a string of ASCII Characters. Input: It takes two parameters: a) inputString:...
Write a complete main method that does the following: Takes any number, but at least two,...
Write a complete main method that does the following: Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array) If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.
In Java: Write a complete main method that does the following: 1. Takes any number, but...
In Java: Write a complete main method that does the following: 1. Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of that end with a digit. (Hint: loop through the args array) 2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.
Write a complete main method that does the following: Takes any number, but at least two,...
Write a complete main method that does the following: Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of length three. (Hint: loop through the args array) If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message. For ex: Jenna has a new sister: there are 2 words of length three public class Question1 { public...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask the user if they want to roll again. If they don't want to roll again, the program should end. If they do want to roll again, the new number should display. At a minimum, the die should have 6 sides, but if you want to use a d12 or d20, that is fine too. USE PYTHON
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Sarah D's road manager plays a dice game with two dice. The six sides of on...
Sarah D's road manager plays a dice game with two dice. The six sides of on each die are numbered from 2 to 7. Define the random variable X to be the sum of the numbers on the up-face of the two dice when rolled. a. Construct table showing PDF and the CDF of the random variable X b. Calculate the mode of the distribution. c. Calculate the expected value of X. d. Calculate variance of X. e. Calculate P(4
Python: Accumulator pattern and conditionals. Write a function play that takes integers rounds and sides and...
Python: Accumulator pattern and conditionals. Write a function play that takes integers rounds and sides and does the following  Plays round number of rounds, rolling two s-sided dice per round (use roll).  Uses the previous functions to calculate the score of each round  Prints out the dice rolls and the score for each round  Accumulates the score over all rounds  Returns the final score over all rounds. *Write a function result that takes an arguments...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT