Final answer:
A program that uses a class for storing student data and all given conditions in the given question is:
class Student {
private int id;
private int grade1;
private int grade2;
private int grade3;
private double averageGrade;
public Student(int id, int grade1, int grade2, int grade3) {
this.id = id;
this.grade1 = grade1;
this.grade2 = grade2;
this.grade3 = grade3;
}
// Accessor and mutator methods
public void setID(int id) {
this.id = id;
}
public void setGrade1(int grade1) {
this.grade1 = grade1;
}
public void setGrade2(int grade2) {
this.grade2 = grade2;
}
public void setGrade3(int grade3) {
this.grade3 = grade3;
}
public double calculateAverageGrade() {
averageGrade = (grade1 + grade2 + grade3) / 3.0;
return averageGrade;
}
// Static method to compare average grades
public static Student compareGrades(Student student1, Student student2) {
if (student1.calculateAverageGrade() > student2.calculateAverageGrade()) {
return student1;
} else {
return student2;
}
}
}
Step-by-step explanation:
To build a program that uses a class for storing student data, you can create a Student class with attributes such as ID number, exam grades, and average grade.
The class should have accessor and mutator methods for the grades, as well as a mutator method for changing the student ID.
You can also include a method to calculate the average grade. Additionally, you can write a static method that accepts two student objects as parameters and compares their average grades to determine the student with the highest average grade.
Here is an implementation of the Student class:
class Student {
private int id;
private int grade1;
private int grade2;
private int grade3;
private double averageGrade;
public Student(int id, int grade1, int grade2, int grade3) {
this.id = id;
this.grade1 = grade1;
this.grade2 = grade2;
this.grade3 = grade3;
}
// Accessor and mutator methods
public void setID(int id) {
this.id = id;
}
public void setGrade1(int grade1) {
this.grade1 = grade1;
}
public void setGrade2(int grade2) {
this.grade2 = grade2;
}
public void setGrade3(int grade3) {
this.grade3 = grade3;
}
public double calculateAverageGrade() {
averageGrade = (grade1 + grade2 + grade3) / 3.0;
return averageGrade;
}
// Static method to compare average grades
public static Student compareGrades(Student student1, Student student2) {
if (student1.calculateAverageGrade() > student2.calculateAverageGrade()) {
return student1;
} else {
return student2;
}
}
}
You can then create instances of the Student class in a separate driver class and test the constructors and methods.