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
  1. Write a method called rollDice that returns the result from rolling two standard six-sided dice)
  2. To test this method, write a main() routine that does the following:
  3. 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
  1. 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: (Hint: The % operator is good for checking for divisibility of one integer by another)
     
  2. 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


Submitting:

Submit the files through the Assignment 4 Blackboard link. Make sure you attach BOTH files before you click Submit:
 Dice.java
 Primes.java