Answer to Question #63165 in Java | JSP | JSF for SJ

Question #63165
I've been set the task of creating a basic earthquake monitoring program.

The classes for the program are:

Earthquake: store and retrieve data on magnitude, position (latitude & longitude) and year.

Observatory: store and retrieve data on name of observatory, country in which it's located, year in which observations began, area covered by observatory (in sq/kms) and list of earthquakes recorded by it.

Includes methods to return: the largest magnitude earthquake recorded by the observatory, the average earthquake magnitude recorded by the observatory, a list of all methods recorded by the observatory with a magnitude greater than a given number.

Monitoring: Holds information about all observatories. Includes methods to return: the observatory with the largest average magnitude, the largest magnitude earthquake ever recorded, a list of all earthquakes recorded with a magnitude greater than a given number

Any help would be hugely appreciated.
1
Expert's answer
2016-11-22T12:57:14-0500
import java.util.ArrayList;

public class Monitoring {
private ArrayList<Observatory> observatoryList;

public Monitoring() {
observatoryList = new ArrayList<>();
}

public Observatory getAverageMax() {
double x = 0.0;
Observatory ans = null;
for (Observatory o: observatoryList) {
if (x < o.getAverage()) {
x = o.getAverage();
ans = o;
}
}

return ans;
}

public int largestMagnitude() {
int ans = 0;

for (Observatory o: observatoryList) {
ans = Math.max(ans, o.largestMagnitude());
}

return ans;
}

public ArrayList<Earthquake> greater(int n) {
ArrayList<Earthquake> ans = new ArrayList<>();

for (Observatory o: observatoryList) {
ArrayList<Earthquake> earthquakes = o.greater(n);

for (Earthquake e: earthquakes) {
ans.add(e);
}
}

return ans;
}
}

class Earthquake {
private int magnitude;
private double latitude;
private double longitude;
private int year;

public Earthquake(int newMagnitude, double newLatitude, double newLongitude, int newYear) {
magnitude = newMagnitude;
latitude = newLatitude;
longitude = newLongitude;
year = newYear;
}

public int getMagnitude() {
return magnitude;
}

public double getLatitude() {
return latitude;
}

public double getLongitude() {
return longitude;
}

public int getYear() {
return year;
}
}

class Observatory {
private String name;
private String country;
private int year;
private double area;
private ArrayList<Earthquake> earthquakeList;

public Observatory(String newName, String newCountry, int newYear, double newArea) {
name = newName;
country = newCountry;
year = newYear;
area = newArea;

earthquakeList = new ArrayList<>();
}

public String getName() {
return name;
}

public String getCountry() {
return country;
}

public int getYear() {
return year;
}

public double getArea() {
return area;
}

public void addEarthquake(Earthquake e) {
earthquakeList.add(e);
}

public int largestMagnitude() {
int ans = 0;

for (Earthquake e: earthquakeList) {
ans = Math.max(ans, e.getMagnitude());
}

return ans;
}

public double getAverage() {
double s = 0.0;

for (Earthquake e: earthquakeList) {
s += e.getMagnitude();
}

return s / earthquakeList.size();
}

public ArrayList<Earthquake> greater(int n) {
ArrayList<Earthquake> ans = new ArrayList<>();

for (Earthquake e: earthquakeList) {
if (e.getMagnitude() > n) {
ans.add(e);
}
}

return ans;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS