In: Computer Science
Create two child classes, UnderGraduateStudent and GraduateStudent that will extend from the Student class. Override the char getLetterGrade() method in each of the child classes.
Use Student.java class defined below: (complete and compile)
class Student {
private int id;
private int midtermExam;
private int finalExam;
public double calcAvg() {
double avg;
avg = (midtermExam + finalExam) / 2.0;
return avg;
}
public char getLetterGrade() {
char letterGrade = ‘ ‘;
return letterGrade;
}
}
For the GraduateStudent class, the lowest passing grade in the getLetterGrade() is a ‘C’.
90-100 =A
80-89 = B
70-79 =C
<70 = F
For the UnderGraduateStudent, the lowest passing grade in the getLetterGrade() is a ‘D’.
90-100 =A
80-89 = B
70-79 =C
60-69 = D
<60 =F
class UnderGraduateStudent extends Student {
}
class GraduateStudent extends Student {
}
Create a main() method in a separate class to test child classes created.
The program is given below. The comments are provided for the better understanding of the logic.
class StudentTest{
public static void main(String[] args) {
Student s;
//Call the classes for testing.
//Print the returned values.
//The getLetterGrade method will call the corresponding overridden method,
//depending on whether the calling instance is UnderGraduateStudent or GraduateStudent
System.out.println("UnderGraduateStudents");
s = new UnderGraduateStudent(1001, 89, 98);
System.out.println("Average: " + s.calcAvg());
System.out.println("Grade: " + s.getLetterGrade());
s = new UnderGraduateStudent(1001, 40, 34);
System.out.println("Average: " + s.calcAvg());
System.out.println("Grade: " + s.getLetterGrade());
System.out.println("\nGraduateStudents");
s = new GraduateStudent(1002, 55, 66);
System.out.println("Average: " + s.calcAvg());
System.out.println("Grade: " + s.getLetterGrade());
s = new GraduateStudent(1002, 55, 98);
System.out.println("Average: " + s.calcAvg());
System.out.println("Grade: " + s.getLetterGrade());
}
}
class Student {
private int id;
private int midtermExam;
private int finalExam;
//This constructor is required to pass the id and marks and assign it to the private members..
public Student(int id, int midtermExam, int finalExam) {
this.id = id;
this.midtermExam = midtermExam;
this.finalExam = finalExam;
}
public double calcAvg() {
double avg;
avg = (midtermExam + finalExam) / 2.0;
return avg;
}
public char getLetterGrade() {
char letterGrade = ' ';
return letterGrade;
}
}
class UnderGraduateStudent extends Student {
//This constructor is required to pass the id and marks and assign it to the private members.
public UnderGraduateStudent(int id, int midtermExam, int finalExam) {
//Call the base class constructor
super(id, midtermExam, finalExam);
}
@Override
public char getLetterGrade() {
//Calculate the grade based on the conditions given and return the value.
char letterGrade = ' ';
double avg = calcAvg();
if(avg >= 90)
letterGrade = 'A';
else if(avg >= 80)
letterGrade = 'B';
else if(avg >= 70)
letterGrade = 'C';
else if(avg >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
}
class GraduateStudent extends Student {
//This constructor is required to pass the id and marks and assign it to the private members.
public GraduateStudent(int id, int midtermExam, int finalExam) {
//Call the base class constructor
super(id, midtermExam, finalExam);
}
@Override
public char getLetterGrade() {
//Calculate the grade based on the conditions given and return the value.
char letterGrade = ' ';
double avg = calcAvg();
if(avg >= 90)
letterGrade = 'A';
else if(avg >= 80)
letterGrade = 'B';
else if(avg >= 70)
letterGrade = 'C';
else
letterGrade = 'F';
return letterGrade;
}
}
The screenshots of the code and output are provided below.