Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CLASS:
- ------
- package Assignment4;
- /**
- *
- * @author
- */
- public class Die {
- private double sides; // the amount of sides on the die
- private int sideShowing; // side currently showing
- public Die(int sides) {
- this.sides = sides;
- sideShowing = rollDice();
- }
- //get the current face of the die
- public int rollDice() {
- sideShowing = (int)(this.sides * Math.random()) + 1;
- return sideShowing;
- }
- public int getCurrentSide() {
- return sideShowing;
- }
- @Override
- public String toString(){
- return "d" + (int)sides + "=" + (int)sideShowing;
- }
- }
- -----------------------------------------------------
- Main Method:
- package Assignment4;
- import java.util.Arrays;
- import java.util.Scanner;
- /**
- *
- * @author
- */
- public class MainHistogram {
- public static void main(String[] args) {
- int menuChoice, diceAmt, rolls, sides;
- Scanner keyboard = new Scanner (System.in);
- System.out.println("How many dice?");
- diceAmt = keyboard.nextInt();
- Die[] diceArray = new Die[diceAmt];
- for (int i = 0; i < diceArray.length; i++){
- System.out.println("Enter the number of sides on die " + (i+1) + ": ");
- sides = keyboard.nextInt();
- diceArray[i] = new Die(sides);
- }
- do{
- System.out.println("Dice Collection: " + Arrays.toString(diceArray));
- System.out.println(" 1=roll once, 2=roll 100,000 times, 3=quit: ");
- menuChoice = keyboard.nextInt();
- if (menuChoice == 1){
- rolls = 1;
- System.out.println(Arrays.toString(diceArray));
- }
- if (menuChoice == 2){
- rolls = 100000;
- }
- }while ( menuChoice != 3);
- System.out.println("BYE!!!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement