Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
rwe214_prac3

# rwe214_prac3

Ratings: (0)|Views: 7|Likes:

### Availability:

See more
See less

03/21/2012

pdf

text

original

CS E214
Practical 32012
Complete the following questions from the Exercises section in the textbook,
In-troduction to Programming in Java
. Resources for certain questions, such as codelibraries you are asked to use and example input and output ﬁles which can be used fordevelopment of your programs can be found on the course website. You have to handin your answers to these questions. In order to keep all the ﬁles in one place you haveto
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.
1 Book 1.5.8 [10]
Write a program that reads in positive real numbers from the standard input and printsout their geometric and harmonic means.The
geometric mean
of
positive numbers
x
1
,x
2
,...,x
is
(
x
1
x
2
···
x
)
1
/N
.The
harmonic mean
of
positive numbers is
(1
/x
1
+ 1
/x
2
+
···
+ 1
/x
)
.Hint: For the geometric mean, consider taking logs to avoid numerical overﬂow.
2 Book 1.5.16 [20]
Given the positions and masses of a set of objects, write a program to compute theircentre-of-mass, or
centroid
. The centroid is the average position of the
objects,weighted by mass. If the positions and masses of the
i
th
object are given by
(
x
i
,y
i
,m
i
)
then the centroid
(
x,y,m
)
is given by
m
=
i
=1
m
i
, x
=
i
=1
m
i
x
i
/m, y
=
i
=1
m
i
y
i
/m.
1

3 Book 1.5.19 [20]
Write a program that takes as command-line arguments an integer
and a doublevalue
p
[0
,
1]
, plots
equally spaced points on the circumference of a unit circleand then with probability
p
for each pair of points draws a line connecting them.
4 Book 1.5.29 [20]
Suppose that a terrain is represented by a two-dimensional grid of elevation values (inmeters). A
peak
is a grid point whose four neighbouring cells are strictly lower. Gridpoints with fewer than four neighbours are on the edge of the grid and these pointsare not peaks. Write a program
Peaks
that reads a terrain from an input ﬁle and thencomputes and prints the number of peaks in the terrain.
Input ﬁle format:
The ﬁrst line of the input ﬁle speciﬁes the dimensions of a
×
matrix of double values, separated by tabs (or any number of spaces). Eachsuccessive line is one row of the matrix. If for example the input ﬁle is called
two- peaks.txt
and we view the ﬁle with the command
cat
we will see the following:
\$ cat twopeaks.txt5 510.17 9.80 10.3 10.5 9.710.70 11.90 11.02 10.98 10.1510.30 10.40 9.20 8.53 9.2610.23 9.58 9.76 11.20 9.0310.87 9.34 9.23 10.45 10.90
In this example there are two peaks:
11
.
90
and
11
.
20
. See the course website forexample ﬁ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
any
ﬁle inthis format and produce the correct number of peaks (in the format shown below). Torun
Peaks
on this input ﬁle we shall run the command (note the redirect):
˜\$ java Peaks < twopeaks.txtpeaks: 2
Note how much easier it is to use an input ﬁle rather than command line parameterswhen you have lots of values!
5 Book 2.1.19 [Optional]
Write a method
histogram()
that takes an integer
and an array
a[]
of
int
valuesas arguments and returns an array of length
whose
i
th entry (
0
i <
) is thenumber 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
as a command lineparameter and input an array by giving an input ﬁle containing the integers that willpopulate
a[]
.
See the example ﬁles on the course site. Your program should be ableto process any ﬁle in the given format. For the ﬁrst input example your command andoutput should look like this:
\$java TestHistogram 4 < histogram example1.txt2 3 1 4
2