Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this is my class for the die and the rolls
- import java.util.Scanner;
- public class DiceGame
- {
- int numSides;
- String name;
- int totalSides;
- public static void main(String[] args)
- {
- Die die1 = new Die();
- Die die2;
- 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 and stores the responses in the variable names.
- 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();
- }
- }
- //this is to count the number of rolls and print every 5000 rolls
- // Im not sure what is missing here
- public void numRolls()
- {
- int rollNum;
- for (rollNum = 0; rollNum <= 50000; rollNum++)
- {
- if (rollNum % 5000 == 0)
- System.out.println(rollNum);
- }
- }
- // this will out put the persons name and the column names(this was not needed i just wanted it to be more organized)
- public void output()
- {
- System.out.println("Experiment by: " + name.substring(0,6));
- System.out.println();
- System.out.printf("%-20s%-20s%s\n", "Number of Rolls", "Money Made", "Average Money Made");
- }
- // this will calculate the money for each roll
- // there is something wrong here too im just not sure what.
- public void money()
- {
- int money = 0;
- if (totalSides < 5)
- {
- money += 2;
- }
- else if (totalSides == 5);
- {
- money += 8;
- }
- else
- {
- money -= 3;
- }
- }
- }
- //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