```
public class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<Integer>();
while(!set.contains(n)) {
set.add(n);
n = calculate(n);
if(n == 1) {
return true;
}
}
return false;
}
public int calculate(int n) {
int pos;
int sum = 0;
while(n != 0) {
pos = n%10;
sum += square(pos);
n /= 10;
}
return sum;
}
public int square(int n) {
return (int)Math.pow(n, 2);
}
}
```