In: Computer Science
In Java design a class named Course to represent a course. The class contains:
Private data fields:
• courseName of type String
• courseNumber of type String
• section of type int
• numberStudents of type int
• finalGrades array of 100 double numbers
A constructor that creates a course with the specified name, number, section , and number of students.
A method setGrades() that will prompt the user to the enter the grades into double [] finalGrades.
int numberStudents, will control how many cells will be used.
A method to find and print the maximum grade.
A method to find and print the average grade.
Implement the class.
Write a test program that
Creates the course course1/CS155 with the values (CS, 155, 01, 20)
Uses setGrades to read 20 grades for the course course1/CS155. Note numberStudents or
finalGrades.length can be used in your for loop holding the value of 20.
print the maximum grade for course1/CS155
print the average grade for course1/CS155
Creates the course course2/CS300 with the values (CS, 300, 03, 30).
Uses setGrades to read 30 grades for the course course2/CS300. Note numberStudents or finalGrades.length can be used in your for loop holding the value of 20.
print the maximum grade for course2/CS300
print the average grade for course2/CS300
Solution:
import java.util.*;
public class Course //class definition
{
private String courseName; //variable declaration
private String courseNumber;
private int section;
private int numberStudents;
double[] finalGrades=new double[100];
Course(String courseName,String courseNumber,int section,int
numberStudents){ //constructor of class
this.courseName=courseName;
this.courseNumber=courseNumber; //store the data in respective
variables
this.section=section;
this.numberStudents=numberStudents;
}
void setGrades(){ //method to read grades from user
System.out.println("Enter grades");
Scanner input=new Scanner(System.in); //Use Scanner class to read
data
for(int i=0;i< this.numberStudents;i++){
finalGrades[i]=input.nextDouble(); //Read and store grades in
array
}
}
void max(){ //method to find maximum grade
double max=finalGrades[0];
for(int i=1;i< this.numberStudents;i++){ //For every grade in
array
if(max<finalGrades[i]){ //compare with max
max=finalGrades[i]; //If max is less than any element, update max
with it
}
}
System.out.print("The maximum grade is ");
System.out.println(max); //print maximum grade
}
void average(){ //method to find average grade
double sum=0.0;
for(int i=0;i< this.numberStudents;i++){ //For every grade in
array
sum+=finalGrades[i]; //add it to sum
}
sum=sum/numberStudents; //find average grades
System.out.print("The average grade is ");
System.out.println(sum); //print average
}
public static void main(String[] args) {
Course course1=new Course("CS","155",01,20); //create object
course1.setGrades(); //call method to read grades
course1.max(); //call method to find maximum grade
course1.average(); //call method to find average
grade
Course course2=new Course("CS","300",03,30); //create
object
course2.setGrades(); //call method to read grades
course2.max(); //call method to find maximum grade
course2.average(); //call method to find average grade
}
}
Screenshots:
The screenshots are attached below for reference along with output. Please follow them.


