Java programming: Essay class: Desgin an Essay class that extends the GradedActi
ID: 3941001 • Letter: J
Question
Java programming:
Essay class:
Desgin an Essay class that extends the GradedActivity* class presented in this chapter. The Essay class should determine the grade a student receives for 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
Demonstrate the class in a simple program.
*GradedActivity*
/**
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;
}
}
Explanation / Answer
public class Essay extends GradedActivity {
double grammerMarks;
double spellingMarks;
double correctLenthMarks;
double contentMarks;
public Essay(double grammerMarks, double spellingMarks, double correctLenthMarks, double contentMarks) {
this.contentMarks = contentMarks;
this.correctLenthMarks = correctLenthMarks;
this.grammerMarks = grammerMarks;
this.spellingMarks = spellingMarks;
}
public double calculateScore() {
return (this.contentMarks + this.correctLenthMarks + this.grammerMarks + this.spellingMarks);
}
public static void main(String[] args) {
Essay es = new Essay(20, 20, 10, 20);
es.setScore(es.calculateScore());
System.out.println(es.getGrade());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.