[英]Parameterized Constructor Error
我的程序有错误。 它位于此处显示的参数化构造函数中。
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;
}
}
对于该程序的大部分内容,我已经掌握了。 但是,我跳过了带有数组的参数化构造函数,直到决定运行它为止。 我发现有一个错误,然后返回查看是否没有在参数化构造函数中正确复制该数组。 我想知道如何解决此问题,因为我找不到解决方案。
问题在下一行
sExams = new double[grades.length];
您正在覆盖参数,而不是调整成绩字段的大小。 如果您将其更改为
grades = new double[sExams.length];
然后,成绩字段将被正确地重新分配给sExam参数的数组大小。
您需要更改以下内容:
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());
这条线
sExams = new double[grades.length];
将破坏sExams的参数值。 sExams
是您的输入参数。 此外,尚未创建grades
,因此grades.length
将在NullPointerException上失败。 sExams.length可以,只要调用方为其提供一个值即可。 您想从sExams
创建grades
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.