参数化构造函数错误

时间:2017-11-09 20:38:04

标签: java

我的程序出错了。它位于此处显示的参数化构造函数中。

public class Student {
// instance variables
private String studentId;
private String firstName;
private String lastName;
private double [] grades;


/** 
 * default constructor
 * the id, first and last names are initialized to "none"
 * the array is instantiated to store 4 elements - each element is
 * initialized to -1.0
 */
public Student()
{
    studentId = "none";
    firstName = "none";
    lastName = "none";
    grades = new double [4];
    for(int i = 0; i < grades.length; i++)
    {
        grades[i] = -1.0;
    }
}

/** 
 * parameterized constructor
 * stores the parameters into the appropriate instance variables
 * @param sId the value to be stored in the instance variable studentId
 * @param sFirstName the value to be stored in the instance variable firstName
 * @param sLastName the value to be stored in the instance variable lastName
 * @param sExams the address of the array whose values will be copied into the 
 * instance variable grades
 */
public Student(String sId , String sFirstName , String sLastName , double[] sExams )
{
    studentId = sId;
    firstName = sFirstName;
    lastName = sLastName;
    sExams = new double[grades.length];
    for(int i = 0; i < grades.length; i++)
    {
        grades[i] = sExams[i];
    }




}

/**
 * setStudentId - mutator method for studentId
 * stores the parameter into the instance variable
 * @param sId the value to be stored in the instance variable studentId
 */
public void setStudentId(String sId)
{
    this.studentId = sId;
}

/**
 * setGrades - mutator method for grades
 * stores the parameter into the instance variable
 * @param sExams the address of the array whose values will be copied into the 
 * instance variable grades
 */
public void setGrades(double [] sExams)
{
    for(int i = 0; i < grades.length; i++)
    {
        grades[i] = sExams[i];
    }
}

/**
 * getStudentId - accessor method for id
 * @return a reference to the instance variable id
 */
public String getStudentId()
{
    return studentId;
}

/**
 * getFirstName - accessor method for firstName
 * @return a reference to the instance variable firstName
 */
public String getFirstName()
{
    return firstName;
}


/**
 * getLastName - accessor method for lastName
 * @return a reference to the instance variable lastName
 */
public String getLastName()
{
    return lastName;
}

/**
 * getGrades - accessor method for grades
 * @return a reference to a copy of the instance variable grades
 */
public double [] getGrades()
{
    double [] gradesCopy = new double [grades.length];
    for(int i = 0; i < grades.length; i++)
    {
        gradesCopy[i] = grades[i];
    }
    return gradesCopy;
}

/**
 * findLowestExam - find the lowest exam score in the array and returns its location 
 * in the array
 * @return the position of the lowest exam grade in the array
 */
public int findLowestExam()
{
    int lowestIndex = 0;
    for(int i = 1; i < grades.length; i++)
    {
        if(grades[i] < grades[lowestIndex])
            lowestIndex = i;
    }
    return lowestIndex;
}

/**
 * calcExamAverage - calculates the average of the exams in one of two ways 
 * if the parameter is true, the lowest exam score is dropped in 
 * calculating the average
 * if the parameter is false, no exams are dropped in the calculating
 * the average
 * @param drop - a boolean variable to specify whether or not to drop the lowest score
 * @return the average of the exams
 */

public double calcExamAverage(boolean drop)
{
    double sum = 0; 
    double average;
    if(drop == false)
    {
        for(int i = 0; i < grades.length; i++)
        {
            sum += grades[i];

        }
        average = sum / grades.length;
        return average;
    }

    else
    {
        for(int i = 0; i < grades.length; i++)
        {
            sum += grades[i];


        }
        average = (sum - grades[this.findLowestExam()]) / (grades.length - 1);
        return average;
    }

}
/**
 * toString - create and return a String with the instance variable values
 * @return a reference to a String containing the id, first and last names
 * and the exam grades
 */
public String toString()
{
    String str = "ID: " + studentId + "\n" + "Name: " + lastName + "," + firstName + "\n" + "Grades:";
    for(int i = 0; i < grades.length; i++)
    {
        str += grades[i] + " ";

    }
    return str;





}

}

对于程序的大部分内容,我已经把它搞定了。但是,我跳过了数组的参数化构造函数,直到我决定运行它。我发现有一个错误,并返回看到我没有在参数化构造函数中正确复制数组。我想知道如何解决这个问题,因为我找不到解决方案。

3 个答案:

答案 0 :(得分:1)

问题出在以下一行

sExams = new double[grades.length];

您正在编写参数,而不是调整成绩字段的大小。如果将其更改为

grades = new double[sExams.length];

然后,grade字段将被正确地重新分配到sExam参数的数组大小。

答案 1 :(得分:0)

您需要更改以下内容:

public Student(String sId , String sFirstName , String sLastName , double[] sGrades )
{
    studentId = sId;
    firstName = sFirstName;
    lastName = sLastName;
    grades = new double[sGrades.length];

    for(int i = 0; i < grades.length; i++)
    {
        grades[i] = sGrades[i];
    }
}

换句话说,你需要初始化grades,就像你在默认构造函数中做的那样,只使用传入的参数。在你当前的参数化构造函数中,你试图设置传入的参数。

此外,您可以将数组设置为:

grades = sGrades.clone();
// or
System.arraycopy(sGrades, 0, grades , 0, sGrades.length());

答案 2 :(得分:0)

这一行

sExams = new double[grades.length];

将销毁sExams的参数值。 sExams是您的输入参数。此外,尚未创建grades,因此grades.length将在NullPointerException上失败。只要调用者为它提供了值,sExams.length就可以了。您想从grades创建sExams