Results 1 to 8 of 8
Thread: WildLifeAnalyser Help Please!!!
- 01-12-2010, 01:00 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
WildLifeAnalyser Help Please!!!
Hello Guys... I am trying to learn java but its really hard:confused:... I will be glad if you help me on my project...
here is my project... Thanks...
Serengeti Wildlife Census
Park rangers in the Serengeti are interested in monitoring wildlife populations as well as their grazing patterns. To do this, the park rangers have tagged a sample of the animal population with GPS radio devices which can record interesting information. This information includes the map location (x, y coordinates), unique ID, animal type (e.g rhino, monkey, giraffe etc) and gender (male or female). The GPS census data is recorded by a central computer and saved in a log file.
Here is an example of the log file containing the census data (you can assume that the name of the file is always census.data):
1742383761,rhino,m,371,-175
594882222,impala,f,-180,-464
1269427866,elephant,f,90,524
1762479409,zebra,m,-693,272
109263160,giraffe,f,87,-545
1241420075,giraffe,m,33,660
1568438268,hippopotamus,f,-9,581
1434398988,elephant,f,20,519
Your task is to create an application called WildlifeAnalyser which reads the log file and, for each type of animal, produces a summary of:
* total population
* average distance
* ratio of males versus females (expressed as a percentage)
A java application called Plotter.java puts the animal locations onto a map and has been provided to you. However, you need to pass it an ArrayList containing ArrayLists which contain Animals (this will be explained in the lecture). You should not modify Plotter.java. You should also download Animal.java which you should use as an user defined class for each animal. Once you read the census data and put each animal into ArrayLists, you should see something like this:
Given files:
Plotter.java
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;
public class Plotter extends JFrame {
private static final long serialVersionUID = 1L;
private ArrayList<ArrayList<Animal>> censusData;
public Plotter(ArrayList<ArrayList<Animal>> censusData) {
this.censusData = censusData;
setSize(800, 800);
setVisible(true);
}
public void paint(Graphics gfx){
//plot points
for(ArrayList<Animal> animals : censusData){
int r = (int)(Math.random() * 255);
int g = (int)(Math.random() * 255);
int b = (int)(Math.random() * 255);
Color c = new Color(r, g, b);
for(Animal a : animals){
gfx.setColor(c);
gfx.fillOval(a.getX(), a.getY(), 8, 8);
if(a.getGender().equals("m")){
gfx.drawOval(a.getX() - 2, a.getY() - 2, 12, 12);
}
}
}
}
}
Animal.java
public class Animal {
private int x, y, id;
private String gender, type;
public Animal(int x, int y, int id, String gender, String type) {
this.x = x;
this.y = y;
this.id = id;
this.gender = gender;
this.type = type;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String toString(){
return id + "," + type + "," + gender + "," + x + "," + y;
}
}
- 01-12-2010, 01:25 PM #2
Senior Member
- Join Date
- Sep 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Is there a question coming any time soon? And if the question is "How do I do this?", then don't bother.
- 01-12-2010, 01:33 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
yes ofcourse... I need ideas... pls share your ideas with me... thanks
- 01-12-2010, 01:47 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
This is the code i have written so far... ofcourse doesnt work properly...
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class WildlifeAnalyser extends Animal {
double tokenNum = 0;
static int x;
static int y;
static int id;
int animalNum = 0;
int animal2 = 0;
static String gender;
static String type;
private static ArrayList<ArrayList<Animal>> censusData;
private static ArrayList<Animal> animal;
public WildlifeAnalyser(int x, int y, int id, String gender, String type)
throws FileNotFoundException {
super(x, y, id, gender, type);
HashMap<String, Integer> wordcount = new HashMap<String, Integer>();
ArrayList<ArrayList<Animal>> censusData;
censusData = new ArrayList<ArrayList<Animal>>();
animal = new ArrayList<Animal>();
Scanner input = new Scanner(new FileReader("census2.data"));
String str = "";
StringTokenizer st = null;
while (input.hasNextLine())
{
str = input.nextLine();
st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
tokenNum++;
setId(Integer.parseInt(st.nextToken()));
setType(st.nextToken());
setGender(st.nextToken());
setX(Integer.parseInt(st.nextToken()));
setY(Integer.parseInt(st.nextToken()));
Animal wild = new Animal(getId(),getX(), getY(), getGender(),
getType());
animal.add(wild);
censusData.add(animal);
// System.out.println(wild);
if (wordhm.containsKey(getType())) {
animalNum++;
wordhm.put(getType(), wordhm.get(getType()) + 1);
}
else {
animal2++;
// wordhm = new HashMap<String, Integer>();
wordhm.put(getType(), 1);
}
for (String s : wordhm.keySet()) {
if (wordhm.get(s) == wordhm.size()) {
System.out.println("The number of " + s + ": "
+ wordhm.size());
}
}
}
}
}
public static void main(String[] args) throws FileNotFoundException {
new WildlifeAnalyser(id, x, y, gender, type); new
Plotter(censusData);
}
}
- 01-12-2010, 02:08 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 13
1.) Use code tags when posting code.
2.) Prefer the String.split method for splitting strings. See the latest API specs for StringTokenizer to see why you should avoid it.
3.) Break down the application into several parts implemented as several classes and methods. Don't try to do everything in one huge constructor.
4.) When you ask for help, describe the exact problems you are having (error mesages,exception traces e.t.c). Don't just say "doesnt work properly...".
- 01-12-2010, 02:27 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
sorry about that... since i am new, i dont know the rules...
My problem is that after reading the file by using string tokenizer, I want to store them into a hash map and later on put them in an arrayList of arrayList coz in the plotter file arraylist of arraylist was used...
- 01-12-2010, 02:35 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 13
You didn't read all of my response above, did you?
- 01-12-2010, 02:38 PM #8
Senior Member
- Join Date
- Sep 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Bookmarks