0

特定のモジュールに基づいて学生のマークを返す単純なアプリケーションに取り組んでいます。getModuleMarkモジュールインデックスを指定してモジュールマークを返す必要があるため、私の主な問題はメソッドにあります。

このsetModuleMarkメソッドでは、インデックス モジュールとマーク パラメータの両方を渡しました。モジュール マークの戻り値に何を入力する必要があるかについて、少し混乱しています。

アプリケーションを実行すると、次の出力が得られます。

ジョー・ブロッグス

モジュール 0: 50.0

モジュール 1: 50.0

モジュール 7: 50.0

以下のコードを参照してください。

public class Student {

    public static void main(String[] args) {

     Student student = new Student("Joe", "Bloggs"); 
   
    // Add some marks to the student. 
    student.setModuleMark(0, 10);   
    student.setModuleMark(1, 80);  
    student.setModuleMark(7, 50);
  
   
    // Display the marks.
    System.out.println(student.getForename() + " " +     student.getSurname());
    System.out.println("Module 0: " + student.getModuleMark(0));
    System.out.println("Module 1: " + student.getModuleMark(1));
    System.out.println("Module 7: " + student.getModuleMark(7));
      } 
    
  

    private String forename;
    private String surname;
    private double marks;

    public Student(String forename, String surname) {
      super();
      this.forename = forename;
      this.surname = surname; 
    
      double [] ModuleMark = new double [7]; //Creates array of fixed size 7
    }
  
    /**
     * @return the forename
     */
     public String getForename() { 
        return this.forename;
     }
  
     /**
      * @return the surname
      */
     public String getSurname() {
       return this.surname;
     }
  
     /**
      * @param marks the marks to set
      * @param i 
      */     
     public double getModuleMark (int in) {
       return this.marks;        
     }
  
     public void setModuleMark(int in, double marks) {
     
    this.marks = marks;
  }
}
4

5 に答える 5

1

公開クラス 生徒 {

public static void main(String[] args) {

 Student student = new Student("Joe", "Bloggs"); 

// Add some marks to the student. 
student.setModuleMark(0, 10);   
student.setModuleMark(1, 80);  
student.setModuleMark(7, 50);


// Display the marks.
System.out.println(student.getForename() + " " +     student.getSurname());
System.out.println("Module 0: " + student.getModuleMark(0));
System.out.println("Module 1: " + student.getModuleMark(1));
System.out.println("Module 7: " + student.getModuleMark(7));
  } 



private String forename;
private String surname;
private double[] marks;

public Student(String forename, String surname) {
  super();
  this.forename = forename;
  this.surname = surname; 

  marks = new double [8]; //Creates array of fixed size 7
}

/**
 * @return the forename
 */
 public String getForename() { 
    return this.forename;
 }

 /**
  * @return the surname
  */
 public String getSurname() {
   return this.surname;
 }

 /**
  * @param marks the marks to set
  * @param i 
  */     
 public double getModuleMark (int in) {
   return this.marks[in];        
 }

 public void setModuleMark(int in, double marks) {

this.marks[in] = marks;

} }

于 2013-10-27T10:46:52.693 に答える
0

最初の ModuleMark は、慣例により moduleMark にする必要があります。:-)

次に、getter と Setter の使用は、moduleMarks では (慣例により) このようにする必要があります。

 public double[] getModuleMark() {
return moduleMark;
 }
 public void setModuleMark(double[] moduleMark) {
this.moduleMark = moduleMark;
 }

3 番目 -- モジュール マークのスコープは次のようになります: (学生コンストラクタのスコープだけでなく定義されています)

   public class Student {
      private double [] moduleMark = new double [7]; 
    ....

次に、あなたがこれをやろうとしていると思うなら:

    // Add some marks to the student.
    student.setSpecificModuleMark(0, 10);
    student.setSpecificModuleMark(1, 80);
    student.setSpecificModuleMark(6, 50);

    // Display the marks.
    System.out.println(student.getForename() + " " + student.getSurname());
    System.out.println("Module 0: " + student.getSpecificModuleMark(0));
    System.out.println("Module 1: " + student.getSpecificModuleMark(1));
    System.out.println("Module 6: " + student.getSpecificModuleMark(6));
   }

private double getSpecificModuleMark(int i) {
    // TODO Auto-generated method stub
    return this.moduleMark[i];
   }

private void setSpecificModuleMark(int i, int j) {
    this.moduleMark[i] = j;

   }

4番目:範囲外のインデックスである7を出力しようとしていることに注意してください..したがって、0〜6を使用してください。

于 2013-10-27T10:53:26.330 に答える