我在創建這個類的無參數默認構造函數時遇到了困難。構造函數應該進入:不知道如何創建一個默認構造函數
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;
}
}
'但沒有按」 t似乎工作,什麼都不起作用。 – 2014-11-04 07:33:25
我基本上沒有得到一個工作的構造函數,應該更清楚 – 2014-11-04 07:35:06