2014-11-04 98 views
-2

我在創建這個類的無參數默認構造函數時遇到了困難。構造函數應該進入:不知道如何創建一個默認構造函數

public Track(){ 

    } 

下面的代碼是我工作的整個班級中,我使用從下面的方法返回的值,並將其設置爲0試過但好像沒有不工作。任何幫助將不勝感激。

package comp125; 

import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Scanner; 


public class Track { 

     //removed max entries (was 1000, now has no limit) 

     ArrayList<Waypoint> pointList = new ArrayList<Waypoint>();{ 
       //for (int i = 0; i < pointList.size(); i++){ 
         //System.out.println(pointList.get(i)); 
       //} 
     } 
     Scanner scanner; 
     String fileMain; 


     public Track(String filename) throws IOException, GPSException { 

       Scanner scanner = new Scanner(new FileReader(filename)); 



       scanner.hasNextLine(); 

       while(scanner.hasNextLine()) { 



         String line = scanner.nextLine(); 

         pointList.add(new Waypoint(line)); 



       } 

       File f = new File(filename);       //easy filenotfound exception 
       if(!f.exists()){ 
         throw new IOException(); 
       } 

       fileMain = filename; 




     } 

     //This is where we create an empty track 
     public Track(){ 




     } 

     public int size() { 

       return pointList.size(); 

     } 

     public void add(Waypoint wp) { 


       pointList.add(pointList.size(), wp); 


     } 

     public String getFilename() { 

       return fileMain; 


     } 

     public String getTimestamp() { 


       return pointList.get(0).getTimestamp(); 
     } 

     public double getDistance() { 

       double totDist = 0.0; 

       for (int i = 1; i < pointList.size(); i++) 
       { 

         totDist = totDist + pointList.get(i-1).distanceTo(pointList.get(i)); 

       } 

       //System.out.println(totDist); 
       return totDist; 




     } 


     public double getElevationGain() { 
       double elevGain = 0.0; 


       for(int i = 1; i < pointList.size(); i++){ 
         if(pointList.get(i).getElevation() > pointList.get(i-1).getElevation()){ 
           elevGain = elevGain + Math.abs(pointList.get(i).getElevation() - pointList.get(i-1).getElevation()); 
         } 
       } 


       return elevGain; 
     } 


     public String toString() { 

       String str1 = this.getFilename(); 
       String str2 = this.getTimestamp(); 
       String str3 = String.valueOf(this.getDistance()); 
       String str4 = String.valueOf(this.getElevationGain()); 
       //System.out.println(str1); 
       //System.out.println(str2); 
       //System.out.println(str3); 

       return str1 + str2 + str3 + str4; 

     } 


     public Waypoint closestTo(Waypoint wp) { 

       Waypoint returnValue = pointList.get(0); 
       for(int i = 1; i < pointList.size(); i++){ 
         if(pointList.get(i).distanceTo(wp) < returnValue.distanceTo(wp)){ 
           returnValue = pointList.get(i); 
         } 
       } 

       return returnValue; 



     } 
} 
+0

'但沒有按」 t似乎工作,什麼都不起作用。 – 2014-11-04 07:33:25

+0

我基本上沒有得到一個工作的構造函數,應該更清楚 – 2014-11-04 07:35:06

回答

0

如果你問什麼投入無參數的構造函數,你有兩個選擇:

保持它的空 - 所有屬性將被初始化爲默認值

public Track() 
{ 
} 

使用一些默認值而不是filename,並使用它來初始化實例,就像在其他構造函數Track(String filename)中那樣。你甚至可以從另一個調用一個構造函數:

public Track() throws IOException, GPSException 
{ 
    this("DEFAULT_FILE_NAME"); 
} 

如果你需要一個參數的構造函數拋出沒有例外,可以捕獲從另一個構造函數拋出的異常。

0

我覺得你正在嘗試使用這個類,但是沒有數據。

你可以像這樣製造假(模擬)數據:

public Track(){ 
    for (int i = 0; i < 10; i++){ 
     pointList.add(new Waypoint(/* put the data here that you need for the Waypoint constructor */); 
    } 
} 

但在構造您的評論//This is where we create an empty track似乎是恰當的......沒有任何軌道=)

相關問題