These are the two important pieces of code:
public int setCreditHrs()
{
return creditHrs;
}
if(!student.setCreditHrs(hrs))
Mistakes:
1. As you can see, in the first piece of code, you have declared your method as "public int setCreditHrs()". What this means is that the method returns an int (where it says "int" before the setCreditHrs) and when you call this method, you cannot call it using an argument (the () have nothing in between them). So to call this method correctly you'd use student.setCreditHrs(). You said "student.setCreditHrs(hrs)", which is incorrect, because you are passing one argument to a method which was declared as taking no arguments. As an example, if you wanted to pass a float argument to your setCreditHrs method, the method would be defined as
public int setCreditHrs(float nameYouGiveItHere)
{
return creditHrs;
}
And it would be called by student.setCreditHrs(someFloatHere).
2. Methods which are declared as "setWhatever" usually "set" the whatever to something. Currently, the method declaration you posted "gets" something (it returns something). So your method is named one thing, but it is doing another thing. An example of a typical set method:
public class Example{
float something;
public void setSomething(float otherThing){
something = otherThing;
}
}
3. if(!student.setCreditHrs(hrs)): This will produce an error since you can only use an if statement to check boolean values for true or false. So (the way you have your code as of your first post) something like if (student.setCreditHrs() == 0) will work, because "==" evaluates …