RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lab11a
lab11a

  1. Look over the documentation for java.util.Random1. From the Code Pad, make a new Random object (remembering it in a variable), then ask that object for a random int in the range2 [0,100). Then, ask it for another random number in that range.
    Q: What is a true/false expression which gets a random number in [0,100) and tests whether it's less than 52?

  2. In a recent election, candidate A received 52% of the votes cast. Before the election, polls tried to predict the answer by asking a small sample of people how they were going to vote. Write a method which simulates a poll of n people. To simulate asking one person, we'll have the computer generate a number in [0,100), and if the answer is less than 52 we consider it a vote for candidate A; otherwise we consider it a vote for candidate B. It returns true if the sample indicates candidate A will win.

      /** Take a poll of n people (between two candidates),
       *  and return whether or not the poll indicates candidate A will win.
       *  (Candidate A is the choice 52% of the time.)
       */
      boolean poll( int n )
    

  3. Different polls might give different results. Write a method manyPolls which:

    Here is a sample output:
    A poll of 100 people predicts candidate B will win.
    A poll of 100 people predicts candidate B will win.
    A poll of 100 people predicts candidate A will win.
    A poll of 100 people predicts candidate A will win.
    A poll of 100 people predicts candidate A will win.
    A poll of 100 people predicts candidate B will win.
    A poll of 100 people predicts candidate A will win.
    A poll of 100 people predicts candidate B will win.
    A poll of 100 people predicts candidate B will win.
    A poll of 100 people predicts candidate B will win.
    
    Out of 10 polls, 4 predicted candidate A.
    

    Of course, while writing manyPolls, you might think to yourself “Gee, I wish there were a built-in Java method that took a single poll of n people, if I give it a number n”. But wait — such a method does already exist; you wrorte it in the previous step. So manyPolls just needs to call poll(100) over and over; manyPolls does not repeat any code already written inside poll.

  4. Factoring your code: Let's make two improvements to the above code, even though it won't change the results:

  5. If you have time:

    We're curious to find out how big a sample size will consistently give accurate results (say, 19 out of 20 polls would be correct — this gives a 95% confidence level, a commonly accepted value).

    Make a function which takes 20 polls of larger and larger sample sizes. Use repeated doublings: twenty polls of size 1, then twenty polls of size 2, then twenty polls of size 4, then twenty polls of size 8, then twenty polls of size 16, etc.. until more than 95% percent of the polls give the correct answer.

    You might want to change several things, in order to come to a good conclusion. In particular, your previous method should probably return its conclusion of how many polls predicted correctly, rather than print it. Also, you can remove the println statements in that previous function, since that's totally cluttering the screen without giving helpful info.


1 Note that the name “Random” is kinda misleading: objects of that class aren't themselves random; instead, they can generate random numbers. The name like “RandomNumberGenerator” would be more accurate, if a mouthful.      

2Remember, [0,100) means the range starting from 0 up to but not including 100 -- so 99 is the largest number in the range.      

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Nov.11 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme