Java: implement a program that calculates and displays the grade average for a t
ID: 3605796 • Letter: J
Question
Java:
implement a program that calculates and displays the grade average for a test.
service class Test should have one instance variable: courseName, secondary constructor, and two business methods: calcAverage and toString.
calcAverage method accepts one varargs parameter grades and calculates the grade average based on the passed content
client class should:
create Test object
client should not create any array, it should call calcAverage method three times to calculate the average for three tests:
first call should pass 5 grades,
second call should pass 11 grades,
and the third call should pass 7 grades.
client should display the result just after receiving the average from the method
Explanation / Answer
Please find my implementation.
public class Test {
private String courseName;
public Test(String name){
courseName= name;
}
public double calcAverage(int...args) {
double sum = 0;
for(int i=0; i<args.length; i++)
sum += args[i];
return sum/args.length;
}
@Override
public String toString() {
return "Course Name: "+courseName;
}
}
##########
public class Client {
public static void main(String[] args) {
Test t1 = new Test("CSE 234");
System.out.println(t1.toString()+", Average grade: "+t1.calcAverage(56,76,88,76,45));
System.out.println(t1.toString()+", Average grade: "+
t1.calcAverage(44,76,55,88,99,78,76,56,76,88,76));
System.out.println(t1.toString()+", Average grade: "+t1.calcAverage(64,76,87,88,67,84,90));
}
}
/*
Sample run;
Course Name: CSE 234, Average grade: 68.2
Course Name: CSE 234, Average grade: 73.81818181818181
Course Name: CSE 234, Average grade: 79.42857142857143
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.