Answer to Question #33305 in Java | JSP | JSF for Roy

Question #33305
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;

}







public class StudentTester {


/**
* @param args
*/
public static void main(String[] args) {
Student student = new Student("StudentName1");
student.addQuiz(85);
student.addQuiz(70);
student.addQuiz(90);
student.addQuiz(80);
student.addQuiz(86);

System.out.println("Student: "+student.getName());
System.out.println("Total quiz score: "+student.getTotalScore());
System.out.println("Average quiz score: "+student.getAverageScore());
}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
11.10.17, 22:39

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

Mark
11.10.17, 22:17

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

LATEST TUTORIALS
APPROVED BY CLIENTS