ITEC 120 LAB 5 Back to Lab index page

Random number printer

You will write Java program that will randomly generate a number between 1 and 999, inclusive. It will print the number, then tell how many 100s, 10s, and 1s are contained in the number.

Sample 1

Program outputs:

The random number is: 782

782 is 7 x 100 + 8 x 10 + 2 x 1

You will use Math.Random() for this assignment. Math.random() generates a random floating point value between 0 and 1 -- including 0 but not including 1. So, to get a value between 1 and some number x, you would need to multiply the random number by x and then add 1. Then, cast the result to be an integer.

You'll also use if statements in your program to output a number only if the number is greater than 0. For example:
Program outputs:

The random number is: 61

61 is 6 x 10 + 1 x 1


Program outputs:

The random number is: 402

402 is 4 x 100 + 2 x 1

Can you solve this problem without having extra +s in the output?

Once you have your program working, show your lab to the Peer Instructor to be checked off. The Peer Instructor will run the program.