1

I must write simple calculator in java. I wrote in Eclipse my code and I have two bugs in dodaj and odejmij methods (the same).

Error in Eclipse - insert "EnumBody" to complete BlockStatement

I looked for how to resolve it but I didn't find. Please help me. Does someone have the same problem? Thanks a lot for help me.

    import java.util.Scanner;
    class Kalkulator {
    static Object z1=new LiczbaZespolona();
    static Object z2=new LiczbaZespolona();
    String dodaj="+";
    String odejmij="-";
    String pomnoz="*";
    String podziel="/";
    String wynik;
    static String d;
    String dzialanie=d;
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        System.out.println("Podaj pierwszą liczbę: ");
        z1 = input.next();

        System.out.println("Podaj drugą liczbę: ");
        z2 =  input.next();

        System.out.println("wybierz działanie: ");
         d = input.next();


        public static dodaj();{//insert "EnumBody" to complete BlockStatement, insert "enum Identifier" to complete EnumHeaderName


                 z3=z1+z2;
                return;
            }
             public static odejmij();{//insert "EnumBody" to complete BlockStatement, insert "enum Identifier" to complete EnumHeaderName

                z4=z1-z2;
                return;
            }



            switch (wynik){
            case 1:
                if (d=='+'){
                    return dodaj;}
                    break;
            case 2:
                if(d=='-'){
                    return odejmij;
                }
                break;
            }   
        }
}
user978758
  • 609
  • 3
  • 17
  • 29

3 Answers3

0

1) public static dodaj();{

Remove colon. When you add colon it would be treated as abstract method.

public static dodaj(){

2) Move your methods to outside main and inside class body.

kosa
  • 65,990
  • 13
  • 130
  • 167
0
  1. Remove semicolons (;) from public static dodaj();{ and public static odejmij();{.

  2. Move them out of main

  3. Add Object as return type.

  4. Return z3 and z4 from methods.

  5. In main, where you are calling dodaj and odejmij, add () parenthesis

  6. I am not sure about your LiczbaZespolona, hence not able to comment on + operator being used, but it sounds inappropriate.

Finally it should look like:

public static void main(String args[]){

   Scanner input = new Scanner(System.in);
   System.out.println("Podaj pierwszą liczbę: ");
   z1 = input.next();

   System.out.println("Podaj drugą liczbę: ");
   z2 =  input.next();

   System.out.println("wybierz działanie: ");
   d = input.next();


    switch (wynik){
    case 1:
        if (d=='+'){
            return dodaj();
         }
         break;
    case 2:
        if(d=='-'){
            return odejmij();
        }
        break;
    }   
  }

    public static Object dodaj(){
         Object z3=z1+z2;
         return z3;
    }

    public static Object odejmij(){
        Object z4=z1-z2;
        return z4;
    }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

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));

    }

}
Community
  • 1
  • 1