简体   繁体   中英

How to write a for-loop on an ArrayList to find a maximum value

I have to create an earthquake monitoring system for my course. I have one class named 'Earthquake' where I have code to store and retrieve magnitude, position and year of the event. My next class is 'Observatory', where I have code to retrieve observatory name, location, year it began and area. This class also lists all earthquake events it has recorded (by importing the 'Earthquake' Arraylist from the previous class).

However, I don't know how to write a method to return the largest magnitude earthquake recorded by the observatory, as this information is stored in the Earthquake class not the Observatory class. SOS?

Java, coded on BlueJ.

EARTHQUAKE CLASS:

public class Earthquake
{
/* instance variables - information about the magnitude, position and year of the event*/
private double magnitude;
private double latitude;
private double longitude;
private int year;

public Earthquake(double magnitude, double latitude, double longitude, int year) // initialise instance variables
{
    this.magnitude = magnitude;
    this.latitude = latitude;
    this.longitude = longitude;
    this.year = year;

}


public double getMagnitude()
//method to get magnitude data
{
    return magnitude;
}

public double getLatitude()
//method to get latitude data
{
    return latitude;
}

public double getLongitude()
//method to get longitude data
{
    return longitude;
}

public int getYear()
//method to get year data
{
    return year;
}

OBSERVATORY CLASS:

import java.util.ArrayList;
public class Observatory
{
/* instance variables - name of observatory, country it is in, year in which earthquake observations started, area covered by observatory and list of earthquake events*/

private String name;
private String country;
private int yearFirstEarthquakeRecorded;
private double area;
private ArrayList<Earthquake> recordedEarthquakes;

public Observatory(String name, String country, int yearFirstEarthquakeRecorded, double area)
{
    // initialise instance variables
    this.name = name;
    this.country = country;
    this.yearFirstEarthquakeRecorded = yearFirstEarthquakeRecorded;
    this.area = area;
    ArrayList<Earthquake> recordedEarthquakes = new ArrayList<Earthquake>(); /*I assume this imports data from Earthquake class*/

}

public String getName()
{
    return name;
}

public String getCountry()
{
    return country;
}

public int getYearFirstEarthquakeRecorded()
{
    return yearFirstEarthquakeRecorded;
}

public double getArea()
{
    return area;
}

public void addEarthquake(Earthquake E)
{
    recordedEarthquakes.add(E); //allows earthquakes recorded to be added?
}

BUT HOW TO GET data about the largest magnitude earthquake?

IDEA: does this make sense? it doesn't work, and says '.size' cannot be found...

public Earthquake getLargestMagnitude(double magnitude)
{
    for (int i = 0; i < Earthquake.size(); i++) {
        if (Earthquake.getMagnitude(i) > magnitude) {
            return Earthquake;
        }
    }
}

public Earthquake getLargestMagnitude(double magnitude)
{
    for (int i = 0; i < Earthquake.size(); i++) {
        if (Earthquake.getMagnitude(i) > magnitude) {
            return Earthquake;
        }
    }
}

for this i am expecting the code to return the whole object of the Earthquake with the largest magnitude. but it doesn't work.

The way you initialized recordedEarthquakes in the constructor is also wrong it should be

`recordedEarthquakes = new ArrayList<>();`

    public Earthquake getLargestMagnitude(double magnitude)
    {
        Earthquake largestMagnitude;
        for (int i = 0; i < recordedEarthquakes.size(); i++) 
        {
            Earthquake earthQuake = recordedEarthquakes.get(i);
            if (earthQuake.getMagnitude() > magnitude) {
                largestMagnitude = earthQuake;
            }
        }
        return largestMagnitude;
    }
    

The coding issue is that you are not referencing the variable. Also tend not to name non-getter methods with a get.

You don't need the parameter since you are asking for the largest, not the first one greater than the parameter. So I removed it.

public Earthquake largestMagnitude()
{
    Earthquake largest = null;
    for (int i = 0; i < recordedEarthquakes.size(); i++) {
        if (largest == null || recordedEarthquakes.get(i).getMagnitude() > largest.getMagnitude()) {
            largest = recordedEarthquake.get(i);
        }
    }
    return largest;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM