Hi, I have a super class Person and its sub class Student. Student has a toString method that prints out the details of the Student object including the first name. But I keep getting the error described in the title. Is there another way to fix this problem without making the method static?
Person super class
public class Person { private String firstName, lastName, id; /** * Creates a Person object. * * @param firstName the first name of the Person * @param lastName the last name of the Person * @param id the id number of the Person */ public Person(String firstName, String lastName, String id) { this.firstName=firstName; this.lastName=lastName; this.id=id; } /** * Returns the first name. * * @return the firstName */ public String getFirstName() { return firstName; } /** * Sets the first name. * * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * Returns the last name. * * @return the lastName */ public String getLastName() { return lastName; } /** * Sets the last name. * * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * Returns the id. * * @return the id */ public String getId() { return id; } }
Student sub class:
import java.util.ArrayList; public class Student extends Person { private double gpa; private ArrayList<CourseAttempted> transcript; private int creditsCompleted; /** * Creates a student. * * @param firstName the first name of the student * @param lastName the last name of the student * @param id the student's id */ public Student(String firstName, String lastName, String id) { super(firstName, lastName, id); gpa = 0.0; transcript = new ArrayList<CourseAttempted> (); creditsCompleted = 0; } /** * @return the gpa */ public double getGpa() { return gpa; } /** * Adds a course to the student's transcript. * * @param courseToAdd the course to add */ public void addCourse(Course c, String semester, int year, double grade) { CourseAttempted courseToAdd = new CourseAttempted(c, semester, year, grade); transcript.add(courseToAdd); // the total number of credits attempted is the sum of // creditsCompleted and the number of credits of the course to be added // to the transcript int creditsAttempted = courseToAdd.course.getCredits() + creditsCompleted; // gpa is computed by doing the following: // for each course attempted, multiply the number of credits by the // course grade. // gpa is the sum of the above for all the courses divided by the // number of total credits // double currentPoints = gpa * creditsCompleted + courseToAdd.grade * courseToAdd.course.getCredits(); gpa = currentPoints / creditsAttempted; if (courseToAdd.grade > 0) creditsCompleted += courseToAdd.course.getCredits(); } /** * Returns the list of courses as a String. * * @return the list of courses the student has taken */ public String getTranscript() { String courseList = ""; for (CourseAttempted ca: transcript) courseList = courseList + ca + "\n"; return courseList; } /** * Returns a String representation of a Student * * @return a string representing the student */ public String toString() { String s = this.getClass().getName().toUpperCase() + ":\t" + Person.getFirstName() + " " + lastName + "\tID = " + id + "\tGPA = " + gpa + "\tCredits Completed = " + creditsCompleted; s += "\nHere are all the courses the student has taken so far"; s += "\n-----------------------------------------------------"; s += "\n" + getTranscript(); return s; } }