gpt4 book ai didi

java - 参数化构造函数错误

转载 作者:行者123 更新时间:2023-12-02 12:01:36 25 4
gpt4 key购买 nike

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

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];

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

关于java - 参数化构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47211115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com