Assignment #4 - Writing Methods
Due: Wed, June 18
Objective
This assignment will consist of writing two small programs
that involve practice writing and calling methods
Task
Write the following programs, each in a separate file. Filenames should
be:
Exercise 1
Filename: Dice.java
- Write a method called rollDice that returns the result
from rolling two standard six-sided dice)
- The method will take no parameters, but it will return the
resulting total of the dice roll as an integer
- Remember that the rolling of a die involves randomness, so you'll
need to use the random number generation library discussed in
class (see online notes).
- Note that rolling two 6-sided dice is NOT equivalent to picking a
random number from 2 through 12. This is because when you roll 2
dice, there is a much larger change of hitting some totals than
others. You need to roll two dice and add them together
- To test this method, write a main() routine that does the
following:
- Ask the user to enter how many times they want to roll the
dice, and let them enter a value (for this writeup, I'll call
it N)
- Call the rollDice method this many times (i.e. N times),
and
count how many
times a total of 2 (known as "Snake Eyes") appears, as well as how
many times a total of 7 appears.
- For each of these totals (2 and 7), print out how many times that
total appeared, as well as what percentage this is of the total
number of rolls. Percentages should be printed to 2 decimal
places.
- Note: When you declare the Random object, instead of
putting it inside of main(), declare this variable inside the
class block, but NOT inside of either of the methods. Declare it to be
static. This way, it will be accessible to both your
rollDice method, as well as to main
Sample Run 1
(user input underlined)
How many times would you like to roll the two dice? 10000
Snake eyes (double 1s) appeared
268 times
2.68 % of the time
A roll of 7 appeared
1675 times
16.75 % of the time
Sample Run 2
(user input underlined)
How many times would you like to roll the two dice? 500000
Snake eyes (double 1s) appeared
13706 times
2.74 % of the time
A roll of 7 appeared
83300 times
16.66 % of the time
Sample Run 3
(user input underlined)
How many times would you like to roll the two dice? 500000
Snake eyes (double 1s) appeared
13921 times
2.78 % of the time
A roll of 7 appeared
83789 times
16.76 % of the time
Exercise 2
Filename: Primes.java
- Recall that an integer is a prime number if it is divisible only
by 1 and itself. Write a method called isPrime
that takes in one integer parameter X and determines whether or not X is
prime. The method should return a boolean value:
- return true if X is a prime number
- return false if X is not a prime number.
(Hint: The % operator is good for checking for divisibility of
one integer by another)
- To test this method, write a main() routine that asks the
user to input a positive integer N. Using your isPrime()
method, find and print all the prime numbers less than or equal to N,
where the output has 8 numbers per line (you can use tab characters to
separate numbers on a line).
Sample Run
(user input underlined)
Please input a positive number: 500
The prime numbers less than or equal to 500 are:
2 3 5 7 11 13 17 19
23 29 31 37 41 43 47 53
59 61 67 71 73 79 83 89
97 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263
269 271 277 281 283 293 307 311
313 317 331 337 347 349 353 359
367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457
461 463 467 479 487 491 499
Requirements for each program
- The required tasks must be performed with the methods
specified (not just with a single main() routine)
- Note that each exercise requires the writing of a method, and a
main routine to test that method.
- Each method should do exactly the task specified in item 1
of each exercise
- Item 2 of each exercise specifies what to do in main() --
this will always involve CALLING the method, sometimes more
than once
- Note that there is NO keyboard-input/screen-output specified in the
methods themselves (i.e. "return" does not mean "print") --
this means you should NOT have any print statements or Scanner
usage inside these named methods. Any printing and/or keyboard
input is done by main() in each exercise
- When you write source code, it should be readable and
well-documented.
Submitting:
Submit the files through the Assignment 4 Blackboard link. Make sure you
attach BOTH files before you click Submit:
Dice.java
Primes.java