Answer:
Following are the program in the Python programming Language.
#define class
class Alarm:
#define constructor
def __init__(self,code,armed = False):
self.code = code #initialize value
self.armed = armed #initialize value
#define function
def changeCode(self,currentCode,newCode):
#set if conditional statement
if currentCode == self.code:
#initialize value if the condition is true
self.code = newCode
#return value
return newCode
#set object of the class and pass value in constructor
myAlarm=Alarm("93478")
#call and print the function
myAlarm.changeCode("93478","1234")
print(myAlarm.code)
Output:
1234
Step-by-step explanation:
Here, we define a class "Alarm" and inside the class.
- Define a constructor and pass arguments in its parameter "code" ,"armed" and assign a boolean value in the variable "armed" to False.
- Then, initialize the variables inside it.
- Define the function name "changeCode()" and pass arguments in its parentheses "currentCode" and "newCode" inside it, we set if condition and check whether the "currentCode" is equal to the code the initialize into code the value of "newCode".
Finally, we set an object of the class and pass value in its parentheses for the constructor and then, we call the function and pass values in its parameter then, we print the code.