2017-01-15 4 views
0

インポートされたクラスからゲッターを呼び出す際に問題があります。
私は働くクラス(Students)とアクションクラス(ProgressReport)を作成しました。メインクラスはテキストファイルを読み取り、データを配列に書き込みます。その後、データは作業クラスで操作されます。最後に、ProgressReport.generateReportは、生徒の名前、学年平均、およびその平均と関連する文字グレードを示すレポートを作成します。
Progress ReportクラスのStudentsゲッターの使用に問題があります。 Eclipseは、メソッドが定義されていないと言っています。私が間違ってやったことや、それを修正する方法を正確には分かっていません。どんな助けも非常に高く評価されるでしょう。
注:printlnを追加して、コードの一部が実行されていることを確認しました。

ありがとうございます。インポートされたクラスからゲッターを呼び出す


コードは、次のとおりです。

進捗報告

package Lab1A; 

import java.util.*; 

import Lab1A.Students; 

import java.io.*; 

public class ProgressReport { 
public Students section[][]; 

    public static void main(String[] args) throws IOException 
    { 
     Students tmpStudent; 

     ProgressReport progressReport = new ProgressReport(); 

     progressReport.readInputFile(); 
     progressReport.generateReport(); 


     System.out.println("\nSEARCH TEST"); 
     tmpStudent = null; 
     tmpStudent = progressReport.sequentialSearch(0, "Cooper"); 
     if (tmpStudent != null) 
      System.out.println("Found " + tmpStudent.getName() + 
        "\tAverage = " + tmpStudent.getAverage() + 
        "\tGrade = " + tmpStudent.getGrade()); 
     else System.out.println("Fail to find the student"); 

     tmpStudent = null; 
     tmpStudent = progressReport.sequentialSearch(0, "Bronson"); 
     if (tmpStudent != null) 
      System.out.println("Found " + tmpStudent.getName() + 
        "\tAverage = " + tmpStudent.getAverage() + 
        "\tGrade = " + tmpStudent.getGrade()); 
     else System.out.println("Fail to find the student"); 

     tmpStudent = null; 
     tmpStudent = progressReport.sequentialSearch(1, "Diana"); 
     if (tmpStudent != null) 
      System.out.println("Found " + tmpStudent.getName() + 
        "\tAverage = " + tmpStudent.getAverage() + 
        "\tGrade = " + tmpStudent.getGrade()); 
     else System.out.println("Fail to find the student"); 

    } 

    public ProgressReport() 
    { 
     section = new Students [2][]; 
    } 

    public void readInputFile() throws FileNotFoundException 
    { 
     System.out.println("in readInputFile method"); 
     //Open file 
     File input = new File("Lab1A.in"); 
     Scanner inputFile = new Scanner(input); 
     System.out.println("file is open"); 
     //Read file data 
     int currentStudent = 0; 
     while (inputFile.hasNext()) 
     { 
      System.out.println("In while loop"); 
      //Get Student count 
      int rows = inputFile.nextInt(); 

      //Read student data 
      section[currentStudent] = new Students[rows]; 
      System.out.println("array initiated"); 
      for(int i = 0; i < rows; i++) 
      { 
       System.out.println("In for loop"); 
       //read in a students info 
       String sName = inputFile.next(); 

       //Read in grades 
       int grade1 = inputFile.nextInt(); 
       int grade2 = inputFile.nextInt(); 
       int grade3 = inputFile.nextInt(); 
       int grade4 = inputFile.nextInt(); 
       int grade5 = inputFile.nextInt(); 

       //Send to Students Array 
       section[currentStudent][i] = new Students(sName, grade1, grade2, grade3, grade4, grade5); 


      } 
      //Next Student Line 
      currentStudent++;   

     } 
     inputFile.close(); 
    } 

    public void generateReport() 
    { 
     System.out.println("Progress Report"); 
     double average = Students.class.getAverage(); 
     //String section = "Section\n"; 




    } 

    public Students sequentialSearch(int section, String searchName) 
    { 
    return null; 


    } 
} 

学生クラス

package Lab1A; 

import java.lang.reflect.Array; 

public class Students { 

    private String name; 
    private char grade; 
    private double average; 
    private int scores[]; 

    public Students(String sName, int grade1, int grade2, int grade3, int grade4, int grade5) 
    { 
     //CONSTRUCTOR load data from ProgressReport 
     name = sName; 
     int newScores[] = {grade1, grade2, grade3, grade4, grade5}; 
     scores = newScores; 
    } 

    //Getters and Setters 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public char getGrade() { 
     if(average >= 90 && average <= 100) 
     { 
      grade = 'A'; 
     } 
     if(average >= 80 && average < 90) 
     { 
      grade = 'B'; 
     } 
     if(average >= 70 && average < 80) 
     { 
      grade = 'C'; 
     } 
     if(average >= 60 && average < 70) 
     { 
      grade = 'D'; 
     } 
     if(average >= 0 && average < 60) 
     { 
      grade = 'F'; 
     } 

     return grade; 
    } 
    public void setGrade(char grade) { 
     this.grade = grade; 
    } 
    public double getAverage() { 
     return average; 
    } 
    public void setAverage(double average) { 
     this.average = average; 
    } 
    public int[] getScores() { 
     return scores; 
    } 
    public void setScores(int[] scores) { 
     this.scores = scores; 
    } 



    //Calculate average score 
    public void calculateAverage() 
    { 
     int total = 0; 

       for (int i = 0; i < scores.length; i++) 
        { 
        total += scores[i]; 
        } 
       average = total*1.0/scores.length; 
    } 

    //calculate letter grade based on average score (calulateAverage.average) 
    public void calculateGrade() 
    { 

     if(average >= 90 && average <= 100) 
     { 
      grade = 'A'; 
     } 
     if(average >= 80 && average < 90) 
     { 
      grade = 'B'; 
     } 
     if(average >= 70 && average < 80) 
     { 
      grade = 'C'; 
     } 
     if(average >= 60 && average < 70) 
     { 
      grade = 'D'; 
     } 
     if(average >= 0 && average < 60) 
     { 
      grade = 'F'; 
     } 

    } 
} 
+0

コードのどの部分がエラーを生成していますか?ボランティアにあなたに手伝ってもらうときは、必要な情報をすべて提供し、他の人があなたを助けてくれるようにすることが礼儀正しいと考えてください。 –

+1

私はあなたが 'double average = Students.class.getAverage();を参照していると仮定します。あなたがここで何が起こると思いますか分かりません。 'getAverage()'はインスタンスメソッドであり、動作させるインスタンスが必要です。 'Students.class'という表現は' Students'のインスタンスではない 'Students'クラスの' Class <> 'オブジェクトを返します。あなたは質問を明確にしなければなりません。 –

+0

ジョシュはすべての生徒オブジェクトの平均を計算しようとしていると思います。この場合、生徒#getAverage()を呼び出してアキュムレータ変数に追加し、生徒数で割る二重にも)....しかし、これが唯一の問題かどうかはわかりません。 – synchronizer

答えて

0

あなたは、あなたがあなたのクラスを命名理由の平均は(わからないたい学生指定しませんsを持つ学生)。

section[1][1].getAverage() 
+0

これはうまくいった!ありがとう。 (私はJavaに慣れていないし、まだすべての慣習を知っていない。 –

関連する問題