"Suppose there is a class Alarm. Alarm has two class variables, code which contains a String value representing the code that deactivates the alarm, and armed which contains a boolean describing whether or not the alarm is activated. Alarm has a function disarm that changes the value of armed to False if the user gives a parameter containing the string that represents the alarm's code. Call the disarm function on the Alarm object myAlarm and give it the code "93478"."

Respuesta :

Limosa

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

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.