Recitation4 - 15-100 RECITATION 4 FALL 2007 In this recitation you will experiment with writing a static method that has parameters and a return

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-100 RECITATION 4 - FALL 2007 In this recitation, you will experiment with writing a static method that has parameters and a return value and you will use a loop to call this method repeatedly. Exercise Using Eclipse, write a simple Java program in a project named Recitation4 that contains a class named NumberGenerator with the following methods: 1. Write a method that returns a random integer in the range [x, y], where x < y. Use the signature: public static int getRandomNum(int x, int y) // Returns a random integer in the range [x, y]. Assume x < y. HINT: Think about how to generate random integers for specific ranges like [1,100], [5, 25], [10,99] etc. and look for a pattern to get the formula you need for [x,y]. 2. Write a main method that calls the getRandomNumber method 10 times using a while loop to display 10 random numbers in the range [10,99]. Additional Exercises (If you have time...) 3. Modify your main method so that it outputs random numbers in the range [100,999] until a total of five even numbers are output. (HINT: x is even if x % 2 == what?) Use a variable to act as a counter and add 1 to it each time you get an even random number. 4. Modify your main method so that it outputs random numbers in the range [1000,9999] until five even numbers are output in a row. When You're Done... Create a zip file of the project folder for this recitation and submit this zip file on Blackboard to the Recitation area. DO NOT ERASE YOUR RECITATION PROJECT FOLDER IN CASE WE NEED TO SEE IT LATER (IF THE SUBMISSION IS DONE INCORRECTLY). ...
View Full Document

This note was uploaded on 01/25/2010 for the course CS 15100 taught by Professor Tom during the Fall '07 term at Carnegie Mellon.

Ask a homework question - tutors are online