Beside the issues that SO users wrote, you will have more problem with the arithmetic you are doing. As Java do not support operator overload. What this mean for you is that if you have two objects of class LiczbaZespolona
(ComplexNumber), compiler will not know what to do with it and will fail to compile.
To solve that issue you will need two thing implement the arithmetic of those number in some class. For sure you will need some point to start and this interface may be helpful
private static interface IComplexNumber {
public IComplexNumber add(IComplexNumber complexNumber);
public IComplexNumber substract(IComplexNumber complexNumber);
public IComplexNumber multiply(IComplexNumber complexNumber);
public IComplexNumber divide(IComplexNumber complexNumber);
}
Then when you know how to add two ComplexNumber you will need something that can do the operation for you. For this type of operations best thing is enum type.
The class could look start this
private enum CompleNumberCalculation {
ADD('+') {
@Override
protected IComplexNumber operation(IComplexNumber left, IComplexNumber right) {
return left.add(right);
}
};
private char operator;
private CompleNumberCalculation(char operator) {
this.operator = operator;
}
protected abstract IComplexNumber operation(IComplexNumber left, IComplexNumber right);
}
To help you out I wrote simple MathOperation
enum that solve calculation for RegularNumber
public enum RegularMathOperation {
ADD('+') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.add(right);
}
},
SUBSTRACT('-') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.subtract(right);
}
},
MULTIPLY('*') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.multiply(right);
}
},
DIVIDE('/') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.divide(right,MathContext.DECIMAL64);
}
};
private final char operator;
private RegularMathOperation(char operator) {
this.operator = operator;
}
public char operator() {
return this.operator;
}
public <T1 extends Number, T2 extends Number> T1 calculate(T1 left, T2 right) {
validateInput(left, right);
Class<? extends Number> resultType = left.getClass();
BigDecimal result = this.operation(toBigDecimal(left), toBigDecimal(right));
return (T1) toType(resultType, result);
}
protected abstract BigDecimal operation(BigDecimal left, BigDecimal right);
private void validateInput(Number left, Number right) {
if(left == null) {
throw new IllegalArgumentException("Value of left argument must not be null to perform operation " + this.name());
}
if(right == null) {
throw new IllegalArgumentException("Value of left argument must not be null to perform operation " + this.name());
}
}
private BigDecimal toBigDecimal(Number value) {
if(value instanceof BigDecimal) {
return (BigDecimal) value;
}
return new BigDecimal(value.doubleValue());
}
private Number toType(Class<? extends Number> type, BigDecimal value) {
if(Double.class.isAssignableFrom(type)) {
return type.cast(value.doubleValue());
}
if(Long.class.isAssignableFrom(type)) {
return type.cast(value.longValue());
}
if(Integer.class.isAssignableFrom(type)) {
return type.cast(value.intValue());
}
//...
throw new IllegalStateException("Type not support: "+type);
}
public static Map<Character, RegularMathOperation> operatorMap = new HashMap<Character, RegularMathOperation>();
static {//Fill map
for(RegularMathOperation mathOperation : RegularMathOperation.values()) {
operatorMap.put(mathOperation.operator(), mathOperation);
}
}
public static RegularMathOperation valueOf(char operator) {
RegularMathOperation mathOperation = operatorMap.get(Character.valueOf(operator));
if(mathOperation == null) {
throw new IllegalArgumentException("Could not find MathOperator for operartor: " + operator);
}
return mathOperation;
}
public static <T1 extends Number, T2 extends Number> T1 resultOf(char operator, T1 left, T2 right) {
return RegularMathOperation.valueOf(operator).calculate(left, right);
}
public static void main(String[] args) {
Long left = 3L;
Double right = 2D;
System.out.println(RegularMathOperation.resultOf('+', left, right));
System.out.println(RegularMathOperation.resultOf('-', left, right));
System.out.println(RegularMathOperation.valueOf('*').calculate(left, right));
System.out.println(RegularMathOperation.valueOf('*').calculate( left, right));
System.out.println(RegularMathOperation.DIVIDE.calculate(left, right));
System.out.println(RegularMathOperation.DIVIDE.calculate(right,left));
}
}