Find Us On:

# Roll the dice (Java program)

josh2013

If the user chooses to roll the dice, the application rolls two six-sided dice, displays the results of each, and asks if the user wants to roll again. Specifications • Create a class named Die to store the data about each die. This class should contain these constructors and methods: public Die() // default to a six-sided die public Die(int sides) // allow a variable number of sides public void roll() public int getValue() using Math class to generate a random number from 1 to the number of sides. It should look this after : Welcome to the Paradise Roller application Roll the dice? (y/n): y Roll 1: 2 5 Craps! Roll again? (y/n): y Roll 2: 2 1 Roll again? (y/n): y Roll 3: 4 6 Roll again? (y/n): y Roll 4: 6 6 Box cars! Roll again? (y/n): y Roll 5: 1 1 Snake eyes! Roll again? (y/n): n Attachments
Tags Clarifications

## Java - Roll Dice - Solution

dickersonka

Attached is the solution including source and comments to the Java - Paradise Roller assignment Attachments
DiceRoller.zip (1 K)
Preview
System.out.println("Welcome to the Paradise Roller Application"); System.out.println(); //prompt to roll dice System.out.print("Roll the dice? (y/n): "); choice = input.nextLine(); int rollNum = 1; //keep looping until user chooses no while(!choice.equalsIgnoreCase("n")){ //roll both dies die1.roll(); die2.roll(); //get

Preview
random number between 1 and number of sides this.number = (int) (Math.random() * sides) + 1; } /** * Get the value on the die * @return Die value between 1-sides */ public

Screenshots

## Rolling dices

dianagaby2002

"Roll.java"

import java.io.*; //to read y/n(Y/N)

//to read the number of faces of die
import java.util.Scanner;

// Method to read char input (y/n): includes casting //explanation(why read and cast to char)
static public char getChar() throws IOException{...}

main method:
do{...}while() loop for dialog with user and calling the corresponding methods from "Die.java"

"Die.java": public void roller() checks the 2 values and displays a message if the cases above are met ("Craps!", "Box cars!", "Snake eyes!").

public Die() // default to a six-sided die
public Die(int sides) // allow a variable number of sides

public int getValue() gets the current die's value
Attachments
Die.java (1 K) Preview
private static int v1, v2; public Die(){ value = (int)(Math.random()*6) + 1; } public Die(int x){ value = (int)(Math.random()*x) + 1; } public void roll(){ counter++; if(counter == 1){ v1 = getValue();

Roll.java (1 K) Preview
throws IOException { System.out.println("Welcome to Paradise Roller application \n"); Die die1, die2; char answer; // 'y' or 'n' int faces; //Scanner object to read the number of faces Scanner sc = new Scanner(System.in); System.out.println("Roll the dice? (y/n):"); answer =

Screenshots