In: Computer Science
Java question, not sure how to do this...
You need to create a base class that should have the following functionality.
- Calculate avg. Students grade. Input parameter to this method is an array that shows grades for at least ten courses.
You need to create a child class that inherits the base class. The child class should have a method to calculate max grade from 10 courses.
You need to write a demo class. The demo class should have a main method that calls child class and able to provide max and avg. Grade.
Here is the completed code for this problem. Please save the code for each class in separate files namely – GradeStats.java, GradeStatsExtended.java and Demo.java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// GradeStats.java
//super class
public class GradeStats {
// method to calculate the average of given grades
public double calcAverage(double grades[]) {
double sum = 0;
// summing up all grades in the array
for (double d : grades) {
sum += d;
}
// finding and returning the average
double avg = (double) sum / grades.length;
return avg;
}
}
// GradeStatsExtended.java
//child class extending the GradeStats class
public class GradeStatsExtended extends GradeStats {
// method to calculate the max grade from given grades
public double calcMaxGrade(double grades[]) {
double max = 0;
for (int i = 0; i < grades.length; i++) {
// if this is first grade or this grade > current max, updating max
if (i == 0 || grades[i] > max) {
max = grades[i];
}
}
return max;
}
}
// Demo.java , for testing
public class Demo {
public static void main(String[] args) {
// creating an array containing 10 grades
double grades[] = { 90, 70, 85, 92, 78, 88, 83, 83, 90, 79 };
// creating an object of GradeStatsExtended (child class)
GradeStatsExtended stats = new GradeStatsExtended();
// displaying the average using calcAverage. super class's calcAverage
// method will be invoked.
System.out.println("Average grade: " + stats.calcAverage(grades));
// displaying max grade. GradeStatsExtended's own calcMaxGrade method
// will be invoked
System.out.println("Max grade: " + stats.calcMaxGrade(grades));
}
}
//OUTPUT
Average grade: 83.8
Max grade: 92.0