Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the source code for the Data class (the \'average\' method is already wr

ID: 3549900 • Letter: H

Question

Here is the source code for the Data class (the 'average' method is already written): http://notepad.cc/snoosheessu82

Here is the source code for the Measurable interace (the 'getMeasure' method is already written):http://notepad.cc/naccapa49


1. Add a method

public static Measurable max(Measurable[] objects)


to the Data class that returns the object with the largest measure.


2. Implement a class Quiz that implements the Measurable interface. A quiz has a score

and a letter grade (such as B+). Use the Data class to process an array

of quizzes. Display the average score and the quiz with the highest score (both letter

grade and score).

Explanation / Answer

Hi,


Please find the below programs and output.


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Data {
    /**
    * Computes the average of the measures of the given objects.
    *
    * @param objects
    *            an array of Measurable objects
    * @return the average of the measures
    */
    public static double average(Measurable[] objects) {
        double sum = 0;
        for (Measurable obj : objects) {
            sum = sum + obj.getMeasure();
        }
        if (objects.length > 0) {
            return sum / objects.length;
        } else {
            return 0;
        }
    }

    public static void processQuiz() {
        System.out.println("Quiz Details with Quiz initialized");
        Quiz q1 = new Quiz();
        Quiz q2 = new Quiz();
        Quiz q3 = new Quiz();
        Quiz q4 = new Quiz();
        Quiz q5 = new Quiz();

        q1.grade = "A+";
        q1.score = 1;
        q2.grade = "B+";
        q2.score = 2;
        q3.grade = "C+";
        q3.score = 3;
        q4.grade = "D+";
        q4.score = 4;
        q5.grade = "E+";
        q5.score = 5;

        Quiz[] quizArray = { q1, q2, q3, q4, q5 };
        System.out.println("Average Score: " + average(quizArray));
        System.out.println("Maximum score and grade"+ max(quizArray).toString());

    }

    public static Measurable max(Measurable[] objects) {
        List measure = new ArrayList();
        measure = Arrays.asList(objects);
        return Collections.max(measure);

    }
   
    public static void main(String[] args) {
        Data data = new Data();
        data.processQuiz();
    }
}


public interface Measurable extends Comparable<Quiz> {
    double getMeasure();

}


public class Quiz implements Measurable {

    int score;
    String grade;

    @Override
    public String toString() {
        return "Quiz [grade=" + grade + ", score=" + score + "]";
    }

    @Override
    public double getMeasure() {
        // TODO Auto-generated method stub
        return score;
    }

    @Override
    public int compareTo(Quiz quiz) {
        // TODO Auto-generated method stub
        if (this.score < quiz.score)
            return 1;
        else if (this.score > quiz.score)
            return -1;
        else
            return 0;

    }

}

Output:


Quiz Details with Quiz initialized
Average Score: 3.0
Maximum score and gradeQuiz [grade=A+, score=1]


Thanks