Exercise: The Calculator Object
Let's create an object which performs the basic functions of a calculator.
We'll cover the following
Problem Statement
In this exercise, you have to implement the calculator
type and create the cal
object from it. Your calculator will perform four basic tasks:
-
Addition
-
Subtraction
-
Multiplication
-
Division
Each function will take two floats as parameters. These floats will not be values of the object. Instead, they’ll be specified in the function calls.
Make sure that cal
is a closed object.
Sample Input
cal#add(10.0, 5.0);
cal#subtract(10.0, 5.0);
cal#multiply(10.0, 5.0);
cal#divide(10.0, 5.0);
Sample Output
15.0
5.0
50.0
2.0
Coding Challenge
Below, you must write code for the calculator
type as well as the calculator
object. Remember that the names of all the functions must be identical to the ones in the Sample Output. Furthermore, these functions should be public and must only handle floats.
Take some time to flesh out the logic behind this problem before jumping to the actual implementation. It isn’t very tricky if you understand the concepts we’ve covered in this section.
If you feel stuck, you can always refer to the solution review in the next lesson.
Good luck!
Get hands-on with 1400+ tech skills courses.