I am trying to write a method that determines if the square of the age of a person is equal to the current year. For example a man was aged 43 in 1849 and the square of 43 is 1849.
Below is my code for it but I dont know why my exception is not working because it is assumed that no body can live past 123 years
public Boolean isAlive(int y){
assert( y>=1888 && y<=2134);
int age=0;
while(age<=123){
y=y+1;
age=age+1;
if(age*age==y){
int c=age;
return true;
}
}
return false;
}
The problem you are trying to solve appears to be
where
yis the starting year andnis the age and both must be a positive whole number.Rearranged this is
e.g.
when y = 1806, n = 43, n + y = n * n = 1849.
when y = 1980, n = 45, n + y = 2025, n * n = 2025.
So you can write