Stats: 3,184,274 members, 7,923,295 topics. Date: Friday, 16 August 2024 at 10:46 PM |
Nairaland Forum / Science/Technology / Programming / Java Proramers:java Assignment: Can Someone Help Provide A Solution To This (4716 Views)
Java Programers:java Assignment: Can Someone Help Provide A Solution To This / Need A Java Programmer To Put Be Through On A JAVA ASSIGNMENT / Help Java Programmers: Need Help With A Java Assignment (2) (3) (4)
Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by buster(m): 7:53pm On Nov 21, 2014 |
Good evening This is an assignment to a program am running in school. Despite all way of trying to get a code for it am still stuck and i have a deadline soon. Please kindly help out. The assignment is below For the unit 2 assignment, you will develop a program that will simulate the rolling of a pair of dice. Your program must simulate rolling two dice and keep track of how many rolls are required before snake eyes are rolled (Note: “Snake Eyes” means that both dice show a value of 1). You will need to use a loop structure to repeatedly ‘roll the dice’. You will also need to make use of the if statement to determine if both die show a 1. You can use the example of an if statement below in your program and you can also read ahead to next weeks lesson for more information on the if statement. To roll the dice, you can use the following Java statement which generates a random number between 1 and 6 (to represent the six sides of the dice). die1 = (int)(Math.random()*6)+1 This statement has some interesting characteristics that need to be explained. The first thing we notice is that we are assigning something to a variable with the name die1. You will need to execute this statement twice, once for a variable die1 and the second time to assign to variable die2 to represent the roll of two dice. The second thing we notice about this is this weird (int) placed in front of the parentheses that contain a subroutine call and a math expression (Math.random()*6). What the (int) in this case is doing is called a cast. The cast is a way to force java to convert data from one data type to another. The subroutine (actually this is calling the class Math and the method random) Math.random() will return a number that is between 0 and 1. This number is a floating point number. By multiplying it by the number 6 we will get a random number between 0 and 5. This number will be a floating point number and is likely to be a fraction meaning that it will have digits to the right of the decimal point. What the cast or (int) in this case does is convert this number into an integer. As you recall from earlier discussions, this will truncate all of the digits to the right of the decimal point. For example assume that that the Math.random() subroutine returns the value of .523432. This value is then multiplied by 6 resulting in 3.14059 to this we add 1 resulting in 4.14059. The reason that we are adding a 1 is because we want a number between 1 and 6 starting at 1. By adding a 1 we know that the starting number will always be 1 or greater. What the (int) will do is to truncate everything to the right of the decimal point and then assign it to the variable die1. The resulting value in die1, in this case, would be 4. An if statement that will test for ‘snake eyes’ would be as follows. if (die1 == 1 && die2 == 1) { // do something } You must complete your program, test, debug, and execute it. You must submit your java code file. Post the output of your program by specifying the number of rolls of the dice before you got snake eyes. Run the program at least five times and report your results. Challenge: See if you can modify your program using a for loop to simulate running your program 1000 times and calculate the average number of rolls required to get snake eyes … what is the average number of rolls? 1 Like |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by Sheggy13(m): 6:52am On Nov 22, 2014 |
I'm guessing programming isn't your piece of cake, otherwise these kinda exercises should be trivial to u. Can u write out your email address here so I could mail the answers to u. Try to do that early enough this morning so I can mail u, cos I'll be on the road majorly today. Btw what level are u in and the name of ur uni if u don't mind? Cheers. |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by Sheggy13(m): 7:46am On Nov 22, 2014 |
buster:I'll post the solution of the first part here now, but I'm kinda confused about the challenge, ie the second part. Are they asking the number of snake eyes u get when u roll each of the die 1000 times? |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by buster(m): 8:22am On Nov 22, 2014 |
My email address is abbeybidmus@yahoo.com thanks so much I would be expecting it |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by buster(m): 8:24am On Nov 22, 2014 |
The main assignment is to calculate the number of times a dice will row before it gets to a snake eye. The second part of it is just a hint to assist. My email address is abbeybidmus@yahoo.com Sheggy13: |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by Sheggy13(m): 8:32am On Nov 22, 2014 |
buster:okay here u go. Solution to first question: (a.) The solution to the main question. class dieRoll { public static void main (String [] args) { // declare variables boolean rollContinue = true; int rollCount = 0; int die1,die2; while (rollContinue) { die1 = (int)(Math.random() * 6) + 1; die2 = (int)(Math.random() * 6) + 1; System.out.println("Result of rolling the dice: " + die1 +" "+ die2); rollCount++; if (die1 == 1 && die2 == 1) { System.out.println("Snake eyes have been reached after " ![]() System.out.print( rollCount + " rolls of the dice!" ![]() rollContinue = false; } else System.out.println("Die Rolling continues.Snake eyes not reached!" ![]() } } } (b.) The challenge, which is to run it 1000 times and report how many rolls on the average it takes to get snake eyes using for loop. class thousandRoll { public static void main (String [] args) { // declare variables int rollCount = 0; int die1,die2; for (int count = 1; count <=1000; count++) { die1 = (int)(Math.random() * 6) + 1; die2 = (int)(Math.random() * 6) + 1; System.out.println("Result of rolling the dice: " + die1 +" "+ die2); rollCount++; if (die1 == 1 && die2 == 1) { System.out.print("Snake eyes have been reached after an average of " ![]() System.out.println( rollCount + " rolls of the dice!" ![]() break; } } } } Please replace those smileys that appeared in the code with closing bracket ). Also find attached the files. I saved in notepad with a .java extension, so u can just run them directly from the command prompt. Cheers
|
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by buster(m): 8:34am On Nov 22, 2014 |
Sheggy13:Am in my second year @ Devry university in d USA. But am in Nigeria, its an online program |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by buster(m): 8:34am On Nov 22, 2014 |
Sheggy13:Am in my second year @ Devry university in d USA. But am in Nigeria, its an online program. My email address is abbeybidmus@yahoo.com |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by buster(m): 8:39am On Nov 22, 2014 |
Sheggy13: Am in my second year(computer science) at devry university in d US. But am in Nigeria cos its an online program.. My email address is abbeybidmus@yahoo.com |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by asalimpo(m): 11:05am On Nov 25, 2014 |
buster: Spell that 'p' word correctly bf u get any help here.. |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by abenmariem: 12:15pm On Aug 02, 2016 |
The cast is a way to say to the compiler: i know what i do trust me i am an engineer. this casting in java tutorial explains this concept very well. |
Re: Java Proramers:java Assignment: Can Someone Help Provide A Solution To This by ubongudobong: 6:06pm On Aug 02, 2016 |
jst Login to http://www.pinemay.com for help |
(1) (Reply)
Coding Challenge For Fun / Ambitious Programmers Lets Talk / Remita Integration
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 47 |