Create a Gradebook program: Create a gradebook class. The data being stored in t
ID: 3830725 • Letter: C
Question
Create a Gradebook program: Create a gradebook class.
The data being stored in the class is the student's name, assignments (up to five assignments - use an array), and grade for each assignment.
Note that each assignment can be a number (1) so a single array can be used for both assignments and grades. Make all attributes private.
Include mutator and accessor methods as needed.
Include a method to calculate the student's average.
Include a method to display the gradebook: On the first line display the student's name.
On the subsequent lines display each Assignment number and grade on a separate line.
On the last line display the average.
Include output messages explaining what is being displayed.
Add a default constructor to initialize all variables and all elements of arrays.
Do not include a main method in the Gradebook class.
Create a test program for the gradebook class.
Add a main method: Do not take any user input.
Hard code all data. Create three objects of type gradebook.
Add student names for each objects.
Add grades for five assignments for each student.
Call the display method for each object.
Explanation / Answer
Gradebook.java
public class Gradebook {
// Declaring instance variables
private String name;
private int assignments[];
private int grade[];
// Zero argumented constructor
public Gradebook() {
super();
this.name = "";
this.assignments = new int[5];
for (int i = 0; i < assignments.length; i++) {
assignments[i] = i + 1;
}
this.grade = new int[5];
}
// Setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getAssignments() {
return assignments;
}
public void setAssignments(int[] assignments) {
this.assignments = assignments;
}
public int[] getGrade() {
return grade;
}
public void setGrade(int[] grade) {
this.grade = grade;
}
// This method will calculate the average
public double calAverage() {
int sum = 0;
for (int i = 0; i < grade.length; i++) {
sum += grade[i];
}
return (double) sum / grade.length;
}
// this method will display the student name,grades in each assignment
public void display() {
System.out.println("Student Name :" + name);
for (int i = 0; i < grade.length; i++) {
System.out.println("Assignemnt#" + assignments[i] + " = "
+ grade[i]);
}
System.out.println("Average :" + calAverage());
}
}
_____________________
Test.java
public class Test {
public static void main(String[] args) {
System.out.println("_______Student#1_______");
// Creating an Object to the Gradebook class
Gradebook std1 = new Gradebook();
// Setting the name
std1.setName("Kane Williams");
int grades1[] = { 67, 78, 98, 87, 77 };
// Setting the grades
std1.setGrade(grades1);
// Displaying the output
std1.display();
System.out.println("_______Student#2_______");
// Creating an Object to the Gradebook class
Gradebook std2 = new Gradebook();
// Setting the grades
std2.setName("Mitchel Johnson");
int grades2[] = { 87, 88, 98, 87, 67 };
// Setting the grades
std2.setGrade(grades2);
// Displaying the output
std2.display();
System.out.println("_______Student#3_______");
// Creating an Object to the Gradebook class
Gradebook std3 = new Gradebook();
// Setting the grades
std3.setName("Pat Simcox");
int grades3[] = { 67, 78, 88, 67, 77 };
// Setting the grades
std3.setGrade(grades3);
// Displaying the output
std3.display();
}
}
______________________
Output:
_______Student#1_______
Student Name :Kane Williams
Assignemnt#1 = 67
Assignemnt#2 = 78
Assignemnt#3 = 98
Assignemnt#4 = 87
Assignemnt#5 = 77
Average :81.4
_______Student#2_______
Student Name :Mitchel Johnson
Assignemnt#1 = 87
Assignemnt#2 = 88
Assignemnt#3 = 98
Assignemnt#4 = 87
Assignemnt#5 = 67
Average :85.4
_______Student#3_______
Student Name :pat Simcox
Assignemnt#1 = 67
Assignemnt#2 = 78
Assignemnt#3 = 88
Assignemnt#4 = 67
Assignemnt#5 = 77
Average :75.4
________________Thank You