Вы находитесь на странице: 1из 3

CS E214

Practical 3 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 26 February 2012.

Book 1.5.8 [10]

Write a program that reads in positive real numbers from the standard input and prints out their geometric and harmonic means. The geometric mean of N positive numbers x1 , x2 , . . . , xN is (x1 x2 xN )1/N . The harmonic mean of N positive numbers is (1/x1 + 1/x2 + + 1/xN ). Hint: For the geometric mean, consider taking logs to avoid numerical overow.

Book 1.5.16 [20]

Given the positions and masses of a set of objects, write a program to compute their centre-of-mass, or centroid. The centroid is the average position of the N objects, weighted by mass. If the positions and masses of the ith object are given by (xi , yi , mi ) then the centroid (x, y, m) is given by
N N N

m=
i=1

mi ,

x=
i=1

mi xi /m,

y=
i=1

mi yi /m.

Book 1.5.19 [20]

Write a program that takes as command-line arguments an integer N and a double value p [0, 1], plots N equally spaced points on the circumference of a unit circle and then with probability p for each pair of points draws a line connecting them.

Book 1.5.29 [20]

Suppose that a terrain is represented by a two-dimensional grid of elevation values (in meters). A peak is a grid point whose four neighbouring cells are strictly lower. Grid points with fewer than four neighbours are on the edge of the grid and these points are not peaks. Write a program Peaks that reads a terrain from an input le and then computes and prints the number of peaks in the terrain. Input le format: The rst line of the input le species the dimensions of a N M matrix of double values, separated by tabs (or any number of spaces). Each successive line is one row of the matrix. If for example the input le is called twopeaks.txt and we view the le with the command cat we will see the following:
$ cat twopeaks.txt 5 5 10.17 9.80 10.3 10.70 11.90 11.02 10.30 10.40 9.20 10.23 9.58 9.76 10.87 9.34 9.23

10.5 10.98 8.53 11.20 10.45

9.7 10.15 9.26 9.03 10.90

In this example there are two peaks: 11.90 and 11.20. See the course website for example les. You are given three les onepeak.txt, twopeaks.txt, and edgecheck.txt. The answers for these data sets being 1, 2, and 2 peaks respectively. edgecheck.txt tests if your program handles edges correctly. Your program must be able to read any le in this format and produce the correct number of peaks (in the format shown below). To run Peaks on this input le we shall run the command (note the redirect):
$ java Peaks < twopeaks.txt peaks: 2

Note how much easier it is to use an input le rather than command line parameters when you have lots of values!

Book 2.1.19 [Optional]

Write a method histogram() that takes an integer M and an array a[] of int values as arguments and returns an array of length M whose ith entry (0 i < M ) is the number of times the integer i appeared in the argument array. Hint: There is a catch here. Do you know what it is? Write a test client for your method which allows the user to give M as a command line parameter and input an array by giving an input le containing the integers that will populate a[]. See the example les on the course site. Your program should be able to process any le in the given format. For the rst input example your command and output should look like this:
$java TestHistogram 4 < histogram example1.txt 2 3 1 4

2.1.22 [10]

Write a method any() that takes an array of boolean values as argument and returns true if any of the entries in the array is true, and false otherwise. Write a method all() that takes an array of boolean values as argument and returns true if all of the entries in the array are true, and false otherwise. To earn marks for this question, both of your methods should work. Write a test client that tests both of your methods which allows the user to input an array of booleans by giving an input le containing the booleans. See the example les on the course site. Yet again your program should be able to process any le in the given format.

2.1.34 [20]

The binomial distribution. Write a function


public static double binomial(int N, int k, double p)

to compute the probability of obtaining exactly k heads in N ips of a biased coin (heads with probability p) using the formula f (N, k, p) = pk (1 p)N k N !/(k!(N k)!) In main() take N and p from the command line. Your program must check that N k=0 f (N, k, p) 1. Hint: Prove that f (N, k, p) = p N k+1 f (N, k 1, p) 1p k (1)

where f (N, 0, p) = (1 p)N = (1 p)f (N 1, 0, p) and f (0, 0, p) = 1. Use Eqn. (1) to avoid numerical overow when computing the binomial distribution.

Вам также может понравиться