Java Program To Simulate Rolling Of Two Dice?

Feb 25, 2015

Write a program to simulate the rolling of two dice. The program should use Math.random (google how to use Math.random if needed) to roll the first die and should use Math.random again to roll the second die. The sum of the two values should then be calculated your program should roll 50,000 times. Use a single dimensional array to tally the numbers of times each possible sum appear(the possible values are 2 through 12, think about why?). Print the results in a tabular format.

Here's the code I'm working on so far:

public class RollingDice{
public static void main(String[] args){
int die1;
int die2;
int roll;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;
System.out.println("The roll of the first dice is: " + die1);
System.out.println("The roll of the second dice is: " + die2);
System.out.println("Your total roll is: " + roll);
}
}

What am I missing at this point, and what parts did I do wrong to change it?

View Replies


ADVERTISEMENT

Code A Program To Simulate Rolling Of 2 Dice

Jul 1, 2014

my assignment is to code a program to simulate the rolling of 2 dice. I have most of it set but when I run it and have it print out how many times each number (2-12) was rolled, it displays the numbers 1 off. for instance it will display the number of 4s rolled next to the 5s.

here it is

import java.util.Scanner; //allows info to be inputted//
import java.util.Random; //opens random number generator//
public class RollTheDiceWithArray13
{
 
public static void main(String[] args)

[code]....

View Replies View Related

Simulate Rolling Two Dice

Oct 18, 2014

Write a method called statistic that simulates the rolling of two dice 1000 times. (1000 times is a parameter of the method statistic).The program should have no input, and should use pseudo random numbers to simulate the rolls (one random number per die). Store the sum resulting from each roll of two dice, determine the number of times each 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 was rolled. The out produced by the program is:

Sum 2 3 4 5 6 7 8 9 10 11 12
Total 33 49 93 103 127 159 150 124 85 49 28

This is my code:

public static void main(String[] args) {
statistic(1000);
}
public static void statistic(int number) {
Random randomNumbers = new Random();

[code]...

i have been trying for hours to make it look like the output above but no luck. I was wondering if there's any other way writing the code without using arrays.

View Replies View Related

Simulate Rolling Dice - Int Values Representation

Jan 20, 2015

import java.util.Random;
/**
* A very basic Dice that can be rolled and represent int values
*/

public class Dice{
private Random rand;
public Dice(){
rand=new Random();
}
/**simulates rolling the dice*/

[Code] ....

Why the Random rand is initiated at the Constructor and not at roll? Like the code works for both but I remember our teacher telling us something about one uses more memory than the other.

View Replies View Related

Do While Loop - Rolling Dice And Getting Scores To Add Up To 100

Aug 21, 2014

I'm writing a program that is about rolling "dice" and getting the scores to add up to 100. There are two players in the game and each must take turns rolling dice choosing whether or not to keep rolling dice and accumulating more points during their turn while risking their points being lost by rolling a 1 or to add their turn points to their total points. The question I have is how would I exit the do while loop if the player chooses to add their turn score, thus adding their score and ending their turn?

Here is the coding so far.

Java Code:

import java.util.Scanner;
import java.util.Random;
public class Pig
{
public static void main(String[] args)
{
int die; int userTotalScore; int userTurnScore; int compTotalScore; int compTurnScore;

[Code] ....

View Replies View Related

Program To Simulate Operation Of Simple Robot

Apr 26, 2015

Write a program to simulate the operation of a simple robot. The robot moves in four directions: Forward, backwawrd, left, and right. The job of the robot is to move items and place it in the right slots in each station. There are 8 stations plus the pickup station. Pick up station is the initial start where the robot picks the items. 8 items can be placed on each station. The nearest station must be filled before placing items on the next station. The items are marked with identification or serial numbers. The items with odd serial number go to the right and items with even number go to the eft. The last slot, number 7 is reserved for light items which are less than 80kg. The serial number is a five digit number, the MSB digit 5 indicates that the product must be placed in the fifth station which is keeping the product at 20 degree F.

View Replies View Related

Multi-sided Dice - Keep Track Of Total Sum Of All Dice In Array

May 7, 2014

I'm finishing up this assignment, and I'm stuck. These are the last 2 instructions:

. Roll each of the Dice by invoking the roll method on each Dice in the array.
. Keep track of the totals sum of all the dice in the array. Be sure to roll the dice array at least 10000 times.

How to finish it up?

Here's my code so far:

package homework3;

import java.util.Random;
public class Dice {
private int numberShowing;
private final int numberOfSides;
private static final Random randomNumber = new Random();
public Dice() {
numberOfSides = 6;

[Code] ....

View Replies View Related

Hi Lo Dice Betting Program

Jun 29, 2014

I want to write a small Hi Lo Betting Dice program that satisfies these conditions :

A player places a bet on whether the sum of two dice will come up High (totalling 8 or higher), Low (totalling 6 or less) or Sevens (totalling exactly 7). If the player wins, he receives a payout based on the schedule given in the table below:

Choice Payout
High 1 x Wager
Low 1 x Wager
Sevens 4 x Wager

The player will start with $100 for wagering. If the player chooses to wager $0 or if he runs out of money, the program should end. Otherwise it should ask him whether he's wagering on High, Low or Sevens, display the results of the die rolls, and update his money total accordingly.

some sample output:

You have 100 dollars.
Enter an amount to bet (0 to quit): 50
High, low or sevens (H/L/S): H
Die 1 rolls: 1
Die 2 rolls: 5
Total of two dice is: 6
You lost!

You have 50 dollars.
Enter an amount to bet (0 to quit): 25
High, low or sevens (H/L/S): L
Die 1 rolls: 6
Die 2 rolls: 2
Total of two dice is: 8
You lost!

You have 25 dollars.
Enter an amount to bet (0 to quit): 12
High, low or sevens (H/L/S): H
Die 1 rolls: 2
Die 2 rolls: 6
Total of two dice is: 8
You won 12 dollars!

I have written some code but now i am stuck at the logic ...

package com.peg.hilodice;
import java.util.Scanner;
public class DiceGame {
 public static void main(String[] args) {
// TODO Auto-generated method stub
 int money = 100;
int bet;

[Code] .....

View Replies View Related

Add Program In Order To Dice To Roll

May 8, 2014

what code I should add to my program in order to get these dice to roll. The program, according to my teacher, is CORRECT so far, so I'm not going to change anything. I just need to know how to roll the 2 dice I added in. I just want to get it done. No math.Random. The direction is: Roll each of the Dice by invoking the roll method on each Dice in the array.

My code:

//Dice.java
package homework3;
import java.util.Random;
public class Dice {
private int numberShowing;
private int numberOfSides;
private static final Random randomNumber = new Random();

[code]....

View Replies View Related

Simulate Simple Calculator That Performs Basic Arithmetic Operations Of JAVA

Sep 9, 2014

How can i write a java program that simulate a simple calculator that performs the basic arithmetic operations of JAVA. (JOption Pane and Repeat control structure)

Example : Addition, Subtraction, Multiplication, Division.

The program will prompt to input two floating point numbers and a character that represents the operations presented above. In the event that the operator input is valid, prompt the user to input the operator again.

Sample Output :

First number 7
Second number 4
Menu

<+> Addition
<-> Subtraction
<*> Multiplication
</> Division

Enter Choice: * <enter>

The answer is 28.

View Replies View Related

Simulating Roll Of D6 Dice With Java Code

Apr 21, 2015

I am trying to figure out how to simulate an ability score calculation for an RPG character creation. The way I want to do it is to have 5 d6 dice rolled and then the best three of the 5 rolls are added together for a maximum of 18 points and a minimum of 3 points.

Now I know how to generate random numbers, but I do not know how to have the best 3 picked out. What I have right now is super simple.

package javaapplication12;
import java.util.Random;
public class JavaApplication12 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {

[Code] ....

So this gives me my five rolls but then have the code pick out the best three every time the program is run is kind of an issue for me.

View Replies View Related

Create A Simple Dice Game Java Eclipse Android

Jan 8, 2015

The game is too have two players, each user clicks a button and two dices will roll, if a user rolls a double they win.

firstly I have started with two imageview's and a button I am trying to randomise two images by clicking one button I have managed to randomise one image in one box but I am struggling to randomise both image views here is my code so far.

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;

[Code]....

That code is only generating a random dice image in one of the image views I have hit a brick wall

View Replies View Related

Using Queues To Simulate Airport

Nov 2, 2014

Suppose that a certain airport has one runway, that each airplane takes LandingTime minutes to land and TakeOffTime minutes to take off, and that on the average, TakeOffTime planes take off and LandingTime planes land each hour.

Assume that the planes arrive at random instants of time. (Delays make the assumption to randomness quite reasonable.) There are 2 types of queues: a queue of airplanes waiting to land and a queue of airplanes waiting go take off. Because it is more expensive to keep a plane airborne than to have one waiting on the ground, we assume that the airplanes in the landing queue have priority over those in the take off queue.

Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue. Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue. You might also investigate the effect of varying arrival and departure rates to simulate the prime and slack times of day, or what happens if the amount of time to land or takeoff is increased or decreased.

My Queue Interface: [URL] ....
My Queue Implementation: [URL] ....
My Demo (Main Method) Program: [URL] ....

Right now, I'm struggling with the demo program. I wrote some pseudocode in which I tried to match what the instructions were asking:

for(each minute) {
rand1 = generator.nextInt();
rand2 = generator.nextInt();
if(rand1 < LANDING_RATE / 60)
Add to the landing queue
if(rand2 < TAKE_OFF_RATE/60)

[Code] .....

Firstly, exactly how many minutes am I supposed to be doing this for? The assignment gave me a variable called final int ITERATIONS = 1440 , could that have something to do with how long I loop? Secondly, what exactly do I add to the queue if the conditions are true. For example, if rand1 < LANDING_RATE/60, what would I enqueue to the landingQueue? Thirdly, how am I supposed to check if the runway is free? Does that mean check to see whether the takeOffQueue is empty or not? Fourth, would allowing the first plane to land mean removing an item from the landingQueue? Also, what does it mean by "otherwise, consider the takeoff queue". Does that mean if the landingQueue is empty, I should start removing items from the takeOff queue?

The biggest problem is the fact that I have to calculate the average queue length and average time that an airplane spends in a queue.

View Replies View Related

Using Queues To Simulate Airport Operation

Nov 13, 2014

*Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue*.

*Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue.*

I have most of the code done as you can see below:

* Queue Interface: Queue.java
* Queue Implementation: ArrayQueue.java
* Demo Program: Airport.java

Right now, I am stuck on the first calculation which is trying to figure out the average size of the landing queue. For some reason, as you can see at the bottom of my demo program, the average always comes out to be 0.0 and I'm not sure what's wrong.

Secondly, how to calculate the average time a plane stays inside a queue for. Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue.

Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue.

I have most of the code done as you can see below:

* Queue Interface: Queue.java
* Queue Implementation: ArrayQueue.java
* Demo Program: Airport.java

Right now, I am stuck on the first calculation which is trying to figure out the average size of the landing queue. For some reason, as you can see at the bottom of my demo program, the average always comes out to be 0.0 and I'm not sure what's wrong.

Secondly, how to calculate the average time a plane stays inside a queue for.

View Replies View Related

Applets :: Simulate Stack - Using Push / Pop

May 10, 2014

I have designed an applet to simulate a stack (last-in, first-out) of strings. The applet consists of:

Two JButtons
Two JTextFields
Two JLabels
A TextArea

The user enters text in the first text field then clicks the push button and it goes in the textArea. After entering several strings the user can click the pop button and the top string goes into the second text field. I am a total newbie and am stuck. I can get a string of text to push to the textArea and then pop it out; however I can't stack them LIFO. Most examples I've seen use integers and I'm not sure how to translate the concept to strings. I'm just not sure how the stack concept works even though I read the API webpage.

My code

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JButton;

[Code] ....

View Replies View Related

Create And Simulate ATM Interface Using Scanner Class

Sep 28, 2014

I need to create and simulate an ATM interface for my computer programming class using scanner class, if else & switch statements. I've been working on this assignment all day and I'm still no closer to figuring out how to do it. I'm currently working in NetBeans to try and solve it. I have attached the pdf , what I need to do. This is what I have so far:

package bankatmifelse;
//Gator Bank ATM Program
import java.util.Scanner;
public class BankATMIfElse {

[code]...

View Replies View Related

Simulate Press Of Space Button Every 15 Minutes

Jul 1, 2014

I'm on windows 8 and I want to use notepad or notepad++ to simulate the press of the space bar every 15 minutes so my computer doesn't auto lock.

View Replies View Related

Simulate Airport - Calculate Average Time A Plane Stays Inside A Queue For

Nov 13, 2014

Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue.

Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue.

I have most of the code done as you can see below:

* Queue Interface: Queue.java
* Queue Implementation: ArrayQueue.java
* Demo Program: Airport.java

Right now, I am stuck on the first calculation which is trying to figure out the average size of the landing queue. For some reason, as you can see at the bottom of my demo program, the average always comes out to be 0.0 and I'm not sure what's wrong.

Secondly, how to calculate the average time a plane stays inside a queue for.

View Replies View Related

Using Loops For Dice Probability

Oct 5, 2014

The instructions are to "write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled... then ask the user to enter the number of sides on a die" The code is all provided here, I just have to put it in the right spots. (Side note, I cannot use arrays.) Here's the original file I was given to work with:

Java Code:

import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args) {
//Declare and initialize variables and objects
//Input: ask user for number of rolls and number of sides on a die
//Print heading for output table

[Code] ....

When I run it, I get something like this (just a part of my output):

Please enter the number of sides on a die: 6
Please enter the number of rolls: 2

Sum of dice Probability

20.0
20.0
30.0
30.0
40.0
40.0
50.0
50.0
650.0
650.0
70.0
70.0

Now I don't really understand the concept in the first place as the lesson was extremely uninformative, so I don't know what went wrong here.

View Replies View Related

Dice Roll GUI - Where To Place Constructor

Feb 17, 2014

I am making a Dice Roll GUI and I have most of it down and I only need this constructor to work for the program to work (I think). I don't know where to place the constructor, I tried placing it around the RollButton class but it still didn't work and gave me java error constructor in class cannot be applied to given types

Here's my constructor:

private JPanel panel;
public RollButton(JPanel panel){
this.panel = panel;
}

Here's my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
import javax.imageio.ImageIO;

[code]....

View Replies View Related

Obtain Dice As Text Not Numbers

Oct 2, 2014

package dicedemo;
public class DiceDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

final int Die1_SIDES = 6; // Number of side of dice 1
final int Die2_SIDES = 6; // Number of side of dice 2
// Create two instances of the Die class.

[Code] ....

I need converting the number output to text: example one, two, three, etc.

View Replies View Related

Changing Number Of Sides On A Dice From 6 To 12

May 29, 2014

I have this really a graphics program that I'm working on which displays a single die, and right now it is set to the default 6 sides. I need to change it to 12. Here is the program:

package homeworkweek6;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

[code]...

Do I have to add more code somewhere further up in the program, or is there a line to replace these two lines to make it 12-sided?

View Replies View Related

All Possible Outcomes Of Dice Roll Using Recursion

Nov 26, 2014

Here is what I have so far:

/**
* This class encapsulates a simple dice game. The number of dice and number of sides on those dice are given by instance variables. The outcomes ArrayList holds a list of all possible outcomes of throwing that number of dice with that number of sides.
*
* If there were 2 dice, each with 6 sides, then possible outcomes would include 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 2 1, 2 2, 2 3, and so on.
*
* Your task is to complete the methods that calculate the possible outcomes. One method calculates outcomes allowing for repeated numbers. One method calculates the outcomes of a fictional dice game where repeated numbers cannot occur.
*
* You must use recursion. This is a variation on the permutations problem from the book.
*/

public class Dice
{
private static int numberOfSides;
private static int numberOfDice;
public ArrayList<String> outcomes;

[code]...

I manage to calculate the numberOfOutcomes correctly, but then get a nullPointerException. Also, is there a way that I can use getNumberOf Outcomes without making it static? The testing code that the teacher provided is using it in another class and if I make it static, then it doesn't work in that file.

View Replies View Related

Not Counting Total Of Dice Rolls

Jan 25, 2015

I am currently trying to roll 5 dice 100,000 times and output the number of times each possible total (of dots/pips) occurs. It seems to output the incorrect thing and isnt counting the occurrences properly.

public class lab_DiceRollTest {
public static void main(String[] args) {
int total;
int total2;
String valid;
String isDifferent;
int rolls = 100000;
int d = 0;

[code]...

View Replies View Related

Roll Two Dice / Add Them Together And Print The Result

Oct 23, 2014

Write a program that rolls two dice, adds the numbers and prints out the results. I have managed to do this but we have to make it a horizontal table instead of a vertical one. like the attached file "CORRECT", right now i only get the "NOTCORRECT". i have tried for hours to fix this but i can't.

import java.util.Random;

public class Statistikk {
public static void skrivStatistikk() {
Random rand = new Random();
int[] antall = new int[13]; {

[Code] .....

(Antall is basically frequency|side is the same as a face of a die)

Here is my code, does my code need improvement? Or is it my printf that is the issue? i've tried deleting the 's and adding the printf to the same line, but nothing worked.

Attached image(s)

View Replies View Related

Random Dice Roller Game

Oct 23, 2014

So far I have

import java.util.Random;
import java.util.Scanner;
public class DiceGame6 {
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
System.out.println("Welcome to Computer Dice!");
System.out.println("-------------------------------");
System.out.println("You will first roll your dice

[code]....

How would I get the code to count the number of times something occurred. In the end it's suppose to count how many times I played the game and how many times I've won vs lost.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved