Created
February 25, 2016 00:53
-
-
Save jrdalpra/bfcd381783027f72a570 to your computer and use it in GitHub Desktop.
OO Factorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AllArgsConstructor; | |
@SuppressWarnings("serial") | |
@AllArgsConstructor | |
public class Factorial extends Number { | |
private final Integer number; | |
@Override | |
public double doubleValue() { | |
return intValue(); | |
} | |
@Override | |
public float floatValue() { | |
return intValue(); | |
} | |
@Override | |
public int intValue() { | |
if (this.number == 0) | |
return 1; | |
return this.number * new Factorial(this.number - 1).intValue(); | |
} | |
@Override | |
public long longValue() { | |
return intValue(); | |
} | |
@Override | |
public String toString() { | |
return Integer.toString(intValue()); | |
} | |
public static void main(String[] args) { | |
System.out.println(new Factorial(5)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment