Implement a class Student. For the purpose of this exercise, a student has a name and
a total quiz score. Supply an appropriate constructor and methods getName(),
addQuiz(int score), getTotalScore(), and getAverageScore(). To compute the latter, you
also need to store the number of quizzes that the student took.
Supply a StudentTester class that tests all methods.
1
Expert's answer
2017-10-11T15:40:19-0400
public class Student { public Student(String name){ super(); this.name=name; this.quizNumber=0; this.quizScore=0; } public String getName(){ return name; } public void addQuiz(int score){ this.quizScore+=score; quizNumber++; } public int getTotalScore(){ return quizScore; } public double getAverageScore(){ return (double)quizScore/quizNumber; }
private String name; private int quizScore; private int quizNumber;
Comments
Dear Mark, this code shold compile if you create both files Student.java and StudentTester.java correctly: user@home /tmp/111 $ ls -l total 8 -rw-r--r-- 1 expert1 expert1 419 Oct 11 20:37 Student.java -rw-r--r-- 1 expert1 expert1 440 Oct 11 20:38 StudentTester.java user@home /tmp/111 $ javac *.java user@home /tmp/111 $ java StudentTester Student: StudentName1 Total quiz score: 411 Average quiz score: 82.2
I am trying to get the code to work but it gives me an error at the line Student student = new Student("StudentName1"); please help fix
Leave a comment