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.
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
Consider four nonstandard dice, whose sides are labeled as follows (the 6 sides on each die...
Consider four nonstandard dice, whose sides are labeled as follows (the 6 sides on each die are equally likely). A: 4, 4, 4, 4, 0, 0 B: 3, 3, 3, 3, 3, 3 C: 6, 6, 2, 2, 2, 2 D: 5, 5, 5, 1, 1, 1 These four dice are each rolled once. Let A be the result for die A, B be the result for die B, etc. (a) Find P(A > B), P(B > C), P(C >D),...
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 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. Count the number...
C++. Write a program that generates a random number between 5 and 20 and asks the...
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 the user makes until...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT