不确定如何创建默认构造函数

时间:2014-11-04 07:32:18

标签: java constructor

我在编写这个类的无参数默认构造函数时遇到了麻烦。构造函数应该进入:

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;



        }
}

2 个答案:

答案 0 :(得分:0)

如果您要询问无参数构造函数的内容,您有两个选择:

保持空白 - 所有属性都将初始化为默认值

public Track() 
{
}

使用一些默认值而不是filename,并使用它来初始化实例,就像在其他构造函数Track(String filename)中一样。你甚至可以从另一个中调用一个构造函数:

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

如果您需要无参数构造函数不抛出异常,则可以捕获从其他构造函数抛出的异常。

答案 1 :(得分: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似乎是合适的......没有任何track =)