2016-10-15 77 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