Derive a child class of GradStudent by treating Student as a parent class. Add a
ID: 3694265 • Letter: D
Question
Derive a child class of GradStudent by treating Student as a parent class.
Add a new data member: Database score.
Add new function members:
(1) A default constructor and a constructor that accepts actual arguments;
Override the compAveScore() function to take into account Database score:
average score = (Mathematics score + Biology score + Database score)/3.0
In the main function, test the two classes as follows:
Declare an empty vector.
Add two Student objects to the vector:
a(“Amy”, ”Miller”, 1001, 60, 66) and b(“John”, “Smith”, 1002, 70,77).
Add two GradStudent objects to the vector:
c(“Diane”, ”Myers”, 2001, 80, 84, 88) and d(“Scott”, “Baker”, 2002, 90, 91, 98).
Loop through all objects using an iterator to print out their names, IDs and average scores in a nice format:
Amy Miller 1001 63.0
John Smith 1002 73.5
Diane Myers 2001 84.0
Scott Baker 2002 93.0
Explanation / Answer
class Test{ public static void main(String[] args) { Student a = new Student("Amy", "Miller", 1001, 60, 66); Student b = new Student("John", "Smith", 1002, 70, 77); GradStudent c = new GradStudent("Diane", "Myers", 2001, 80, 84, 88); GradStudent d = new GradStudent("Scott", "Baker", 2002, 90, 91, 98); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } } class Student { String fname; String lname; int id; double mathsScore; double biologyScore; public Student() { } public Student(String fname, String lname, int id, double mathsScore, double biologyScore) { this.fname = fname; this.lname = lname; this.id = id; this.mathsScore = mathsScore; this.biologyScore = biologyScore; } public double compAveScore(){ double average_score = (mathsScore + biologyScore )/2.0; return average_score; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getMathsScore() { return mathsScore; } public void setMathsScore(double mathsScore) { this.mathsScore = mathsScore; } public double getBiologyScore() { return biologyScore; } public void setBiologyScore(double biologyScore) { this.biologyScore = biologyScore; } @Override public String toString() { return fname+" "+lname+" "+id+" "+compAveScore(); } } class GradStudent extends Student{ double databaseScore; public GradStudent() { } public GradStudent(String fname, String lname, int id, double mathsScore, double biologyScore, double databaseScore) { super(fname,lname,id,mathsScore,biologyScore); this.databaseScore = databaseScore; } public double compAveScore(){ double average_score = (mathsScore + biologyScore + databaseScore)/3.0; return average_score; } @Override public String toString() { return fname+" "+lname+" "+id+" "+compAveScore(); } public double getDatabaseScore() { return databaseScore; } public void setDatabaseScore(double databaseScore) { this.databaseScore = databaseScore; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.