Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: HELP urgent

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Thanked 0 Times in 0 Posts

    Default HELP urgent

    I was given this school assignment

    you will develop a program that will simulate the rolling of a pair of dice. You 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. 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.

    You must complete your program, test, debug, and execute it. You must submit your java code file. The output of your program must be captured by either copying the content in the output window and pasting it into a text document that you submit along with your java code file, or by capturing the image of the screen which contains the output of your java program. In windows you can capture a screen shot with the Ctrl Alt and Print Screen key sequence. This image can then be pasted into a Word, WordPad, or OpenOffice document which can be submitted with your assignment.

    Can someone help me out

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,273 Times in 2,244 Posts

    Default Re: HELP urgent

    What have you tried so far?
    Do you have any specific java programming questions?
    If you are getting error messages, please copy and paste here

  3. #3
    Join Date
    Feb 2012
    My Mood
    Thanked 11 Times in 11 Posts

    Default Re: HELP urgent

    Its true we can write your assignment for you, but you will learn nothing, and your programs are only going to become harder.

    If reading the assignment is scaring you, stop what you are doing and go for a short 1 min break to get a snack or stretch.
    Then, come back and make sure you have a piece of paper and a pen.
    You will need to do what every programmer does which is trying to understand what is wanted.
    Sometimes in early programming classes this is the most difficult because its exactly like reading a foreign language.

    So if that is what you need help with, ask for that specifically.
    (Though this may be an indication you are not absorbing the class lecture!)

    If you understand what is wanted, think about it for a few minutes.
    Try to understand what topic your teacher is trying to teach you.
    (how to use arrays? if statements? for loops? ) once you figure that out, it will give you a bonus insight on how to write the code.

    Using your pen and paper, write out the steps your program has to complete in non code form.

    then go through your steps by reading them writing down some notes of what your different variables are doing.
    E.G. "okay, at this point my int array(AKA int[]) called numbers has a 3 then a 4 then a 7" you would probably write this as: numbers = (3,4,7)

    Repeat this until you are happy you are taking all the needed steps. (this is called pseudo code, and you will get better and faster at it)

    this helps you keep track of what your program does with information.

    now the fun part! try to code it!

    once you finish trying, if you still cant make it, come back and post your code with specific questions.(using a question mark actually helps us!)



Similar Threads

  1. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  2. [SOLVED] need help urgent
    By rupa in forum Java Servlet
    Replies: 1
    Last Post: January 19th, 2011, 01:41 PM
  3. Urgent!!!!!!!!!!!!!!!!
    By dinesh03188 in forum JDBC & Databases
    Replies: 3
    Last Post: May 14th, 2010, 01:38 AM
  4. Urgent Help Please :)
    By Jamie94 in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 12th, 2010, 06:34 AM
  5. Replies: 1
    Last Post: May 4th, 2009, 06:30 AM