1

I am using Eclipse and I got this error.

import static org.junit.Assert.*;

public class StudentTest extends Student {

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testGetCurrentEarnedCr() {
        Student student1 = new Student("Jane", "Smith");
        int credits = 45;
        student1.setCurrentEarnedCr(credits);
        assertEquals(credits, student1.getCurrentEarnedCr());
    }

}

I do not understand why it is giving me this error. Is it something to do with Eclipse? I looked it up online and tried the suggested solutions to fix if it was in fact an Eclipse error but it is not resolving anything.

Furthermore, here is the Student class if that helps to resolve any issues.

public class Student
{
    // Information about the individual student
    private double gpa;
    private String firstName, lastName, id;
    private int currentEarnedCr; // Current earned credits (already completed)
    private int anticipatedAdditionalCr; // Anticipated additional credits (currently taking)
    private boolean lascComplete; // Has the student completed LASC requirements
    private boolean majorComplete; //Has the student completed requirements for the major

    // Minimum number of credits required to graduate
    public static final int REQUIRED_CR = 120;

    // Keeps track of the id number to assign to the next student to be created
    private static int nextId = 1;

    /**
     * Creates a new student given a first and last name. An id number is assigned sequentially.
     *
     * @param firstName the student's first name
     * @param lastName the student's last name
     */
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = String.format("%07d",nextId); // 7 digits wide, zero padded
        nextId++;
    }

    /**
     * Returns the number of students which have been created.
     *
     * @return the number of students which have been created
     */
    public static int getStudentCount() {
        return nextId-1;
    }

    /**
     * Set the GPA of the student.
     *
     * @param gpa the student's GPA
     */
    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    /**
     * Returns the student's GPA.
     *
     * @return the student's GPA
     */
    public double getGpa() {
        return gpa;
    }

    /**
     * Sets the student's first name.
     *
     * @param firstName the student's first name
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Returns the student's first name.
     *
     * @returns the student's first name
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets the student's last name.
     *
     * @param lastName the student's last name
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Returns the student's last name.
     *
     * @returns the student's last name
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Returns the student's ID number.
     *
     * @returns the student's ID number
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the student's current earned credits.
     * (This should really be done by looking at a
     * list of completed courses, not by setting it directly.)
     *
     * @param currentEarnedCr the number of current earned credits
     */
    public void setCurrentEarnedCr(int currentEarnedCr) {
        this.currentEarnedCr = currentEarnedCr;
    }

    /**
     * Returns the student's current earned credits.
     *
     * @return the student's current earned credits
     */
    public int getCurrentEarnedCr() {
        // This should really be done by looking at a
        // list of completed courses, not by returning a variable.
        return currentEarnedCr;
    }

    /**
     * Sets the student's anticipated additional credits (the
     * courses they are currently taking).
     * (This should really be done by looking at a
     * list of registered courses, not by setting it directly.)
     *
     * @param anticipatedAdditionalCr the number of anticipated additional credits
     */
    public void setAnticipatedAdditionalCr(int anticipatedAdditionalCr) {
        this.anticipatedAdditionalCr = anticipatedAdditionalCr;
    }

    /**
     * Returns the student's anticipated additional credits.
     *
     * @return the student's anticipated additional credits
     */
    public int getAnticipatedAdditionalCr() {
        // This should really be done by looking at a
        // list of registered courses, not by returning a variable.
        return anticipatedAdditionalCr;
    }

    /**
     * Sets whether the student has completed the LASC requirements.
     * (This should really be done by looking at a
     * list of completed courses, not by setting it directly.)
     *
     * @param lascComplete whether LASC requirements are complete
     */
    public void setLascComplete(boolean lascComplete) {
        this.lascComplete = lascComplete;
    }

    /**
     * Returns whether the student has completed the LASC requirements.
     *
     * @return whether the student has completed the LASC requirements
     */
    public boolean getLascComplete() {
        // This should really be done by looking at a
        // list of complete courses, not by returning a variable.
        return lascComplete;
    }

    /**
     * Sets whether the student has completed the major requirements.
     * (This should really be done by looking at a
     * list of completed courses, not by setting it directly.)
     *
     * @param majorComplete whether major requirements are complete
     */
    public void setMajorComplete(boolean majorComplete) {
        this.majorComplete = majorComplete;
    }

    /**
     * Returns whether the student has completed the major requirements.
     *
     * @return whether the student has completed the major requirements
     */
    public boolean getMajorComplete() {
        // This should really be done by looking at a
        // list of complete courses, not by returning a variable.
        return majorComplete;
    }

    /**
     * Returns the student's remaining credits to graduate
     * (not including the courses they are currently taking).
     *
     * @return the student's remaining credits to graduate
     */
    public int getCurrentRemainingCr() {
        return REQUIRED_CR - currentEarnedCr;
    }

    /**
     * Returns the student's anticipated remaining credits to graduate
     * (including the courses they are currently taking).
     *
     * @return the student's anticipated remaining credits to graduate
     */
    public int getAnticipatedRemainingCr() {
        return getCurrentRemainingCr() - anticipatedAdditionalCr;
    }

    /**
     * Returns whether the student is ready to graduate.
     *
     * @return whether the student is ready to graduate
     */
    public boolean readyToGraduate() {
        return getCurrentRemainingCr() == 0 && gpa >= 2.0 && lascComplete && majorComplete;
    }
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • are you having a default constructor in your Student class? – Manjunath Sep 12 '14 at 03:00
  • Note: The IDE is usually irrelevant for questions relating to code. The same error would be produced if using notepad.exe for editing the code and javac directly for compiling it. – user2864740 Sep 12 '14 at 03:12
  • 1
    possible duplicate of [Java error: Implicit super constructor is undefined for default constructor](http://stackoverflow.com/questions/1197634/java-error-implicit-super-constructor-is-undefined-for-default-constructor) – user2864740 Sep 12 '14 at 03:14

2 Answers2

3

It is not an issue with eclipse but with your inheritance of Student class in Studenttest. Generally if a superclass doesnot have a default constructor then the child class should have a constructor which should explicitly call the parent class constructor. In your case, your parent Student class has only one constructor with arguments so any class which extends Student should have a constructor which explicitly calls this constructor of Student using super(firstName. lastName) else java wont allow you to extend your class.

As a solution for your problem you need not extend Student class in your StudentTest class. Generally any test class which tests a functionality of any other class need not extend it.

Manjunath
  • 1,685
  • 9
  • 9
1

Student declares a constructor: public Student(String firstName, String lastName)

When you extend Student, your subclass must invoke one of the constructors of the superclass. Since there is only one constructor for Student you must invoke that constructor. In Java, constructors are not inherited by extending a class.

Your StudentTest class extends Student but does not declare a constructor for itself. In this circumstance, the compiler will create a public, no-argument constructor for you. Unfortunately, the compiler cannot guess what the firstName and lastName values should be, so instead you are requires to write your own StudentTest constructor:

public StudentTest()
{
    super("first", "last");
}

You can of course use different values in the super call, but you will have to provide those values one way or another. It is not uncommon to mirror the same constructor arguments as the parent class and defer the values to where the test class is used:

public StudentTest(String firstName, String lastName)
{
    super(firstName, lastName);
}
William Price
  • 4,033
  • 1
  • 35
  • 54