Introduction
For this lab, you should implement a calculator with a few extra functions (and minus a few others). You should have a small user interface (UI) on the command line and be able to calculate multiply, divide, exponent and the remainder of a division. NOTE: To make this a little more fun, you should implement this without using * or /. This means that you can only use + or -. This lab should assume that every number in the universe that will be entered into it is positive or 0.
The Math Library
Please write a short program that implements the following functions:
- public static int multiply(int a, int b)
- public static int divide(int a, int b)
- public static int exponent(int a, int b)
- public static int remainder(int a, int b)
What they should do:
- multiply() should take two ints and multiply them together. You should implement this using + only. It should return an int.
- divide() should take two ints, a and b, and divide a by b. You should implement this using - only. It should return an int.
- exponent() should take two ints and raise a to the bth power. So exponent(2,4) should calculate 2^4, and return the value 16. You can use other methods that you made. It should return an int.
- remainder() should return the remainder of a when divided by b. So remainder(8,3) should calculate 8 divided by 3, and then return only the remainder; which in this case is 2. You can use other methods that you made. It should return an int.
The User Interface
Your program should also implement a small user interface, which will ask the user what they want to do - multiply, divide, calculate an exponent, calculate a remainder, or quit. We recommend the use of a loop. Who knows, someone might decide to use our calculator to calculate exponents all night!
Don't forget to include your name, section, the date, this lab number, and your favorite flavor of cheese in a commented out section at the top. /* like this, remember? */ If you wished to comment your code as well, to tell someone who looks at your code what it does, feel free to do so. We always like that.
Hints
- 2+2+2+2 = 2*4 = 8
- if 0 is multiplied by any other number, we still get 0
- 5/2 = 2 (in this lab, anyway)
- 8-2-2-2-2 = 0 (note that there are 4 2's)