0

私のコード:学生クラス...

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;

 public class Student {
    private Scanner scanner;

    //initialize data members
    private String firstName;
    private String lastName;
    private int assignmentScore;
    private int labScore;
    private int quizScore;
    private int attendanceScore;
    private int homeworkScore;
    private int midterm1Score;
    private int midterm2Score;
    private int finalExamScore;
    private int zyanteScore;
    private int patScore;
    private int testScore;
    private String letterGrade;

    public void setScanner() {
        try { //start try statement
            scanner = new Scanner(new File("gradesA5.txt")); //initialize scanner to scan from the grades.txt file
        } //end try statement
        catch (FileNotFoundException e) { //start catch statement
            System.out.println("Error opening file. Please make sure t  that you have a grades.txt file in the same folder as GradeCalculator.class"); //print statement telling user that the grades.txt file is not in the right place
            System.exit(0); //system exit 
        } //end catch statement
    }

    public Scanner getScanner(){
        return scanner;
    }

    public void setfirstName(){
        if(scanner.hasNext()){
            firstName = scanner.next();
        }
    }

    public String getFirstName(){
        return firstName;
    }

    public void setLastName(){
        if(scanner.hasNext()){
            lastName = scanner.next();
        }   
    }

    public String getLastName(){
        return lastName;
    }

    public void setAssignmentScore(){
        assignmentScore = 0;
        int i = 0;
        int[] assignment = new int[6];

        while(scanner.hasNext() && i <= 5){
            assignment[i] = scanner.nextInt();
            assignmentScore += assignment[i];
            i++;
        } 
    }

    public int getAssignmentScore(){
        return assignmentScore;
    }

    public void setLabScore(){
        labScore = 0;
        int i = 0;
        int[] lab = new int[15];

        while(scanner.hasNext() && i <=14){
            lab[i] = scanner.nextInt();
            labScore += lab[i];
            i++;
        }
    }

    public int getLabScore(){
        return labScore;
    }

    public void setQuizScore(){
        if(scanner.hasNext()){
            quizScore = scanner.nextInt();
        }
    }

    public int getQuizScore(){
        return quizScore;
    }

    public void setAttendanceScore(){
        if(scanner.hasNext()){
            attendanceScore = scanner.nextInt();
        }
    }

    public int getAttendanceScore(){
        return attendanceScore;
    }

    public int computeTotalHomeworkScore(){
        homeworkScore = (assignmentScore + labScore + attendanceScore);
        return homeworkScore;
    }

    public void setMidterm1Score(){
        if(scanner.hasNext()){
            midterm1Score = scanner.nextInt();
        }
    }

    public int getMidterm1Score(){
        return midterm1Score;
    }

    public void setMidterm2Score(){
        if(scanner.hasNext()){
            midterm2Score = scanner.nextInt();
        }
    }

    public int getMidterm2Score(){
        return midterm2Score;
    }

    public void setFinalExamScore(){
        if(scanner.hasNext()){
            finalExamScore = scanner.nextInt();
        }
    }

    public int getFinalExamScore(){
        return finalExamScore;
    }

    public void setZyanteScore(){
        if(scanner.hasNext()){
            zyanteScore = scanner.nextInt();
        }
    }

    public int getZyanteScore(){
        return zyanteScore;
    }

    public void setPATScore(){
        if(scanner.hasNext()){
            patScore = scanner.nextInt();
        }
    }

    public int getPATScore(){
        return patScore;
    }

    public int computeTotalTestScore(){
        testScore = (midterm1Score + midterm2Score + finalExamScore + quizScore + zyanteScore + patScore);
        return testScore;
    }

    public void computeLetterGrade(){
        if ((testScore <165 && homeworkScore <780) || (testScore <180 && homeworkScore <720) || (testScore <195 && homeworkScore <660) || (testScore <210 && homeworkScore <660) || (homeworkScore <600)){ //if statement that executes if scores earned a failing grade
            letterGrade = "P" ; //sets letterGrade string to P
        } //end if statement

        if (((testScore >= 150 && testScore <= 164) && (homeworkScore >= 780)) || ((testScore >= 165 && testScore <= 179) && (homeworkScore >=720)) || ((testScore >=180 && testScore <=194) && (homeworkScore >= 660 && homeworkScore <= 1139)) || 
            ((testScore >= 195 && testScore <= 209) && (homeworkScore >= 660 && homeworkScore <= 1079)) || ((testScore >= 210 && testScore <= 224) && (homeworkScore>= 600 && homeworkScore <= 959)) || ((testScore >= 225 && testScore <= 239) && (homeworkScore >= 600 && homeworkScore <= 899)) ||
            ((testScore >= 240 && testScore <= 254) && (homeworkScore >= 600 && homeworkScore <= 839)) || ((testScore >= 255 && testScore <= 269) && (homeworkScore >= 600 && homeworkScore <= 779)) || ((testScore >= 270 && testScore <= 284) && (homeworkScore >= 600 && homeworkScore <= 779)) ||
            ((testScore >= 285 && testScore <= 300) && (homeworkScore >= 600 && homeworkScore <= 719))) { //if statement that executes if scores earned an average grade
            letterGrade = "A" ; //sets letterGrade string to A
        } //end if statement

        if (((testScore >= 180 && testScore <= 194) && (homeworkScore >=  1140 && homeworkScore <= 1200)) || ((testScore >= 195 && testScore <= 209) && (homeworkScore >= 1080 && homeworkScore <= 1200)) ||
            ((testScore >= 210 && testScore <= 224) && (homeworkScore >= 960 && homeworkScore <= 1200)) || ((testScore >= 225 && testScore <= 239) && (homeworkScore >= 900 && homeworkScore <= 1200)) ||
            ((testScore >= 240 && testScore <= 254) && (homeworkScore >= 840 && homeworkScore <= 1200)) || ((testScore >= 255 && testScore <= 269) && (homeworkScore >= 780 && homeworkScore <= 1200)) ||
            ((testScore >= 270 && testScore <= 284) && (homeworkScore >= 780 && homeworkScore <= 1200)) || ((testScore >= 285 && testScore <= 300) && (homeworkScore >= 720 && homeworkScore <= 1200))) { //if statement that executes if scores earned a good grade
            letterGrade = "G" ; //sets letterGrade string to G
        } //end if statement
    }

    public String getLetterGrade(){
        return letterGrade;
    }

    public void testing() {
        for (int i = 0; i < 7; i++) {
            if (scanner.hasNext()) {
                String test = scanner.next();
            }
        }
    }
 }      

コース提供クラス...

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;

 public class CourseOffering {
private Scanner scanner;

Student myStudent = new Student();

//initialize data members
private int students;
private String description;
private int allTotalHomeworkScore = 0;
private int allTotalTestScore = 0;
private int totalStudents = 0;
private int numberOfSemesters;
private int studentArrayCount = 0;
private double homeworkAverage;
private double testAverage;

public CourseOffering(Scanner inScanner){
    scanner = inScanner;
}

public void setNumberOfSemesters(){
    if(scanner.hasNext()){
        numberOfSemesters = scanner.nextInt();
    }
}

public int getNumberOfSemesters(){
    return numberOfSemesters;
}

public void setStudents(){
    int[] student = new int[numberOfSemesters];
    student[studentArrayCount] = scanner.nextInt();
    students = student[studentArrayCount];
    studentArrayCount++;
    totalStudents += students;
}

public double computeHomeworkAverage(){
    allTotalHomeworkScore += myStudent.computeTotalHomeworkScore();
    homeworkAverage = (allTotalHomeworkScore / totalStudents);
    return homeworkAverage;
}

public double computeTestAverage(){
    allTotalTestScore += myStudent.computeTotalTestScore();
    testAverage = (allTotalTestScore / totalStudents);
    return testAverage;
}

public void countPassingStudents(){

}
 }

CourseStatistics クラス (他を呼び出すクラス)...

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;

 public class CourseStatistics {

public static void main(String[] args) {

    Scanner scanner = null;

    try { //start try statement
        scanner = new Scanner(new File("gradesA5.txt")); //initialize scanner to scan from the grades.txt file
    } //end try statement
    catch (FileNotFoundException e) { //start catch statement
        System.out.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class"); //print statement telling user that the grades.txt file is not in the right place
        System.exit(0); //system exit 
    } //end catch statement

    Student myStudent = new Student();
    CourseOffering myCourse = new CourseOffering(scanner);
    myStudent.setScanner();
    myStudent.getScanner();
    myStudent.testing();
    myCourse.setNumberOfSemesters();
    for(int i = 0; i < 4; i++){
    myStudent.setfirstName();
    myStudent.setLastName();
    myStudent.setAssignmentScore();
    myStudent.setLabScore();
    myStudent.setMidterm1Score();
    myStudent.setMidterm2Score();
    myStudent.setFinalExamScore();
    myStudent.setQuizScore();
    myStudent.setAttendanceScore();
    myStudent.setPATScore();
    myStudent.setZyanteScore();
    myStudent.computeLetterGrade();
    System.out.println(myStudent.getFirstName() + " " + myStudent.getLastName() + " " + myStudent.computeTotalHomeworkScore() + " " + myStudent.computeTotalTestScore() + " " + myStudent.getLetterGrade());
    }
    System.out.println(myCourse.getNumberOfSemesters());
}   
 }

出力は...

Anthony Hopkins 854 284 P
John Smith 730 214 G
Pan Mei 730 267 A
Rafael Vega 801 236 A
3

.txt ファイルのデータ オブジェクトは 3 でありmyCourse.setNumberOfSemesters()、最初にスキャンするものではありませんが、3 の最初のデータまでスキャンします。2 つのスキャン クラスを混在させるにはどうすればよいですか。もう一方はやめました。

4

2 に答える 2

0

1) 何を、どこで、誰もそんなに多くのコードを勉強したくないのか、もっと具体的にすべきです。

2)OOPアプローチについて考える必要があります

OOP ヒント NO.1

オブジェクトに同様の意味を持つ多くの値がある場合は、それらを配列/リストに格納してみてください。そうすれば、新しい値が表示されたときにクラスを書き直す必要がなくなります (middle3 など)。

たとえば、これ:

private int assignmentScore;
private int labScore;
private int quizScore;
private int attendanceScore;
private int homeworkScore;
private int midterm1Score;
private int midterm2Score;
private int finalExamScore;
private int zyanteScore;
private int patScore;
private int testScore;

として良いだろう

private Map<String, Integer> exams = new HashMap<>();

次に、次のようにアクセスできます。

exams.get("midterm1")
于 2013-11-01T16:26:18.833 に答える