Question: 1 2 . 1 2 LAB: Calculator class In the file main.py write a class called Calculator that emulates basic functions of a calculator: addition,

12.12 LAB: Calculator class
In the file main.py write a class called Calculator that emulates basic functions of a calculator: addition, subtraction, multiplication, division, and clear. The class has one attribute called calc_(v)alue for the calculator's current value. Implement the following methods as listed below:
Default constructor method to set the attribute to 0.0
addition (self, val)- add the parameter to the attribute
subtraction (self, val)-subtract the parameter from the attribute
multiplication (self, val)- multiply the attribute by the parameter
division(self, val)- divide the attribute by the parameter
clear (self)- set the attribute to 0.0
get_(v)alue (self)- return the attribute
Given two float input values num1 and num2, the program outputs the following values:
1. The initial value of the instance attribute, calc_(v)alue
2. The value after adding num1
3. The value after multiplying by 3
4. The value after subtracting num2
5. The value after dividing by 2
6. The value after calling the clear() method
Ex: If the input is:
10.0
5.0
the output is:
0.0
10.0
30.0
25.0
12.5
0.0
class Calculator:
#type your code here
if name == "main":
calc = Calculator()
num1= float(input())
num2= float(input())
# 1. The initial value
print(f'{calc.get_value:.1f}')
# 2. The The value after adding num1
calc.addition(num1)
print(f'{calc.get_value:.1f}')
# 3. The value after multiplying by 3
calc.multiplication(3)
print(f'{calc.get_value:.1f}')
# 4. The value after subtracting num2
calc.subtraction(num2)
print(f'{calc.get_value:.1f}')
# 5. The value after dividing by 2
calc.division(2)
print(f'{calc.get_value:.1f}')
# 6. The value after calling the clear() method
calc.clear()
print(f'{calc.get_value:.1f}')
1 2 . 1 2 LAB: Calculator class In the file

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!