[英]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.