ok so this is the assignment that I’m working on:
“Implement a class Student (you may use the one you created for lab and add on to it). In addition to any other attributes that the class already has, the student object will have a total quiz score. Be sure to write a complete class (i.e. proper attributes, constructor, accessors and mutators).
Other methods needed are: addQuiz(int score), showStudentInfo(), and getAverageScore(). Note that in order to calculate average, the class needs to know the number of quizzes.
The driver/tester class will create three (3) student objects. Each student object’s method will be called five times to add five (5) quiz grades.
I recommend that you use Random class to generate the quiz values, instead of hard-coding them.Then the driver calls Student’s method to display the students’ name, all five quiz grades, and the average score of the quizzes.”
I have a basic idea of what needs to be done but I’m just unsure on some of the methods. For example the “getAverageScore” method is the one that I’m having trouble with. Also do I even need the get and set methods for age name and major if I’m am just initializing them at the beginning of the tester program? Any help on what needs to be added or fixed with my class is greatly appreciated. Here is the code that I have so far:
import java.util.Random;
public class Student
{
private int Age;
private String Name;
private String Major;
private int Score;
public Student(String n, int a, String m)
{
Name = n;
Age = a;
Major = m;
}
public String showStudentinfo()
{ return (Name + " " + Age + " " + Major + "\n");
}
public int addQuiz()
{ Score = randomNumbers.nextInt(101);
return Score;
}
public int getAverageScore()
{
}
//setter methods
public void setAge(int a)
{ Age = a;
}
public void setName(String n)
{ Name = n;
}
public void setMajor(String m)
{ Major = m;
}
//getter methods
public int getAge()
{ return Age;
}
public String getName()
{ return Name;
}
public String getMajor()
{ return Major;
}
}
You need to store the quiz grades somewhere. Your professor said there would be more than one. Above the ‘Student’ constructor, add either an Array of size 5 or an ArrayList. I will use an Array for this, so:
Then in your method that gets the scores, add them to the Array with a Loop:
Then you should be able to calculate the average of the scores by adding the grades together and dividing them by 5:
I have not actually tested this code, and I am a beginner myself, but hopefully this puts you on the right track. 🙂