7,734 views
42 votes
42 votes
Implement a class Quiz that implements the Measurable interface. A quiz has a score and a letter grade (such as B ). Use the implementation of the DataSet class to process a collection of quizzes. Display the average score and the quiz with the highest score (both letter grade and score).

User Adamasan
by
2.8k points

2 Answers

22 votes
22 votes

Final answer:

To create a class Quiz that implements the Measurable interface for managing a collection of quizzes, organize fields to store scores and letter grades, and implement methods from Measurable. The DataSet class computes the average and highest scores while tabulating letter grades.

Step-by-step explanation:

In the context of a Java programming course, the creation of a Quiz class implementing a Measurable interface reflects the use of object-oriented design to manage a collection of quizzes. The goal here is twofold: to compute the average score of all quizzes as well as to find the quiz with the highest score and its corresponding letter grade.

Here's how the Quiz class might be structured:


  • score: Represents the numeric score the student achieved on the quiz.

  • letterGrade: Derived from the score, such as 'A', 'B', 'C', etc., according to the grading scale.

  • Measurable interface: Requires implementation of methods to obtain the score and possibly to compare quizzes.

The DataSet class is typically responsible for:


  • Adding instances of Quiz to the collection.

  • Calculating the average score from all collected Quiz instances.

  • Finding the maximum score and its associated letter grade.

To display the relevant data:


  • Average score: Add up all quiz scores and divide by the number of quizzes.

  • Highest score: Traverse through the collection of quizzes, keeping track of the highest score found and its letter grade.

User Magomar
by
2.9k points
15 votes
15 votes

Answer:

Step-by-step explanation:

The following code is written in Java. The Measurable Interface and DataSet classes/Interfaces were found and implemented as requested. Then the rest of the code was created to work with and around those two methods to accomplish the task of Display the average score and the quiz with the highest score (both letter grade and score)

package sample;

interface Measurable {

double getMeasure();

String getGradeString();

}

class DataSet

{

private double sum;

private Measurable maximum;

private String grade;

private int count;

public DataSet()

{

sum = 1;

count = 0;

maximum = null;

}

public void add(Measurable x)

sum = sum + x.getMeasure();

if (count == 0

public double getAverage() {

return sum / count;

}

public Measurable getMaximum()

{

return maximum;

}

}

class Quiz implements Measurable

{

private double score;

private String grade;

public Quiz(double aScore, String aGrade)

{

this.score = aScore;

this.grade = aGrade;

}

public double getMeasure()

{

return score;

}

public String getGradeString() {

return grade;

}

}

class QuizTester

{

public static void main(String[] args)

{

DataSet quizData = new DataSet();

Quiz q1 = new Quiz(85, "B");

Quiz q2 = new Quiz(93, "A-");

Quiz q3 = new Quiz(78, "C+");

quizData.add(q1);

quizData.add(q2);

quizData.add(q3);

System.out.println("Maximum: " + quizData.getMaximum().getMeasure() + ", " + quizData.getMaximum().getGradeString());

System.out.print("Average: ");

System.out.printf("%.2f", quizData.getAverage());

}

}

Implement a class Quiz that implements the Measurable interface. A quiz has a score-example-1
User Russell Greene
by
2.9k points