Reputation: 95
I'm just trying to do a simple if
statement which tells you what personal allowance you get, depending on the age that you are. There is a field for "age" in the Employee
class, which is why I have passed over the age. But I'm getting the error:
The operator > is undefined for the argument type(s) Employee, int.
Where am I going wrong? Here is my code:
double calculatePersonalAllowance(Employee age){
if (this.age < 65) {
personalAllowance = 94440;
}
else if (this.age >= 65 && <75){
personalAllowance = 10500;
}
else if (this.age >75){
personalAllowance = 10660;
return personalAllowance;
}
Upvotes: 1
Views: 1676
Reputation: 210
If in you method you want to check propery age of it's parameter you must use parameters all the way.In that case method can be static like this:
static double calculatePersonalAllowance(Employee employee)
{
double personalAllowance = 0.0;//default value
int age = employee.age;
if (age < 65)
{
personalAllowance = 94440;
}
else if (age >= 65 && age <75)
{
personalAllowance = 10500;
}
else if (age >75)
{
personalAllowance = 10660;
}
return personalAllowance;
}
Upvotes: 0
Reputation: 3806
First:
"There is a field for "age" in the Employee class, which is why I have passed over the age"
You have not passed over the age, you have done the following: (Employee age) which passes over a reference to the Employee object, not the age field. the 'age' variable between the brackets is a variable local to the method referencing the Employee object which called the method, in order to get the age you will need to either get it directly from the employee object you are referencing, or via a getter method.
Second: The way an If expression is read is as follows:
if(expression){do code} //where expression returns either true or false
Or with some operators:
if(expression && expression)
You have as follows:
else if (this.age >= 65 && <75){
//Which equates to the below:
else if(expression && <75){
The compiler is expecting an expression but finding an operand. You have age as the argument passed to your method, but also appear to be trying to access a field named age. I would recommend changing the method argument to something like (Employee emp). Then change wherever you have this.age to emp.age.
else if (emp.age >= 65 && emp.age <75){
//You effectively make
else if(expression && expression){
which satisfies the compiler!
Upvotes: 1
Reputation: 95978
else if (this.age >= 65 && <75)
Should be:
else if (this.age >= 65 && this.age <75)
It's highly recommended to read 15.23. Conditional-And Operator && and The if-then and if-then-else Statements.
Also, it's redundant to have a parameter that's never used.. If you don't use age
, remove it from the method's signature:
double calculatePersonalAllowance()
Don't worry, the age
you're referring to is the class member and not the local one.
Upvotes: 6
Reputation: 26094
I think this.age
need to be change as employee.age
and (this.age >= 65 && <75)
need to change as if (employee.age >= 65 && employee.age<75)
the complete method as below.
double calculatePersonalAllowance(Employee employee){
double personalAllowance = 0;
if (employee.age < 65) {
personalAllowance = 94440;
}else if (employee.age >= 65 && employee.age<75){
personalAllowance = 10500;
}else if (employee.age >75){
personalAllowance = 10660;
}
return personalAllowance;
}
Upvotes: 1