fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Max
  9. {
  10. long max;
  11.  
  12. public Number constrain(Number n) {
  13. if (n.longValue() <= max)
  14. try{
  15. return n.getClass()
  16. .getConstructor(String.class)
  17. .newInstance(String.valueOf(max));
  18. }catch(Exception ex){ex.printStackTrace();}
  19. return n;
  20. }
  21.  
  22. public static void main(String[]args){
  23. Max m = new Max();
  24. m.max = 10;
  25. System.out.println(m.constrain(new Double(25)).getClass()); // class java.lang.Double
  26. System.out.println(m.constrain((int) 18).getClass()); // class java.lang.Integer
  27. }
  28. }
Success #stdin #stdout 0.09s 27696KB
stdin
Standard input is empty
stdout
class java.lang.Double
class java.lang.Integer