Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Allison Finley
- This program will */
- //import the scanner so inputs can be read from the keyboard
- import java.util.Scanner;
- public class DiceGame
- {
- int numSides;
- String name;
- int totalSides;
- int money;
- double avgMoney;
- // this is to run the class with the functions of both.
- public static void main(String[] args)
- {
- //this creates an object of the class to use its methods
- DiceGame test = new DiceGame();
- test.readInput();
- test.output();
- test.numRolls();
- }
- //to ask the user for the number of sides on the second die
- //and their name
- public void readInput()
- {
- Scanner keyboard = new Scanner(System.in);
- System.out.print("Please enter the number of sides on the second die: ");
- numSides = keyboard.nextInt();
- keyboard.nextLine(); // Clearing the input stream.
- System.out.println("Please enter your name (minimum 5 characters): ");
- name = keyboard.nextLine();
- int len = name.length();
- while (len < 5)
- {
- System.out.println("Name must be at least "
- + "5 characters long. Please reenter: ");
- name = keyboard.nextLine();
- len = name.length();
- }
- }
- public void numRolls()
- {
- //this creates a new object for the first die.
- Die die1 = new Die();
- //this creates a new die object for the second die.
- Die die2 = new Die(numSides);
- int rollNum;
- for (rollNum = 0; rollNum <= 50000; rollNum++)
- {
- //counts the total sides of both die so calculate the money exchange.
- totalSides = die1.roll() + die2.roll();
- System.out.println("this is the total fo both sides " + totalSides);
- // this will take the number of sides and calculate the money
- if (totalSides < 5)
- {
- money += 2;
- }
- else if (totalSides == 5)
- {
- money += 8;
- }
- else
- {
- money -= 3;
- }
- //this will calculate the average money per roll
- avgMoney = money/rollNum;
- if (rollNum % 5000 == 0)
- System.out.printf("%-20i%-20i%d\n", rollNum, money, avgMoney);
- }
- }
- public void output()
- {
- System.out.println("Experiment by: " + name.substring(0,6));
- System.out.println();
- }
- }
- //this is his code
- //This class represents a Die (half a set of dice)
- import java.util.*; //for Random
- public class Die
- {
- //-------- data
- private int numSides;
- //-------- constructors
- //default constructor - sets the number of sides to 6
- public Die()
- {
- numSides = 6;
- }
- //parameterized constructor - sets the number of sides to whatever is passed in. Throws an
- // exception if number of sides if < 4
- public Die(int newSides)
- {
- if (newSides < 4)
- throw new IllegalArgumentException("number of sides cannot be less than 4");
- this.numSides = newSides;
- }
- //-------- methods
- //roll - rolls the Die by returning a random number between 1 and numSides
- public int roll()
- {
- Random generator = new Random();
- return generator.nextInt(numSides)+1; //generator.nextInt(numSides) returns random in range 0 to numSides-1
- }
- //getNumSides - returns the number of sides
- public int getNumSides()
- {
- return numSides;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement