-3

I'm trying to evaluate an equation randomly generated by the system where the two integers are stored in an array and the operator is in a separate string array. I want to compare the answer with the user's answer. here is my code:

Integer[] array;
String[] operators = {"+", "-", "*", "/"};
String question;
int operator = 0;

public void generateSum(){
    for (int x = 0; x < 2; x++) {
        Random randomNumber = new Random(); //creating random object
        int number = randomNumber.nextInt(100) + 1; 
        Random randomOperator = new Random();
        operator = randomOperator.nextInt(3);
        array[x] = (number);
    }
    question = array[0].toString() + operators[operator] + array[1].toString() + "=";
    TextView txt = findViewById(R.id.txtQuestion);
    txt.setText(question);
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
BzeQ
  • 93
  • 1
  • 11
  • Map string to callback with operation... use enum with operation method... make class with name and operation method... Use tuple with name and callback as pair... Callback method obviously should take two integers as params and returns result of given operation... – Selvin Mar 14 '18 at 21:03

2 Answers2

0

You can call this function and pass the integers array and the operator as parameters, and you'll get the result of the operation:

public int result(int[]array, int operator){
    switch(operator){
        case 0: // addition
            return array[0]+array[1];
            break;
        case 1: // subtraction
            return array[0]-array[1];
            break;
        case 2: // multiplication
            return array[0]*array[1];
            break;
        case 3: // division - result rounded up to integer
            return array[0]/array[1];
            break;
        default: // this shouldn't happen
            return 0;  
     }
}

Then you can compare the answer given by user with the calculated result.

By the way your array should be of type int[] and not Integer[]. int is a primitive type representing the integer number, and Integer is a class with many static functions and one int field. If you are interested, you can look into this post about difference between int and Integer:

What is the difference between Integer and int in Java?

colens
  • 467
  • 3
  • 12
0

What if you had an interface that looked like this:

interface Operator {
    public String getView();
    public int operate(int v1, int v2);
}

so instead of String[] operators you'd have Operator[] operators.

Your loop would then be independent of your implementation, you would just have:

question = "" + array[0] + operators[operator].getView() + 
     array[1] + "=" + operators[operator].operate(array[0],array[1]); 

Now, how you implement the operate method is up to you, it could be 4 different classes implementing Operator, or it could be enums implementing Operator or it could be a single class that acts differently if it was constructed with a "+" or a "-" passed in.

The nice part is that you can change the "Operaton" implementation completely separately from the rest of your logic. It will also be easier to understand the two pieces.

As an example, one class, "Plus" could be implemented like this:

public class Plus implements Operator
{
    public String getView(){return "+";}
    public int operate(int v1, int v2){return v1+v2)
}

Implementing 4 classes like that is cake. You could do more of course (Lambda's for instance, or a base-class that implements getView() so your sub-classes only need to implement operate()...)

(Note, in my second code block, starting with ""+ is a trick to "Start out" as a string which eliminates the need for the .toString() on array[0] and makes the line's length less awkward)

Bill K
  • 62,186
  • 18
  • 105
  • 157