Student Class in Java

Posted by Samath
  1730
January 31, 2015

 

Implement a class named Student.  For the purpose of this exercise, a student has a name, an id number and a quiz count.  Supply an appropriate constructor and methods getName(), addQuiz(int score), getTotalScore(), getNumQuizzes() and getAverageScore().   The scores should be stored in an array.  You should compute the total score and the average score when needed.

Write a separate class TestStudent, which includes a main method that fully tests your Student class.

Student:

public class Student 
{
    private String name;
    private static String id_number;
    private int quiz_count;
    private int totalScore;
    private int avgScore;
    private int score[];
    
    public Student()
    {
      name = "";
      id_number = "";
      quiz_count = 0;
      totalScore = 0;
      avgScore = 0;
      score = new int[100];
    }
    
    public Student(String n, String id)
    {
      name = n;
      id_number = id;
      score = new int[100];
    }
    
    public void changeName(String n)
    {
        name = n;
    }
    
    public String getName()
    {
        return name;
    }
    
    public String getID()
    {
        return id_number;
    }
    
    public void addQuiz(int s)
    {
          score[quiz_count] = s;
          quiz_count++;
          totalScore = totalScore + s;
    }
    
    public int getTotalScore()
    {
       return totalScore;
    }
    
    public int getNumQuizzes()
    {
        return quiz_count;
    }
    
    public int getAverageScore()
    {
        avgScore = totalScore/quiz_count;
        return avgScore;
    }
    
}

TestStudent:

public class TestStudent {
    public static void main(String[] args) {
        
        Student stu = new Student("Samath", "18200978");
        stu.addQuiz(100);
        stu.addQuiz(100);
        System.out.println("Total Score: " + stu.getTotalScore());
        System.out.println("Average Score: " + stu.getAverageScore());
        System.out.println("Number of Quiz: " + stu.getNumQuizzes());
    }
    
}