Programming Exercise 1


This exercise will get you started with programming in Java. For this exercise, you will need to download this template, and enter your code in the main method. You are welcome to create your own (static) methods if you like. You will also need to download a separate Java program - the file UniformRandom.java from the useful subdirectory (see the course homepage). Do not make changes to this file; simply let it reside in the same directory as your program (The compiler will look for it there). Before you write your first line of code, you ought to try compiling the template.

Consider the following experiment: suppose you create an integer array of size N with all elements initialized to 0. Now, you pick K positions at random in the array, and change the corresponding value at each of those positions to 1. What are the chances that all the 1's so generated are all in a row (i.e., that no two 1's have a 0 between them)? Note that it's possible to get fewer than K such 1's in the array (see below).

You will write code to estimate this probability using repeated trials of the experiment. The only thing you will need by way of random generation, is to be able to select an array index randomly. To do this, use the method uniform which takes integers i and j as parameters and returns a randomly selected integer between i and j.

There is one sublety to be aware of in selecting the k positions randomly in the array. Suppose you've selected the first position randomly, and it happens to be 2 for an array of size 10. Now, you pick a random array index again, and you once again get 2. Then, do you pick again, or use this value? We will adopt the policy of using this value: that is, simply overwrite the previous 1 written here. In this case, you might have fewer 1's than K; we will allow that to happen.

Deliverables:

Note:

Submission: