Sei sulla pagina 1di 3

CS E214

Practical 2 2012
Complete the following questions from the Exercises section in the textbook, Introduction to Programming in Java. Resources for certain questions, such as code libraries you are asked to use and example input and output les which can be used for development of your programs can be found on the course website. You have to hand in your answers to these questions. In order to keep all the les in one place you have to tar your les. A quick tutorial on tar is given below.

Tar how-to
We use tar to archive multiple les in one le for convenience. For more information on tar see the man pages: man tar To create a tar archive: tar cvf archive.tar file1 file2 file3 To extract the les contained in a tar archive: tar xvf archive.tar You must use the format <student#> prac<prac#>.tar for your tar le name. Hand-in date: 23:59 Sunday 19 February 2012.

Marking scheme for practical assignments


The number of marks awarded to each question is given in square brackets. For each question, you will earn a mark of zero if your program does not work correctly for at least some of the sample input provided to the marker (no partial credit). If your program works correctly for a subset of the sample input, you will earn a subset of the available marks. If your program works correctly for all of the sample input, you may earn up to the maximum mark, depending on the style and readability of your code. The total mark for each practical will be rescaled to count out of 100. If you cannot complete all of the questions in the time you have set aside for the practical, it is up to you to optimise your mark by doing the easier questions and skipping the harder ones. Do not spend more time than you have available: if you do this, your performance in other subjects will suffer.

Book 1.3.9 [10]

Write a program AvgRand that takes an integer N as a command-line argument, uses Math.random() to print N uniform random values between 0 and 1, and then prints their average value. An example of what your command on the command-line and the program output should look like:
$ java AvgRand 5 0.23 0.5 0.18 0.84 0.20 Average: 0.39

Note that 5 is the command-line argument. The program output must print each random number on a new line and then print the average. The format should be exactly like shown above.

Web 1.3.42 [20]

Chaos. Write a program to study the following simple model for population growth, which might be applied to study sh in a pond, bacteria in a test tube, or any of a host of similar situations. We suppose that the population ranges from 0 (extinct) to 1 (maximum population that can be sustained). If the population at time t is x, the we suppose the population at time t + 1 to be rx(1 x) where the parameter r, sometimes known as the fecundity parameter, controls the rate of growth. Start with a small population, say x = 0.01 and study the result of iterating the model, for various values of r. For which values of r does the population stabilize at x = 1 1/r? Can you say anything about the population when r is 3.5? 3.8? 5? Biologists model population growth of sh in a pond using the logistic equation. Investigate some of its chaotic behavior. The fecundity parameter r is between 0 and 4 and x is initially chosen to be between 0 and 1. The iterates x settle into a denite pattern. This pattern is constant if r < 3. Then starts period doubling with a second bifurcation at r = 3.5, chaos shortly afterwards, and 3-step period around r = 3.8.

Web 1.3.14 [10]

Boys and girls. A couple beginning a family decides to keep having children until they have at least one of either sex. Estimate the average number of children they will have via simulation. Also estimate the most common outcome (record the frequency counts for 2, 3, and 4 children, and also for 5 and above). Assume that the probability p of having a boy or girl is 1/2. (The probability P of having n boys (girls) followed by a girl (boy) is P = 2pn (1p).)

Web 1.3.20 [10]

Alice tosses a fair coin until she sees two consecutive heads. Bob tosses another fair coin until he sees a head followed by a tail. Write a program to estimate the probability that Alice will make fewer tosses than Bob? Answer: 39/121.

Book 1.3.40 [20]

In the 1970s game show Lets Make a Deal, a contestant is presented with three doors. Behind one of them is a valuable prize. The contestant chooses a door, but does not open it. The host (who knows which door contains the prize) then opens one of the other two doors that does not contain the prize (there must be at least one such door) and shows the contestant that the prize is not there. The contestant is then given the opportunity to switch to the other unopened door. Should the contestant do so? Intuitively, it might seem that the contestants initial choice and the other unopened door are equally likely to contain the prize, so there would be no incentive to switch. Write a program GameSimulation that takes a command-line argument N , plays the game N times using each of the two strategies (switch or not switch), and prints the chance of success for each strategy.

Book 1.4.27 [30]

Alice is throwing a party with N other guests, including Bob. Bob starts a rumor about Alice by telling it to one of the other guests. A person hearing this rumor for the rst time will immediately tell it to one other guest, chosen at random from all the people at the party except Alice and the person from whom they heard it. If a person (including Bob) hears the rumor for a second time, he or she will not propagate it further. Write a program to estimate the probability that everyone at the party (except Alice) will hear the rumor before it stops propagating. Also calculate an estimate of the expected number of people to hear the rumor.

Potrebbero piacerti anche