【发布时间】:2017-02-24 13:10:33
【问题描述】:
我正在做作业,我很难在这里找到问题所在。我写的程序无法编译。请帮忙。作业详情:
“使用以下字段创建一个名为 Airport 的类:标识符。由纬度和经度组成的坐标(不要每个创建两个!!)。纬度为正表示位于赤道以北,为负表示位于赤道以北南半球。经度为负表示在西,经度为正表示在格林威治中线以东。磁变化也表示西为负,东为正。没有磁变化是可以的。海拔高度以英尺为单位。
添加一个静态方法,该方法接受四个双精度值,它们是两个偏移坐标(双纬度 1、双倍长 1、双纬度 2、双倍长 2),并使用实验室 05 中给出的公式返回以海里为单位的距离。例如圣地亚哥机场具有值 ID:SAN,Lat:32.7335556,Long:-117.1896667,Var:14,Elev:16.8' (http://www.airnav.com/airport/SAN) 该类应该有每个字段的访问器和修改器方法。"
我在这里完成了大部分工作,但我认为我需要在这里添加构造函数。请帮忙。
主类:
package lab06;
import javax.swing.JOptionPane;
public class Lab06
{
public static void main(String[] args) {
double number; // To hold the number
String input; // To hold user input
//Create two Airport objects.
Airport firstAirport = new Airport();
Airport secondAirport = new Airport();
// Get and store the coordinates for firstAirport.
input = JOptionPane.showInputDialog("Enter the first Latitude: ");
number = Double.parseDouble(input);
firstAirport.setLatitude(number);
input = JOptionPane.showInputDialog("Enter the first Longitude: ");
number = Double.parseDouble(input);
firstAirport.setLongitude(number);
input = JOptionPane.showInputDialog("Enter the first Elevation: ");
number = Double.parseDouble(input);
firstAirport.setElevation(number);
// Get and store the coordinates for secondAirport.
input = JOptionPane.showInputDialog("Enter the second Latitude: ");
number = Double.parseDouble(input);
secondAirport.setLatitude(number);
input = JOptionPane.showInputDialog("Enter the second Longitude: ");
number = Double.parseDouble(input);
secondAirport.setLongitude(number);
input = JOptionPane.showInputDialog("Enter the second Elevation: ");
number = Double.parseDouble(input);
secondAirport.setElevation(number);
}
// The Distance method calculates the distance in nautical miles
public static void getDistance(String[] args)
{
double R = 3440;
double dist = Math.sin(firstAirport.getLatitude())
* Math.sin(secondAirport.getLatitude())
+ Math.cos(secondAirport.getLatitude())
* Math.cos(firstAirport.getLatitude())
* Math.cos(firstAirport.getLongitude()
- secondAirport.getLongitude());
dist = Math.acos(dist);
dist = dist * R;
// Display result in nautical miles.
JOptionPane.showMessageDialog(null,
"The distance in nautical miles is: %.1f\n" + dist);
System.exit(0);
}
}
还有 Airport 类 ....
package lab06;
public class Airport
{
public double latitude;
public double longitude;
public double elevation;
//The setLatitude method stores a value in the latitude field.
public void setLatitude(double latitude)
{
this.latitude = latitude;
}
//The setLongitude method stores a value in the longitude field.
public void setLongitude(double longitude)
{
this.longitude = longitude;
}
//The setElevation method stores a value in the elevation field.
public void setElevation (double elevation)
{
this.elevation = elevation;
}
//The getLatitude method returns an Airport object's latitude.
public double getLatitude()
{
return latitude;
}
//The getLongitude method returns an Airport object's longitude.
public double getLongitude()
{
return longitude;
}
//The getElevation method returns an Airport object's elevation.
public double getElevation()
{
return elevation;
}
}
【问题讨论】:
-
我将问题解读为“它无法编译,我不会费心阅读错误消息或将其提供给您。我希望您为我完成所有工作”。跨度>
-
为什么不能编译?当您尝试编译它时,具体会发生什么?有什么东西停止它的编译吗?那是什么东西?
-
不要贴这么多代码。发布minimal reproducible example:发布最低限度的内容以显示您的问题。
-
主类getDistance方法出现错误,提示:Cannot find variable firstAirport, secondAirport。创建两个 Airport 对象时,如何访问存储在 Airport 类中的变量?
标签: java class variables methods arguments