Java problem with jenarys



  • I'm trying to make arithmetic operation and, depending on the number to be left on the screen, the method returned or int or double.

    I'm doing this:

    @FunctionalInterface
    public interface JoinMathAction<T extends Number>
    {
        T mathAction(Number a, Number b);
    }
    

    public class GoMath
    {
    public <T extends Number> T getResultMath(T arg1, T arg2, String op)
    {
    Number res = 0;

        if (arg1 instanceof Integer &amp;&amp; arg2 instanceof Integer) {
            JoinMathAction&lt;Integer&gt; mm = (a, b) -&gt;
            {
                int argFirst = a.intValue(), argSec = b.intValue();
                switch (op) {
                    case "/": {
                        if(argFirst == 0 || argSec == 0)
                        {
                            try
                            {
                                throw new DivZeroException("Ділення на 0 неможливе");
                            }
                            catch (DivZeroException d)
                            {
                                System.out.println(d.getMessage());
                            }
                        }
                        else
                        {
                            return argFirst / argSec;
                        }
                    }
                    case "*": {
                        return argFirst * argSec;
    
                    }
                    case "-": {
                        return argFirst - argSec;
                    }
                    case "+": {
                        return argFirst + argSec;
                    }
                }
                return 0;
            };
    
            res = mm.mathAction(arg1, arg2);
    
        } else if (arg1 instanceof Double || arg2 instanceof Double) {
            JoinMathAction&lt;Double&gt; mm = (a, b) -&gt;
            {
                double argFirst = a.doubleValue(), argSec = b.doubleValue();
                double result = 0;
    
                switch (op) {
                    case "/": {
                        if(argFirst == 0 || argSec == 0)
                        {
                            try
                            {
                                throw new DivZeroException("Ділення на 0 неможливе");
                            }
                            catch (DivZeroException d)
                            {
                                System.out.println(d.getMessage());
                            }
                        }
                        else
                        {
                            return argFirst / argSec;
                        }
                    }
                    case "*": {
                        return argFirst * argSec;
                    }
                    case "-": {
                        return argFirst - argSec;
                    }
                    case "+": {
                        return argFirst + argSec;
                    }
                }
                return result;
            };
    
            res = mm.mathAction(arg1, arg2);
        }
        return (T) res;
    }
    

    }

    It's working, but how do we make two switch one? How do you do that?



  • That's not the problem. Genericautoboxing♪ Class Integer and Double No arithmetic operation is also identified, and if you try to compil the type code:

    void add(Integer b, Integer c){
        Integer a = b + c;
    }
    

    In Java 1.4, you can't do anything, the code starts only with version 1.5 when you actually showed up. autoboxing♪ And in Java 1.5, the code will be:

    void add(Integer b, Integer c) {
       Integer a = Integer.valueOf(b.intValue() + c.intValue());
    }
    

    For class Number You can't figure out how to convert it, you can't use arithmetic surgery.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2