Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
rwe214_prac3

rwe214_prac3

Ratings: (0)|Views: 7|Likes:
Published by Calvin Maree

More info:

Published by: Calvin Maree on Mar 21, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
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 files 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 files in one place you haveto
tar
your files. A quick tutorial on
tar
is given below.
Tar how-to
We use
tar
to archive multiple files in one file 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 files contained in a
tar
archive:
tar xvf archive.tar
You
must
use the format
<student#> prac<prac#>.tar
for your
tar
file 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 overflow.
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 file and thencomputes and prints the number of peaks in the terrain.
Input file format:
The first line of the input file specifies 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 file is called
two- peaks.txt 
and we view the file 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 files. You are given three files
onepeak.txt 
,
twopeaks.txt 
, and
edgecheck.txt 
.The answers for these data sets being
1
,
2
, and
2
peaks respectively.
edgecheck.txt 
testsif your program handles edges correctly. Your program must be able to read
any
file inthis format and produce the correct number of peaks (in the format shown below). Torun
Peaks
on this input file we shall run the command (note the redirect):
˜$ java Peaks < twopeaks.txtpeaks: 2
Note how much easier it is to use an input file 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 file containing the integers that willpopulate
a[]
.
See the example files on the course site. Your program should be ableto process any file in the given format. For the first input example your command andoutput should look like this:
$java TestHistogram 4 < histogram example1.txt2 3 1 4
2

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->