PTA/6-5 writes the Student class, and uses the generic and collection framework to write the StudentList class to realize the function of calculating the average score of the class. (10 points)

Posted by angulion on Tue, 26 Oct 2021 06:38:44 +0200

bug log post

The title requirements are as follows:
Main class: in the main method, the constructStudentList method is called to construct a Worker object list, and the computeAverageScore method is used to calculate the average score of a class and output it to the screen. Write Student class and StudentList class as required.

The programming requirements of Student class are as follows:
Member variables include: student name (String name), class code (String classCode), and score (int score)
Write construction methods according to program requirements.
Write set and get methods according to program requirements.

The programming requirements of StudentList class are as follows:
Write construction methods according to program requirements.
constructStudentList method: call the readInStudent method to read multiple Student information, add Student objects to the linked list, build a linked list of Student objects, and finally return to the linked list.
readInStudent method: use scanner to read in a Student's name, class and score from the keyboard, build a Student object and return it.
Computeaveragescore (list) method: traverse the linked list, accumulate the number and total score of all students in the linked list, calculate the average score of a class and return it.

Interface definition

List<Student> constructStudentList();
Student readInStudent();
double computeAverageScore(List<Student> list);

judging procedures

import java.util.*;

public class Main {
    public static void main(String[] args) {
        StudentList sl=new StudentList();        
        List<Student> list=sl.constructStudentList();        
        System.out.println(sl.computeAverageScore(list));    
    }
}

/* Please fill in the answer here */
/*Please supplement the Student class here*/


/*Please supplement the StudentList class here*/

Stick the correct answer first

class Student1{
    String name;
    String classCode;
    int score;
    public Student1(){}
    public Student1(String name,String classCode,int score){
        this.name = name;
        this.classCode = classCode;
        this.score =score;
    }
    public String getClassCode(){
        return classCode;
    }
    public int getScore(){
        return score;
    }
}

class StudentList{
    private Scanner sc;//Common input stream
    public String NeedClassCode;
    List<Student1> constructStudentList(){
        sc = new Scanner(System.in);
        NeedClassCode = sc.next();//Big pit
        List<Student1> Stus = new ArrayList<>();
        Student1 Stu = readInStudent();
        while(Stu != null){
            if(Stu.getClassCode().equals(NeedClassCode))
                Stus.add(Stu);
            Stu = readInStudent();
        }
        sc.close();
        return Stus;
    }
    Student1  readInStudent(){
       //Scanner sc = new Scanner(System.in);// Big pit
        String name = sc.next();
        if(name.equals("#")){
            return null;
        }
        String classCode = sc.next();
        int score = sc.nextInt();
        return new Student1(name,classCode,score);

    }
    double computeAverageScore(List<Student1> list){
        ListIterator<Student1> i=list.listIterator();
        double sum = 0;
        while(i.hasNext()){
                int x = i.next().getScore();
                sum+=x;
        }
        return sum/list.size();
    }
}

The initial idea is to enter all the data into the list, compare the classCode during traversal, and then calculate the average. I didn't expect that there was a problem from this beginning

Because i.next().getClassCode() and i.next().getScore() are used in the iterator to compare classCode and calculate score. During the result test, an overflow error is always reported (the corresponding value cannot be found). Later, after checking, I know that the next() method will automatically change to the next element every time it is executed. In fact, I can know by thinking about it. I'm really out of my mind

So change your mind, put the classCode comparison in the list reading element, and only put the people in the corresponding class

It's also because I saw the practice of a classmate of CSDN and learned from it. Then he marked a pit in Scanner. At that time, I looked at it as a close problem, because he didn't seem to write close, and then he successfully walked through his pit again

The problem is that we first read a classCode as the flag for calculating the average score, and then the rest of the data is read by another function, so I have a Scanner in both functions. It's no problem in normal tests. Because our data is input one by one, but pta can't. pta is pasted like a clipboard, Therefore, our first Scanner will directly read all the data in all streams, resulting in that the second function can only re-enter the data if it wants to read

Solution:
Since the input data can only be read by one Scanner, let's just make it public. Both functions use this Scanner
Although the solution is very simple, this mistake is really in the underworld. I've been looking for me for a long time

Topics: Java linked list list