6. Analyzable Interface
Modify the CourseGrades class you created in Programming Challenge 5 so it implements
the following interface:
public interface Analyzable
{
double getAverage();
GradedActivity getHighest();
GradedActivity getLowest();
}
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. Demonstrate the new
methods in a complete program.
Here i am providing the answer. Hope it helps, please give me a like. it helps me a lot.
Program Code:
public class CourseGradeDemo
{
/*Main method to demonstrate the classes CourseGrade,
* GradedActivity, PassFailExam, Essay, FinalExam.
*/
// start main method
public static void main(String[] args)
{
GradedActivity lowest;
GradedActivity highest;
// create an object for the GradedActivity class
GradedActivity ga = new GradedActivity();
// set score of the GradedActivity
ga.setScore(80);
// create an object for the PassFailExam class
PassFailExam pfe = new PassFailExam(10, 3, 90);
// create an object for the Essay class
Essay e = new Essay(28, 19, 18, 30);
// create an object for the FinalExam class
FinalExam fe = new FinalExam(50, 12);
// create an object for the CourseGrades class
CourseGrades coursegrade = new CourseGrades();
/* call the "setLab" method with the
GradedActivity object */
coursegrade.setLab(ga);
/* call the "setPassFailExam" method with the
PassFailExam object */
coursegrade.setPassFailExam(pfe);
/* call the "setEssay " method with the Essay
object */
coursegrade.setEssay(e);
/* call the "setFinalExam " method with the
FinalExam object */
coursegrade.setFinalExam(fe);
// call the "toString" method of courseGrade
System.out.println(coursegrade);
System.out.println("\n");
double average;
average= coursegrade.getAverage();
System.out.println("Average of the scores is:\t"
+average);
System.out.println("Lowest of the scores is:\t" +coursegrade.getLowest().getScoreOf());
System.out.println("Highest of the scores is:\t" +coursegrade.getHighest().getScoreOf());
} // end of main method
} // end of CourseGradeDemo class
//Create an Interface Analyzable
public interface Analyzable
{
double getAverage();
GradedActivity getHighest();
GradedActivity getLowest();
}
Comments (1)
Step 5 of 13
//File: CourseGrades.java
// CourseGrades class implementation
public class CourseGrades implements Analyzable
{
//create an array grades of 4 GradedActivity objects
private GradedActivity[] grades = new GradedActivity[4];
// Method setLAb() which assigns the gradedActivity
//to first array element
/*
* setLab method accepts a GradedActivity object as
* a parameter and then stores this object in the
* grades array as the 0th element.*/
// setLab method implementation
public void setLab(GradedActivity ga)
{
grades[0] = ga;
} // end of setLab method
// setPassFailExam() which assigns the GradedActivity
//to second array element
/*
* The following setPassFailExam method accepts a
* PassFailExam object as a parameter and then stores
* this object in the grades array as the 1st
* element.
*/
// setPassFailExam method implementation
public void setPassFailExam(PassFailExam fe)
{
grades[1] = fe;
} // end of setPassFailExam method
// Method setEssay() which assigns the gradedActivity
//to third array element
/*
* The following setEssay method accepts an Essay
* object as a parameter and then stores this object
* in the grades array as the 2nd element.
*/
// setEssay method implementation
public void setEssay(Essay e)
Comment
Step 6 of 13
{
grades[2] = e;
} // end of setEssay method
// Method setFinalExam() which assigns the
//gradedActivity to fourth array element
/*
* The following setFinalExam method accepts
* a FinalExam object as a parameter and then stores
* this object in the grades array as the 3rd
* element.
*/
// setFinalExam method implementation
public void setFinalExam(FinalExam fe)
{
grades[3] = fe;
} // end of setFinalExam method
// Method string() which calls all the methods and
//return the values
/* The following toString method returns a string
* representation of the numeric scores and grades of
* all elements in the grades array. */
// toString method implementation
public String toString()
{
return "Lab:\n" + " Score: "
+ grades[0].getScoreOf()
+ ", Grade: " + grades[0].getGradeOf()
+ "\n\nPassFailExam:\n" + " Score: "
+ grades[1].getScoreOf() + ", Grade: "
+ grades[1].getGradeOf()
+ "\n\nEssay:\n"
+ " Score: " + grades[2].getScoreOf()
+ ", Grade: " + grades[2].getGradeOf()
+ "\n\nFinalExam:\n" + " Score: "
+ grades[3].getScoreOf() + ", Grade: "
+ grades[3].getGradeOf();
} // end of toString method
/*
* The following getAverage method computes and returns the average score
* of all elements in the grades array.
*/
// getAverage method implementation
public double getAverage()
{
double total = 0;
for (int i = 0; i < grades.length; i++)
{
total += grades[i].getScoreOf();
}
double average = total / grades.length;
return average;
} // end of getAverage method
/* The following getHighest method finds and returns
* the reference to the element with the highest
* score in the grades array. */
// getHighest method implementation
public GradedActivity getHighest()
{
GradedActivity highest = grades[0];
for (int i = 0; i < grades.length; i++)
{
if (grades[i].getScoreOf() > highest
.getScoreOf())
highest = grades[i];
}
return highest;
} // end of getHighest method
/* The following getLowest method finds and returns
* the reference to the element with the lowest score
* in the grades array.*/
// getLowest method implementation
public GradedActivity getLowest()
{
GradedActivity lowest = grades[0];
for (int i = 0; i < grades.length; i++)
{
if (grades[i].getScoreOf() < lowest.getScoreOf())
lowest = grades[i];
}
return lowest;
} // end of getLowest method
} // end of CourseGrades class
/**********************************************************
* The Following GradedActivity class demonstrates *
* the numeric score and calculating lettergrade with *
* the corresponding numeric score values *
*********************************************************/
//File: GradedActivity.java
//Implemetation of GradedActivity Class
/*Class to demonstrate the numeric score and the letter
grade calculation*/
public class GradedActivity
{
//declaring the variable scoreValue
private double scoreValue;
//setter to set a value for the scoreValue
public void setScore(double s1)
{
//setting value
scoreValue = s1;
}
//getter to get score
public double getScoreOf()
{
//return statement
return scoreValue;
}
//getter to get grade
public char getGradeOf()
{
//variable declaration
char letterGrade;
//checking each scoreValue to get a letter grade
/*Calculating the letter grade by comparing score
values of each stage*/
if (scoreValue >= 90)
letterGrade = 'A';
else if (scoreValue >= 80)
letterGrade = 'B';
else if (scoreValue >= 70)
letterGrade = 'C';
else if (scoreValue >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
//return statement
return letterGrade;
}
}
Comment
public class PassFailActivity extends GradedActivity
{
//Declare a variable for Minimum Passing Score
private double minPassScore;
public PassFailActivity(double mp)
{
minPassScore = mp;
}
@Override
public char getGradeOf()
{
char letterGrade;
if(super.getScoreOf()>=minPassScore)
letterGrade='P';
else
letterGrade='F';
return letterGrade;
}
}
//File: PassFailExam.java
//Implemetation of PassFailExam Class
public class PassFailExam extends PassFailActivity
{
// Number of questions
private int numOfQuestions;
// Points for each question
private double pointsOfEach;
// Number of questions missed
private int numOfMissed;
/*Constructor which is passing the parameters
Questions,missed, minpassing and that can be
assigned for the private data members which is
already declared above.*/
//parameterised constructor which is passing
//Questions, missed and minPassing
public PassFailExam(int questions, int missed,
double minPassing)
{
// Call the superclass constructor.
super(minPassing);
// Declare a local variable for the score.
double numericScoreValue;
// Set the numQuestions and numMissed fields.
numOfQuestions = questions;
numOfMissed = missed;
// Calculate the points for each question and
// the numeric score for this exam.
/*Calculating the points of each and numeric score value
* which can be subtracted with 100 and multiplied by the
* points of each*/
pointsOfEach = 100.0 / questions;
numericScoreValue = 100.0 - (missed *
pointsOfEach);
// Call the superclass's setScore method to
// set the numeric score.
setScore(numericScoreValue);
}
//Get method to get each points
public double getPointsOfEach()
{
//return statement
return pointsOfEach;
}
//Get method to get nummissed
public int getNumOfMissed()
{
//return statement
return numOfMissed;
}
}
public class FinalExam extends GradedActivity
{
// Number of questions
private int numOfQuestions;
// Points for each question
private double pointsOfEach;
// Questions missed
private int numOfMissed;
//Constructor for the class FinalExam
public FinalExam(int questions, int missed)
{
// To hold a numeric score
double numericScoreValue;
// Set the numQuestions and numMissed fields.
numOfQuestions = questions;
numOfMissed = missed;
/*Calculating the points of each and numeric
score value which can be subtracted with 100 and
multiplied by the points of each*/
// the numeric score for this exam.
pointsOfEach = 100.0 / questions;
numericScoreValue = 100.0 - (missed * pointsOfEach);
// Call the inherited setScore method to
// set the numeric score.
setScore(numericScoreValue);
}
//Get method to get each points
//Method to get points
public double getPointsOfEach()
{
//return statement
return pointsOfEach;
}
//Method which is getting the num of missed
//Get method to get nummissed
public int getNumOfMissed()
{
//return statement
return numOfMissed;
}
}
//File: Essay.java
// Essay class implementation
public class Essay extends GradedActivity
{
/* declare the variables to store the points for the
grammar, spelling, correct length, and content */
private int grammars;
private int spellings;
private int correctLengths;
private int contents;
/*Constructors which is passing the parameters egrammer,
* espelling, ecorrectlength and econtent */
// parameterized constructor
public Essay(int egrammar, int espelling,
int ecorrectLength, int econtent)
{
grammars = egrammar;
spellings = espelling;
correctLengths = ecorrectLength;
contents = econtent;
/*Summing the grammars, spelling, correct length
* and the contents*/
setScore(grammars + spellings + correctLengths
+ contents);
} // end of constructor
} // end of Essay class
Sample Output:
Lab:
Score: 80.0, Grade: B
PassFailExam:
Score: 70.0, Grade: F
Essay:
Score: 95.0, Grade: A
FinalExam:
Score: 76.0, Grade: C
The average of the scores is: 80.25
The lowest of the scores is: 70.0
The highest of the scores is: 95.0
Thank you. please like.
Get Answers For Free
Most questions answered within 1 hours.