// sample class to illustrate inheritance: This class represents an undergraduate Student consisting of a name (first and last), major, gpa and hours earned. // it has several constructors, accessors for all instance data, a private method to obtain the class rank, only used internally by toString, a toString and an equals // plus mutators to change major and to add results of a new class to change gpa and hoursEarned public class Student { protected String firstName, lastName, major; protected double gpa; protected int hoursEarned; public Student() // 0-arg constructor, no nothing about the student { firstName="unknown"; lastName="unknown"; major="unknown"; gpa=0.0; hoursEarned=0; } public Student(String f, String l) // assumed that this is a new student who has no major yet { firstName=f; lastName=l; major="unknown"; gpa=0.0; hoursEarned=0; } public Student(String f, String l, String m) // new student with no hours earned yet so no gpa { firstName=f; lastName=l; major=m; gpa=0.0; hoursEarned=0; } public Student(String f, String l, String m, double g, int h) // student that we know all info on { firstName=f; lastName=l; major=m; gpa=g; hoursEarned=h; } public String getFirstName() { return firstName; } // accessors for all 5 instance data public String getLastName() { return lastName; } public String getMajor() { return major; } public double getGpa() { return gpa; } public int getHours() { return hoursEarned; } public String toString() // toString returns first and last name, major, gpa and class rank (freshman, sophomore, etc) { return firstName + " " + lastName + ": " + major + ", " + String.format("%5.3f", gpa)+ ", " + getRank(); } private String getRank() // private method only called internally to convert hours into class rank (String) { if(hoursEarned>=90) return "senior"; else if(hoursEarned>=60) return "junior"; else if(hoursEarned>=30) return "sophomore"; else return "freshman"; } public void changeMajor(String newm) // mutator to change the major { major = newm; } public boolean equals(Student s) // determine if this Student has the same info as the Student parameter s { return (firstName.equals(s.getFirstName())&&lastName.equals(s.getLastName())&&major.equals(s.getMajor())&&gpa==s.getGpa()&&hoursEarned==s.getHours()); } public void newClass(char grade, int hours) // add a new class' result to the student's hours and gpa { switch(grade) { case 'A': gpa=(gpa*hoursEarned+hours*4)/(hoursEarned+hours); hoursEarned+=hours; break; case 'B': gpa=(gpa*hoursEarned+hours*3)/(hoursEarned+hours); hoursEarned+=hours; break; case 'C': gpa=(gpa*hoursEarned+hours*2)/(hoursEarned+hours); hoursEarned+=hours; break; case 'D': gpa=(gpa*hoursEarned+hours*1)/(hoursEarned+hours); hoursEarned+=hours; break; case 'F': gpa=(gpa*hoursEarned+hours*0)/(hoursEarned+hours); hoursEarned+=hours; break; default: System.out.println("Error, " + grade + " is not a legal grade"); // if not A, B, C, D, F, then illegal grade, do not change anything } } }