Question
Write a method called char randOperator() that will return a random arithmetic operator. The possible operators returned are: +, -, *, /, and %. The
Write a method called char randOperator() that will return a random arithmetic operator. The possible operators returned are: +, -, *, /, and %. The operators are just characters: '+', '-', '*', '/', and '%'. To generate a random operator, you generate a random number between 0 and 4. You can use a switch statement to return the operator character based on the integer value. For example, if the random number is 0, the method returns '+'. Here's the possible mapping of the integer value to operators: 0->'+', 1-> '-', 2->'*', 3-> '/', 4->'%'.
Write a method called int compute (char op, int first, int second) that computes the operation on the first and second operand and returns the result. For example, if op is '+', then it would return value of first + second. Hint: use switch statement.
In your main method, prompts the user for the number of expressions. Use a for loop prints random arithmetic expressions and their answers. The operands are random integers between 1 and 100.
Sample run:
How many problems? 10 87 % 66 = 21 81 % 51 = 30 47 / 70 = 0 47 * 57 = 2679 87 % 76 = 11 45 / 51 = 0 27 * 31 = 837 70 % 20 = 10 43 * 18 = 774 53 + 48 = 101 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started