user13959111
user13959111

Reputation:

Get random operators and integers

im doing an assignment which requires me to create 3 classes.

i need a little help here as im not sure if i've done the random operators method correctly and am really lost at how i should tabulate both random int and operators together in one class.

here's the code i've done so far:

    public static char getOperator (int x, int y)
    {
        char operator;
        int answer;
        
        switch (rand.nextInt(4))
          {
            case 0: operator = '+';
                    answer = x + y;
                    break;
                    
            case 1: operator = '-';
                    answer = x - y;;
                    break;
                    
            case 2: operator = '*';
                    answer = x * y;;
                    break;
                    
            case 3: operator = '/';
                    answer = x / y;;
                    break;
                    
            default: operator = '?';
          }
        return operator;
    }

Upvotes: 0

Views: 83

Answers (1)

shaki
shaki

Reputation: 230

I believe you mean you need to create 3 methods (and not classes).

  1. One that creates a random integer (it's a bad design to create a method that returns two integers instead of just calling two times one that returns one int).
  2. One that returns a random operator
  3. One that checks if an operation consisting of "random-number random-operator random-number" is equal to user input

Anyways, there's a lot to unpack here, so:

Your first method getTwoIntegers is incorrect, you're asking for two integers in input but you never actually use them, then you return one random number.

The method getOperator needs to be redesigned to have no input and return one char (equal to + - x /).

Your final method will call the your first method twice, then the second method once, it will then print the operation for the user to see, and check if the response is correct.

Hopefully this can help you conceptualize better the way you should build your code. I didn't post the source code since I believe it's much better for you if you try to do it by yourself (this being an assignment and all)

Good luck and have fun :)

Upvotes: 1

Related Questions