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

JAVA: 1) Copy the four existing classes, GradedActivitity, FinalExam, PassFailAc

ID: 3910337 • Letter: J

Question

JAVA:

1) Copy the four existing classes, GradedActivitity, FinalExam, PassFailActivitity, and PassFailExam in Unit 9 Code Examples. The code zip file is linked in Module 9 in Canvas. Suppose they were created by others, DO NOT modify code in these classes. You will use them in your program for this lab.

2) Design an Essay class that extends the GradedActivitity class. The Essay class should determine the grade a student received on an essay. The student’s essay score can be up to 100 and is determined in the following manner:

Grammar: 30 points

Spelling: 20 points

Correct Length: 20 points

Content: 30 points

The Essay class should have:

a) Four private members

b) A constructor with 4 parameters. Use the mutators to initialize all scores (means you do not need to validate each point here) and then the super class method, setScore(), to set up the total score.

c) A set of mutators and accessors (for each of the data members). A mutator for each data member needs to validate the point passed through the parameter (>0 and <= highest point specified); if it is invalid, set the point to 0.

d) A method to get the total score

3) In a course, a teacher gives the following tests and assignments:

• A lab activity that is observed by the teacher and assigned a numeric score.

• A pass/fail exam that has 20 questions. The minimum passing score is 70.

• An essay that is assigned a numeric score.

• A final exam that has 50 questions.

Write a class named CourseGrades that implements the Analyzable interface. The class should have a GradedActivity array named grades as a field. The array should have four elements, one for each of the assignments previously described. Demonstrate the polymorphism feature by declaring a GradeActivitity array and instantiating each array element (object) in each of the four methods. The class should have the following methods:

Constructor: Default constructor used to instantiate the data member.

setLab: This method should accept a GradedActivity object as its argument. This object should already hold the student’s score for the lab activity. Element 0 of the grades field should reference this object.

setPassFailExam: This method should accept a PassFailExam object as its argument. This object should already hold the student’s score for the pass/fail exam. Element 1 of the grades field should reference this object.

setEssay: This method should accept a Essay object as its argument. This object should already hold the student’s score for the essay. Element 2 of the grades field should reference this object.

setFinalExam: This method should accept a FinalExam object as its argument. This object should already hold the student’s score for the final exam. Element 3 of the grades field should reference this object.

toString: This method should return a string that contains the numeric scores and grades for each element in the grades array. The return string should resemble the output below:

Lab Score: 85.0 Grade: B

Pass/Fail Exam Score: 85.0 Grade: P

Essay Score: 80.0 Grade: B

Final Exam Score: 80.0 Grade: B

The CourseGrades class does not inherit any existing class, but it implements the Analyzable interface. The getAverage method should return the average of the numeric scores stored in the grades array. The getHighest method should return a reference to the element of the grades array that has the highest numeric score. The getLowest method should return a reference to the element of the grades array that has the lowest numeric score.

The relationship between the GradedActivity class and CourseGrades class should be aggregation: GradedActivity objects are data members in the CourseGrades class.

Use the provided GradeDemo.java application to test the classes.

GradeActivity: 85

PassFailExam: 20, 3, 70

Essay: 25, 18, 17, 20

FinalExam: 50, 10

The output:

Lab Score: 85.0 Grade: B

Pass/Fail Exam Score: 85.0 Grade: P

Essay Score: 80.0 Grade: B

Final Exam Score: 80.0 Grade: B

Average score: 82.5

Highest score: 85.0

Lowest score: 80.0

____________________________________________________________________________________________

(GRADEDACTIVITY.JAVA)

/**

A class that holds a grade for a graded activity.

*/

public class GradedActivity

{

private double score; // Numeric score

/**

The setScore method sets the score field.

@param s The value to store in score.

*/

public void setScore(double s)

{

score = s;

}

/**

The getScore method returns the score.

@return The value stored in the score field.

*/

public double getScore()

{

return score;

}

/**

The getGrade method returns a letter grade

determined from the score field.

@return The letter grade.

*/

public char getGrade()

{

char letterGrade;

if (score >= 90)

letterGrade = 'A';

else if (score >= 80)

letterGrade = 'B';

else if (score >= 70)

letterGrade = 'C';

else if (score >= 60)

letterGrade = 'D';

else

letterGrade = 'F';

return letterGrade;

}

}

____________________________________________________________________________

(FINALEXAM.JAVA)

/**

This class determines the grade for a final exam.

*/

public class FinalExam extends GradedActivity

{

private int numQuestions; // Number of questions

private double pointsEach; // Points for each question

private int numMissed; // Questions missed

/**

The constructor sets the number of questions on the

exam and the number of questions missed.

@param questions The number of questions.

@param missed The number of questions missed.

*/

public FinalExam(int questions, int missed)

{

double numericScore; // To hold a numeric score

// Set the numQuestions and numMissed fields.

numQuestions = questions;

numMissed = missed;

// Calculate the points for each question and

// the numeric score for this exam.

pointsEach = 100.0 / questions;

numericScore = 100.0 - (missed * pointsEach);

// Call the inherited setScore method to

// set the numeric score.

setScore(numericScore);

}

/**

The getPointsEach method returns the number of

points each question is worth.

@return The value in the pointsEach field.

*/

public double getPointsEach()

{

return pointsEach;

}

/**

The getNumMissed method returns the number of

questions missed.

@return The value in the numMissed field.

*/

public int getNumMissed()

{

return numMissed;

}

}

_________________________________________________________________________________

(PASSFAILACTIVITY.JAVA)

/**

This class holds a numeric score and determines

whether the score is passing or failing.

*/

public class PassFailActivity extends GradedActivity

{

private double minPassingScore; // Minimum passing score

/**

The constructor sets the minimum passing score.

@param mps The minimum passing score.

*/

public PassFailActivity(double mps)

{

minPassingScore = mps;

}

/**

The getGrade method returns a letter grade determined

from the score field. This method overrides the

superclass method.

@return The letter grade.

*/

@Override

public char getGrade()

{

char letterGrade;

if (super.getScore() >= minPassingScore)

letterGrade = 'P';

else

letterGrade = 'F';

return letterGrade;

}

}

___________________________________________________________________________________________

(PASSFAILEXAM.JAVA)

/**

This class determines a passing or failing grade for

an exam.

*/

public class PassFailExam extends PassFailActivity

{

private int numQuestions; // Number of questions

private double pointsEach; // Points for each question

private int numMissed; // Number of questions missed

/**

The constructor sets the number of questions, the

number of questions missed, and the minimum passing

score.

@param questions The number of questions.

@param missed The number of questions missed.

@param minPassing The minimum passing score.

*/

public PassFailExam(int questions, int missed,

double minPassing)

{

// Call the superclass constructor.

super(minPassing);

// Declare a local variable for the score.

double numericScore;

// Set the numQuestions and numMissed fields.

numQuestions = questions;

numMissed = missed;

// Calculate the points for each question and

// the numeric score for this exam.

pointsEach = 100.0 / questions;

numericScore = 100.0 - (missed * pointsEach);

// Call the superclass's setScore method to

// set the numeric score.

setScore(numericScore);

}

/**

The getPointsEach method returns the number of

points each question is worth.

@return The value in the pointsEach field.

*/

public double getPointsEach()

{

return pointsEach;

}

/**

The getNumMissed method returns the number of

questions missed.

@return The value in the numMissed field.

*/

public int getNumMissed()

{

return numMissed;

}

}

_____________________________________________________________________________________

(FINALEXAM.JAVA)

/**

This class determines the grade for a final exam.

*/

public class FinalExam extends GradedActivity

{

private int numQuestions; // Number of questions

private double pointsEach; // Points for each question

private int numMissed; // Questions missed

/**

The constructor sets the number of questions on the

exam and the number of questions missed.

@param questions The number of questions.

@param missed The number of questions missed.

*/

public FinalExam(int questions, int missed)

{

double numericScore; // To hold a numeric score

// Set the numQuestions and numMissed fields.

numQuestions = questions;

numMissed = missed;

// Calculate the points for each question and

// the numeric score for this exam.

pointsEach = 100.0 / questions;

numericScore = 100.0 - (missed * pointsEach);

// Call the inherited setScore method to

// set the numeric score.

setScore(numericScore);

}

/**

The getPointsEach method returns the number of

points each question is worth.

@return The value in the pointsEach field.

*/

public double getPointsEach()

{

return pointsEach;

}

/**

The getNumMissed method returns the number of

questions missed.

@return The value in the numMissed field.

*/

public int getNumMissed()

{

return numMissed;

}

}

______________________________________________________________________

(ANALYZABLE.JAVA)

public interface Analyzable

{

double getAverage();

GradedActivity getHighest();

GradedActivity getLowest();

}

___________________________________________________________________

Explanation / Answer

/**

A class that holds a grade for a graded activity.

*/

public class GradedActivity

{

private double score; // Numeric score

/**

The setScore method sets the score field.

@param s The value to store in score.

*/

public void setScore(double s)

{

score = s;

}

/**

The getScore method returns the score.

@return The value stored in the score field.

*/

public double getScore()

{

return score;

}

/**

The getGrade method returns a letter grade

determined from the score field.

@return The letter grade.

*/

public char getGrade()

{

char letterGrade;

if (score >= 90)

letterGrade = 'A';

else if (score >= 80)

letterGrade = 'B';

else if (score >= 70)

letterGrade = 'C';

else if (score >= 60)

letterGrade = 'D';

else

letterGrade = 'F';

return letterGrade;

}

}

/**

* CourseGrades

*/

public class CourseGrades implements Analyzable{

GradedActivity grades[];

CourseGrades()

{

}

void setLab(GradedActivity ob)

{

grades[0]=ob;

}

void setPassFailExam(GradedActivity ob)

{

grades[1]=ob;

}

void setEssay(GradedActivity ob)

{

grades[2]=ob;

}

void setFinalExam(GradedActivity ob)

{

grades[3]=ob;

}

String toString()

{

System.out.println("Lab Score: "+grades[0].getScore()+" Grade: "+grades[0].getGrade()+" Pass/Fail Exam Score: "+grades[1].getScore()+" Grade: "+grades[1].getGrade()+" Essay Score: "+grades[2].getScore()+" Grade: "+grades[2].getGrade()+" Final Exam Score: "+grades[3].getScore()+" Grade: "+grades[3].getGrade());

}

}