I currently have two ArrayLists that are not linked, though in order to run some of the methods I require, I believe I may need to nest one ArrayList within the object that fills the other ArrayList.
My two lists are:
public static ArrayList<Earthquake> quakeList = new ArrayList<>();
public static ArrayList<Observatory> obsList = new ArrayList<>();
where:
public Earthquake(String n, String obs, double m, int y, double lat, double lng) {
magnitude = m;
setQuakeYear(y);
setLatitude(lat);
setLongitude(lng);
setQuakeName(n);
setObsName(obs);
}
and:
public Observatory(String n, String c, int y, double a) {
this.setObsname(n);
this.setObscountry(c);
this.setObsyear(y);
this.setObsarea(a);
avgMag = (averageEarthquake());
}
Which contain data on earthquakes and observatories respectively. My problem comes when trying to link the Earthquake data with a specific observatory, as I will need this functionality in order to return the Observatory with the largest AVERAGE earthquake magnitude on record.
I have a method to return the average Earthquake size at the observatory:
public double averageEarthquake() {
Observatory o = new Observatory();
int i = 0;
double sum = 0.0;
for(Earthquake quake : rcdQuakes) {
i++;
sum += quake.magnitude;
}
o.avgMag = sum / i;
return sum / i;
}
(rcdQuakes is a new ArrayList that am trying to add only earthquakes where obsName matches the Observatory.obsname)
Using the comparator as follows to order the obsList by averageEarthquake:
class ObsObsComp implements Comparator<Observatory>{
@Override
public int compare(Observatory o1, Observatory o2) {
if (o1.averageEarthquake() > o2.averageEarthquake()) {
return -1;
}
if (o1.averageEarthquake() < o2.averageEarthquake()) {
return 1;
}
else {
return 0;
}
}
}
The method I am calling to do this (in the IO main method) is:
public static Earthquake obsLargestAvg() {
Collections.sort(obsList, new ObsObsComp());
return obsList.get(0);
}
In which I am hoping to order the obsList by highest averageEarthquake (avgMag in Observatory object). Of course, this isn't working right now. I am hope to find some way of connecting Earthquake.obsName and Observatory.obsname so that I can find the average of Observatory.o1 and compare it with the average of Observatory.o2, returning the obsname with the highest.
I apologise if this is confusing and will provide more info if required - Java is new to me and perhaps I have overcomplicated things a little. Any help will be greatly appreciated!
EDIT***
I have changed my code so that the Earthquake object takes Observatory object as a field rather than just obsName. So:
public Earthquake(String n, Observatory obs, double m, int y, double lat, double lng)
I now need to populate rcdQuakes with a list of discrete Earthquakes of obs "o1" to run the average, assigning the average to the observatory Object itself. Are there any suggestions on how to populate the list with just earthquakes of a single observatory each time?