2016-10-15 74 views
-1

我正在做我的功课,我很难在这里找到问题。我写的程序不能编译。请帮忙。作业详细信息:如何从包(java)中的其他类访问主类中的变量?

“创建一个名为Airport的类,其中包含以下字段:标识符。包含经度和纬度的坐标(不要创建每个!!的两个)。纬度为正值表示赤道北,负值当它位于南半球时,经度为负值,表示为西,正值表示它是格林威治中位数的东部,磁性变化也表示西方为负,东方为正值,没有磁性变化是可以的。海拔高度以英尺为单位

添加一个静态方法,该方法接受四个双精度坐标(双lat1,double long1,double lat2,double long2),并返回海里距离,使用给出的公式实验室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); 
    } 
} 

和机场类....

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; 
    } 
} 
+1

我读了一个问题,“这并不编译,我不能打扰阅读错误消息或把它给你我希望你能为我做所有的工作“。 – John3136

+0

*为什么*不编译?当你尝试编译它时,会发生什么?是否有东西*阻止它从编译?那是什么? – David

+0

不要发布这么多的代码。发布[MCVE]:发布最低限额以显示您的问题。 – c0der

回答

0

如何访问变量在主类从另一个类的包(Java)的?

我相信你是问有关访问的主要方法>> < <声明的局部变量。简单的答案是你不能。

但是,您可以将作为方法或构造函数参数传递给另一个类。

我在这里做了大部分工作,但我想我需要在这里添加构造函数。

是的。这将是一个好主意。

请帮

提示:看了你的讲义/教科书/如何编写构造一个在线Java教程。

+0

感谢您的帮助Stephen。 –

0

我读到这个问题的方式,静态方法应该与数据一起提供,而不知道它是计算机场之间的距离。如果意图是这样做的话,那么它需要接受两个机场对象,而不是所描述的4个双打对象。

注意:一般来说,避免使任何可变数据静态可用(除了缓存)。

+0

这篇文章不是回答这个问题的实际尝试。请注意[Stack Overflow不像讨论论坛](http://stackoverflow.com/about),它是一个问答网站,每个帖子都是问题或问题的答案。帖子也可以有[评论](http://stackoverflow.com/help/privileges/comment) - 这样的小句子 - 可以用来批评或请求作者澄清。这应该是一个评论或[新问题](http://stackoverflow.com/questions/ask)。 –

0

下面是如何通过3个双打Airport一个例子:

public class Airport { 

    public double latitude; 
    public double longitude; 
    public double elevation; 

    public Airport(double latitude, double longitude, double elevation) { 

     this.latitude = latitude; 
     this.longitude = longitude; 
     this.elevation = elevation; 

    } 

    //if you need to access variables you add get methods like: 
    public double getLatitude(){ 
     return latitude; 
    } 

    public static void main(String[] args) { 

     Airport ap = new Airport(30.34567, 27.6789, -140); 
     System.out.println("Airport latitude is "+ ap.getLatitude()); 
    } 

} 
+0

要求用户输入坐标。 –

+0

该程序的用户被要求输入坐标... –

+0

发布[MCVE]。另请参阅:http://stackoverflow.com/help/someone-answers – c0der