adamrsb48 adamrsb48 - 6 months ago 9
Java Question

Problems with Parent and Child Classes

I am trying to create a constructor for class HthsStudent, where it inherits variables and states from class Student. (Student inherited variables and states from class Person.)

The problem is, java doesn't like the way I'm trying to do it, and keeps telling me that what I'm doing is wrong, even though it is exactly what I did in class Student. It tells me that there is a "invalid method declaration, return type required" error, but I don't see a way to fix it.

What am I doing wrong, and how can I fix it?

Thanks!

/**
* Write a description of class Persn here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Person
{
String firstName;
String lastName;

public Person (String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}

public String toString ()
{
return lastName + ", " + firstName;
}
}





/**
* Write a description of class Student here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Student extends Person
{
int gradeLevel;
private static int studentId = 0;

public Student (String firstName, String lastName, int gradeLevel)
{
super(firstName, lastName);
this.gradeLevel = gradeLevel;

if (gradeLevel > 5 || gradeLevel < 0)
{
gradeLevel = 0;
}

studentId = studentId + 1;
}

public int getLevel()
{
return gradeLevel;
}

public String toString ()
{
return lastName + ", " + firstName + "\nGrade Level: " + gradeLevel + "\nID #: " + studentId;
}
}




/**
* Write a description of class HTHSStudent here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HTHSStudent extends Student
{
double gpa;

public HthsStudent (String firstName, String lastName, int gradeLevel, double gpa)
{
super(firstName, LastName, gradeLevel);
this.gpa = gpa;

if (gpa > 5.0 || gpa < 1.0)
{
gpa = 0.0;
}


}

public String toString()
{
return lastName + ", " + firstName + "\nGrade Level: " + gradeLevel + "\nID #: " + studentId + "\nGPA: " + gpa;
}
}

Answer

You aren't capitalizing correctly.

In order to have the method work properly, you need to keep it consistent with the way it was declared originally in the class.

So, it'd be:

public HTHSStudent

not

public HthsStudent.

Java requires that case be followed at all times, and it's a good practice to keep it in order.