2017-11-09 18 views
0

私のプログラムにエラーがあります。ここに示すパラメータ化されたコンストラクタにあります。プログラムのほとんどの部分についてはパラメータ化されたコンストラクタエラー

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; 





} 

}

、私はそれを得ています。しかし、私はそれを実行することを決定するまで、私は配列とパラメータ化されたコンストラクタをスキップしました。エラーがあり、パラメータ化されたコンストラクタで配列を正しくコピーしていないことがわかりました。私は解決策を見つけることができなかったので、これを修正する方法を知りたい。あなたは、以下の変更が必要

+0

クラス全体を表示します。例えば、「成績」の定義は何ですか? – Andre

+0

@Andreもっと鮮明な画像のためにクラス全体を追加しました – Ryan

+0

2つのバグのいずれかを削除したため、あなたの編集内容が変更されました。 –

答えて

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()); 
1

proble mは次の行にあります。

グレードフィールドのサイズを変更する代わりに、パラメータを上書きしています。

grades = new double[sExams.length]; 

次に、グレードフィールドは、正確にsExamパラメーターの配列サイズに再割り当てされます。

+1

'sExams'と' length'の間に余分なスペースがあります –

+0

問題ありません。 –

+0

@DawoodibnKareemそれは確かです。 –

0

このライン

sExams = new double[grades.length]; 

はsExamsのパラメータ値を破壊します。入力パラメータはsExamsです。また、gradesはまだ作成されていないので、grades.lengthはNullPointerExceptionで失敗します。呼び出し元が値を提供する限り、sExams.lengthは大丈夫です。 sExamsからgradesを作成したいとします。

関連する問題