15-110 FALL 2009 [CORTINA]

LAB 4

In this lab, you will experiment with writing a static method that requires parameters and a return value, and you will look at your first loop to repeat the execution of code.

EXERCISES

Using Eclipse, write a simple Java program in a project named Lab4 that contains a class named NumberGenerator.

  1. Write a static method named getRandomNum that has two int parameters x and y. This method should generate and return a random integer in the range [x,y]. You may assume x < y.

    First, think about what the signature of the method should be. Remember, each signature needs a return type, then the method name, then the list of parameters in a pair of parentheses. Each parameter requires a data type and a variable name for that data. Once you have a signature, compare your signature with a neighbor's signature to see if you're on the right track.

    Next, 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]. Write code in your getRandomNum method to store the required random integer in a variable, and then use a return statement to return the value in that variable.

    Finally, write a main method that calls the getRandomNum method to get a random number between 10 and 20, inclusive, and prints the result that is returned. Run your program at least 10 times and check the results to see if your code is working so far. Change the main method so you get a random number between 100 and 199, inclusive, and run your program again a number of times to check for correctness. Once you are convinced your program is working correctly, move on to the next step.

  2. Modify the main method so that it generates a random phone number by calling your getRandomNum method two times. The first time it is called, get a random integer in the range [100, 999]. The second time it is called, get a random integer in the range [0, 9999]. In each case, store the returned value in a variable and then print out the phone number with a dash in between using your variables (e.g. 392-1532).

    NOTE: The first random integer must have three digits, but the second random integer may not have 4 digits. Write code to deal with this in your main method. For example, if the first random integer is 392 and the second random integer is 54, then you want to print the phone number 392-0054.

  3. A simple way to repeat code is to write a for loop in Java:

    for (int i = 1; i <= numberOfRepetitions; i++) {
    	// code to repeat goes here
    }
    

    For example, to print out "HELLO WORLD!" 7 times, you can write:

    for (int i = 1; i <= 7; i++) {
    	System.out.println("HELLO WORLD!");
    }
    

    (The variable i is a counter that starts at 1 and increments 1 each time the loop repeats while i is less than or equal to 7.)

    Modify your main method so it prints out 10 random phone numbers.

HANDIN

At the end of lab, create a zip file of your program and submit it to the handin server http://handin.intro.cs.cmu.edu/v1.