Question

Problem 1: Basic Calculator Given two integers, ’x’ and ’y’ and a character representing an arithmetic...

Problem 1: Basic Calculator Given two integers, ’x’ and ’y’ and a character representing an arithmetic operator, ’op’. Return the result of ’x’ ’op’ ’y’ if possible, otherwise return -1. .java = BasicCalculator.java Examples (a) basicCalculator(2, 3, ’+’) returns 5 (b) basicCalculator(7, 3, ’%’) returns 1. (c) basicCalculator(5, 0, ’%’) prints an error message and returns -1 (5%0 is invalid).

Homework Answers

Answer #1
public class BasicCalculator {

    public static int basicCalculator(int x, int y, char op) {
        if (op == '+') {
            return x + y;
        } else if (op == '-') {
            return x - y;
        } else if (op == '*') {
            return x * y;
        } else if (op == '/') {
            if (y == 0) {
                return -1;
            }
            return x / y;
        } else if (op == '%') {
            if (y == 0) {
                return -1;
            }
            return x % y;
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(basicCalculator(2, 3, '+'));
        System.out.println(basicCalculator(7, 3, '%'));
        System.out.println(basicCalculator(5, 0, '%'));
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
The table below shows annual returns for stocks of companies X and Y. Calculate the arithmetic...
The table below shows annual returns for stocks of companies X and Y. Calculate the arithmetic average returns. In addition, calculate their variances, as well as their standard deviations.    Returns Year X Y 1 7 %     22 %     2 25         43         3 14         -9         4 -15         -23         5 16         51             Requirement 1: (a) The arithmetic average return of company X's stock is: (Click to select)  7.61%  10.62%  9.40%  11.75%  11.47%     (b) The...
The table below shows annual returns for stocks of companies X and Y. Calculate the arithmetic...
The table below shows annual returns for stocks of companies X and Y. Calculate the arithmetic average returns. In addition, calculate their variances, as well as their standard deviations.    Returns Year X Y 1 15 %     18 %     2 33         39         3 22         -11         4 -23         -25         5 24         47             Requirement 1: (a) The arithmetic average return of company X's stock is:   (Click to select)   14.20%   16.05%   11.50%   17.75%   17.32%     (b) The...
Problem 12-7 Calculating Returns and Variability [LO1] Returns Year X Y 1 12 % 23 %...
Problem 12-7 Calculating Returns and Variability [LO1] Returns Year X Y 1 12 % 23 % 2 15 27 3 11 11 4 – 12 – 13 5 10 17    Using the returns shown above, calculate the arithmetic average returns, the variances, and the standard deviations for X and Y. (Do not round intermediate calculations. Enter your average return and standard deviation as a percent rounded to 2 decimal places, e.g., 32.16, and round the variance to 5 decimal...
Returns Year X Y 1 13% 20% 2 31 41 3 20 -7 4 -21 -21...
Returns Year X Y 1 13% 20% 2 31 41 3 20 -7 4 -21 -21 5 22 49 1) Calculate the arithmetic average return for X. Calculate the arithmetic average for Y. 2) Calculate the variance for X and Y. Calculate the arithmetic average for Y. 3) Calculate the standard deviation for X. Calculate the standard deviation for Y.
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
a)Program a calculator or computer to use Euler's method to compute y(1), where y(x) is the...
a)Program a calculator or computer to use Euler's method to compute y(1), where y(x) is the solution of the given initial-value problem. (Give all answers to four decimal places.) dy dx + 3x2y = 9x2, y(0) = 4 h = 1     y(1) = h = 0.1     y(1) = h = 0.01     y(1) = h = 0.001     y(1) = (b) Verify that y = 3 + e−x3 is the exact solution of the differential equation. y = 3 + e−x3      ⇒     y'...
The table below shows annual returns for stocks of companies X and Y. Calculate the arithmetic...
The table below shows annual returns for stocks of companies X and Y. Calculate the arithmetic average returns. In addition, calculate their variances, as well as their standard deviations. PLEASE SHOW ALL STEPS AND I WILL RATE! THANK YOU Returns Year X Y 1 12 %     24 %     2 30         45         3 19         -10         4 -20         -24         5 21         53             Requirement 1: (a) The arithmetic average return of company...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is -3 10...
Coin Change Problem) Given a list of positive integers c[0...n − 1], and a positive integer...
Coin Change Problem) Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT