我必须为自己的课程创建一个地震监测系统。我有一个名为“地震”的类,其中有代码可以存储和检索事件的大小,位置和年份。我的下一个课程是“天文台”,在那里我有代码检索天文台的名称,位置,开始的年份和区域。此类还列出了已记录的所有地震事件(通过从上一类中导入“地震”数组列表)。
但是,我不知道如何编写一种方法来返回天文台记录的最大地震,因为此信息存储在“地震”类而不是“天文台”类中。 SOS?
Java,在BlueJ上编码。
地震课:
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;
}
天文台:
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?
}
但是如何获取有关最大地震的数据?
想法:这有意义吗?它不起作用,并说找不到“ .size” ...
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;
}
}
}
为此,我期望代码返回最大强度的整个地震对象。但这不起作用。
最佳答案
您在构造函数中初始化recordedEarthquakes的方式也应该是错误的
`recordedEarthquakes = new ArrayList<>();`
公众地震getLargestMagnitude(双震级)
{
地震最大震级;
对于(int i = 0; i {
地震EarthQuake =记录的Earthquakes.get(i);
如果(earthQuake.getMagnitude()>幅度){
MaximumMagnitude = EarthQuake;
}
}
返回maximumMagnitude;
}