Student Class in Java
Posted by Samath
Last Updated: January 20, 2024

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());
    }
    
}

 

Related Content
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Student Management System in C++
Student Management System in C++
Samath | Jan 08, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Phone Class using Java
Samath | Jan 17, 2024