-3

This is the code I have currently.

Below is my object "Student"

`public class Student {
    
    private String name;
    private int score;
    
    public Student() {
    }
    
    public Student(String name, int score){
        this.name = name;
        this.score = score;
        
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setScore(int score) {
        this.score = score;
    }
    
    public void readInput() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the student's name: ");
        this.name = keyboard.next();
        System.out.println("Please enter the student's score: ");
        this.score = keyboard.nextInt();
    }
    public void writeOutput() {
        System.out.println("The student's name and score: " + name + ", " + score + "%");
    }
    
    public String getName(String name) {
        return this.name;
    }
    public int getScore(int score) {
        return score;
    }
}`

Then in another class "TestReporter" I am attempting to compute the averageof the array of ourClass[] .

I am also to find the highest score within the ourClass array but don't know how to seperate scores from students , I probably overcomplicated the question but any help would be appreciated.

`import java.lang.reflect.Array; import java.util.Arrays; import java.util.Scanner;

public class TestReporter {

private int highestScore;
private double averageScore;
private Student[] ourClass; 
private int numOfStudents;

public TestReporter(){
}

public void getData() {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter the number of students");
    numOfStudents = keyboard.nextInt();
    ourClass = new Student[numOfStudents];
    for (int i = 0; i < numOfStudents ; i++) {
        ourClass[i] = new Student(); 
        ourClass[i].readInput();
    }
}

public void computeStats() {
    double total = 0;
    for (int i = 0; i < numOfStudents; i++) {
        total = total + ourClass[i];
    }
    averageScore = total / ourClass.length;

    
}

public void displayResults() {
    
    for (Student Student: ourClass) {
        Student.writeOutput();
    }
}

}`


  • 1
    `total = total + ourClass[i].getScore();`. Also, remove the parameters from `getName` and `getScore`. Those are **getters** and should only return your fields. – QBrute Nov 19 '22 at 08:35

1 Answers1

-1

To get Highest Score declare variable in compute StateStats

public void computeStats() {
    double total = 0;
   int highestScore = 0;

    for (int i = 0; i < numOfStudents; i++) {
        int score = ourClass[i].getScore();
        total = total;
        if(score > highestScore)
           highestScore =  score;

    }
    averageScore = total / ourClass.length;
    System.output.println("Average Score = "+averageScore;
    System.output.println("Highest Score = " highestScore;
    
}

then add following to displayResults()

computeStats();

Also: change Setters as mentioned by {QBrute}