1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall 2002 Problem Set 8

advertisement
1.00/1.001 Introduction to Computers and Engineering
Problem Solving
Fall 2002
Problem Set 8
Due: Day 29
Problem 1. Linked List Application (100%)
A preliminary step in establishing a natural reserve for an endangered species is the
estimation of its foraging area. Let us take ospreys as an example. Due to the use of DDT
and other poisonous insecticides, ospreys have been dying, prompting conservationists to
worry about their extinction. In an attempt to keep these species alive and make this
world more diverse and beautiful, biologists are hired to study their behavior. In order to
study their average foraging range, transmitters are tied to the captured ospreys before
they are freed. Each osprey is assigned a unique ID number. Sample positions are taken
periodically and recorded. You can find this data in the file called data.txt. The first
number is the ID of the osprey and the other two numbers denote its position when the
sample was taken. Universal Transverse Mercator (UTM) co-ordinates are used for
recording each entry. In UTM coordinates, northing and easting is used for a particular
zone. To simplify the problem, we will assume our study is only within one particular
zone, so that you only need to worry about the northing and easting coordinates. You can
look at them as if they are the y, and x in Cartesian coordinates. In the data, easting is
recorded first and then the northing. For example:
109 58734 24387
109 is the ID of the osprey. 58734 is the easting (x) coordinate. 24387 is the northing (y)
coordinate.
For this problem, you need to write 3 Classes. They are:
Sighting.java
Osprey.java
PS8Main.java
Sighting.java
An instance of this class is used to represent each osprey sighting. This class has two data
fields—one for easting and one for northing. Write a constructor that takes in two
arguments and sets the value for the two data fields. Implement the following three
methods in this class:
public void print() //prints the coordinates
public static double distance(Sighting p1, Sighting p2)
//calculate the distance between position p1 and position p2.
public static double area(Sighting A, Sighting B, Sighting C)
//calculates the area of the triangle with vertices A, B, and C
At the end, write a main() method in this class to test if your area function gives the right
output. Use the following co-ordinates for A, B and C.
Co-ordinate
Easting
Northing
A
100
100
B
100
200
C
200
100
The Area of this triangle should be 5000.0
Osprey.java
An instance of this class is used to represent an osprey. This class should have two data
fields. One is the ID number of the osprey and the other is a linked list of sightings.
Please use the SLinkedList presented in class as the linked list. Write a constructor for
this class. Depending on your preference, you could take in one argument or multiple
arguments. Provide get methods for both data fields.
Write three methods for this class:
public void print() : print out the ID and all the sightings in the linked list
public void addSighting(Sighting p) : add a sighting item to the linked list
public void analyze() : check if there are more than two sightings collected for this
one osprey. If there are, then an area can be computed, otherwise give out a message
saying there is insufficient data. We will assume here that all the sighting data collected is
unique and will form a convex polygon, and that no sighting will fall inside the polygon
(i.e., if we have 5 sightings, it will form a pentagon and not a quadrilateral with one
sighting that falls within the quadrilateral). We assume that the sighting data forms a
convex polygon. We further assume that the sightings are in counter-clockwise order in
the data file. For example, for the pentagon on the next page, the order in which we will
read in sightings from the file will be p0, p1, p2, p3, and last p4.
PS8Main.java
In main(), read in data from data.txt, and create a linked list of ospreys. (Again, use the
SLinkedList presented in class.)
To read in data from file,
First create a File object using the complete path of your file by doing
File myfile =
new File("C:\\1.00 Problem Sets\\ProblemSet8", "data.txt");
/* if you have saved data.txt in the following folder C:\1.00 Problem
Sets\ProblemSet8 */
Then create a reader that reads the file by doing
BufferedReader reader = new BufferedReader(new FileReader(myfile));
If you don’t use a full path, you will discover that Forte may not be able to find your file.
The readLine() function in the BufferedReader class will read in one full line of text.
A StringTokenizer object will help you read in each data element that is separated by
space. Remember that everything you read in is of type String, and you would need to
convert it to an int in order to use the methods in the two other classes. (Please consult
the Java® API Documentation to find out which methods you should use, and how to use
them.)
After you have read in all the data and put them in the linked lists, go through the linked
list and print out the id number and list of sightings and the foraging area for each osprey.
Useful area formulas:
A
Triangle:
Area = 0.5bc sin A
b2 + c2 − a2
cos A =
2bc
therefore

 b2 + c2 − a2
Area = 0.5bc sin  arccos
2bc


c
B
b
C
a

 


Area of the pentagon
= area of triangle(P0, P1, P2) +
area of triangle(P0, P2, P3) +
area of triangle(P0, P3, P4)
P4
P0
Area of a n-sided polygon
P3
P2
i = n −1
=
∑ area _ of _ Triangle( P , P , P
i =1
0
i
i +1
)
P1
Extra Credit(15%)
Let us assume that the sightings for each Osprey are randomly recorded, i.e., the vertices
are not recorded in counter clockwise fashion. Change your code such that you will still
be able to calculate the correct foraging area for each Osprey.
Turnin
Turnin Requirements
•
Hardcopy and electronic copy of ALL source code (all .java files). Remember to
comment your code, points will be taken off for insufficient comments.
•
Place a comment with your name, username, section, TA's name, assignment number,
and list of people with whom you have discussed the problem set on ALL files you
submit.
•
Do NOT turn in electronic or hardcopies of compiled byte code (.class files).
Electronic Turnin
Use SecureFX (or another secure ftp or secure shell program) to upload your problem set
to your 1.00 homework locker.
Detailed instructions of how to upload are on the course website.
Since your problem set is due at the beginning of lecture, your uploaded problem should
have a timestamp of no later than morning on the due date.
Penalties
•
Missing Hardcopy: -10% off problem score if missing hardcopy.
•
Missing Electronic Copy: -30% off problem score if missing electronic copy.
•
Late Turnin: -30% off problem score if 1 day late. More than 1 day late = NO
CREDIT.
If your problem set is late, or if a professor has granted you an extension in advance, do
not submit a printed copy of your problem set.
Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other
countries.
Download