Alright, here is my question Objects Chapter 4) Implement a class Student. For t
ID: 3696922 • Letter: A
Question
Alright, here is my question
Objects Chapter
4) 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 need to store the number of quizzes the student took.
Write a program that demonstrates the methods for a group of at least 4 students using an ArrayList to hold Student objects.
I know that you need to use methods on eclipse for this problem, but I don't know how you do that. Could you explain how you do that also with the solution to this problem. Can you also provide a screenshot of the answer you got.
Thanks!
Explanation / Answer
import java.util.ArrayList;
public class Student {
// data members
private String name;
private int quiz_score;
private int no_of_quiz;
/**
* @param name
*/
public Student(String name) {
this.name = name;
quiz_score = 0;
no_of_quiz = 0;
}
public String getName(){
return name;
}
public void addQuiz(int score){
quiz_score = quiz_score + score;
no_of_quiz++;
}
public int getTotalScore(){
return quiz_score;
}
public double getAverageScore(){
return (double)quiz_score/no_of_quiz;
}
}
class StudentDriver{
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
Student s1 = new Student("Alex");
s1.addQuiz(67);
s1.addQuiz(87);
Student s2 = new Student("Bob");
s2.addQuiz(98);
s2.addQuiz(78);
s2.addQuiz(65);
Student s3 = new Student("Rocks");
s3.addQuiz(76);
s3.addQuiz(77);
s3.addQuiz(80);
Student s4 = new Student("Pickks");
s4.addQuiz(90);
s4.addQuiz(89);
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
for(Student student: students){
System.out.println("Name: "+student.getName()+", Quiz Total Score: "+
student.getTotalScore()+", Average: "+String.format("%.2f",student.getAverageScore()));
}
}
}
/*
Output:
Name: Alex, Quiz Total Score: 154, Average: 77.00
Name: Bob, Quiz Total Score: 241, Average: 80.33
Name: Rocks, Quiz Total Score: 233, Average: 77.67
Name: Pickks, Quiz Total Score: 179, Average: 89.50
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.