My Programming Lab Chapter 10

25 July 2022
4.7 (114 reviews)
19 test answers

Unlock all answers in this set

Unlock answers (15)
question
Write the definition of a class WeatherForecast that provides the following behavior (methods): A method called set_skies that has one parameter, a String. A method called set_high that has one parameter, an int. A method called set_low that has one parameter, an int. A method called get_skies that has no parameters and that returns the value that was last used as an argument in set_skies. A method called get_high that has no parameters and that returns the value that was last used as an argument in set_high. A method called get_low that has no parameters and that returns the value that was last used as an argument in set_low. No constructor need be defined. Be sure to define instance variables as needed by your "get"/"set" methods.
answer
class WeatherForecast(): skies = "" high = 0 low = 0 def set_skies(self, skies): self.skies = skies def get_skies(self): return self.skies def set_high(self, high): self.high = high def get_high(self): return self.high def set_low(self, low): self.low = low def get_low(self): return self.low
question
Write the definition of a class Counter containing: An instance variable counter of type int, initialized to 0. A method called increment that adds one to the instance variable counter. It does not accept parameters or return a value. A method called get_value that doesn't accept any parameters. It returns the value of the instance variable counter.
answer
class Counter(object): def __init__(self): self.counter = 0 def increment(self): self.counter += 1 def get_value(self): return self.counter
question
Write the definition of a class Counter containing: An instance variable named counter of type int. A constructor that takes one int argument and assigns its value to counter A method named increment that adds one to counter. It does not take parameters or return a value. A method named decrement that subtracts one from counter. It also does not take parameters or return a value. A method named get_value that returns the value of the instance variable counter.
answer
class Counter: def __init__(self): self.counter=0 def increment(self): self.counter+=1 def decrement(self): self.counter-=1 def get_value(self): return self.counter
question
Write the definition of a class Counter containing: An instance variable named counter of type int An instance variable named limit of type int. A constructor that takes two int arguments and assigns the first one to counter and the second one to limit A method named increment. It does not take parameters or return a value; if the instance variable counter is less than limit, increment just adds one to the instance variable counter. A method named decrement. It also does not take parameters or return a value; if counter is greater than zero, it just subtracts one from the counter. A method named get_value that returns the value of the instance variable counter.
answer
class Counter: def __init__(self, c, l): self.counter = c self.limit = l def increment(self): if(self.counter0): self.counter-=1 def get_value(self): return self.counter; t = Counter(12,20) t.increment() v = t.get_value() print("Counter Value: ",v) t.decrement() v = t.get_value() print("Counter Value: ",v)
question
Write the definition of a class Player containing: An instance variable name of type String, initialized to the empty String. An instance variable score of type int, initialized to zero. A method called set_name that has one parameter, whose value it assigns to the instance variable name. A method called set_score that has one parameter, whose value it assigns to the instance variable score. A method called get_name that has no parameters and that returns the value of the instance variable name. A method called get_score that has no parameters and that returns the value of the instance variable score. No constructor need be defined.
answer
class Player: def __init__(self): self.__name = "" self.__score = 0 def set_name(self, name): self.__name = name def set_score(self, score): self.__score = score def get_name(self): return self.__name def get_score(self): return self.__score
question
Write the definition of a class ContestResult containing: An instance variable winner of type String, initialized to the empty String. An instance variable second_place of type String, initialized to the empty String. An instance variable third_place of type String, initialized to the empty String. A method called set_winner that has one parameter, whose value it assigns to the instance variable winner. A method called set_second_place that has one parameter, whose value it assigns to the instance variable second_place. A method called set_third_place that has one parameter, whose value it assigns to the instance variable third_place. A method called get_winner that has no parameters and that returns the value of the instance variable winner. A method called get_second_place that has no parameters and that returns the value of the instance variable second_place. A method called get_third_place that has no parameters and that returns the value of the instance variable third_place. No constructor need be defined.
answer
class ContestResult: def __init__(self): self.winner = "" self.second_place = "" self.third_place = "" def set_winner(self, winner): self.winner = winner def set_second_place(self, second_place): self.second_place = second_place def set_third_place(self, third_place): self.third_place = third_place def get_winner(self): return self.winner; def get_second_place(self): return self.second_place def get_third_place(self): return self.third_place
question
Suppose there is a class AirConditioner. The class supports the following behaviors: turning the air conditioner on and off. The following methods are provided for these behaviors: turn_on and turn_off. Both methods accept no arguments and return no value. There is a reference variable my_ac to an object of this class, which has already been created. Invoke a method to turn the air conditioner on.
answer
my_ac.turn_on()
question
Suppose there is a class AirConditioner. The class supports the following behaviors: turning the air conditioner on and off. The following methods are provided for these behaviors: turn_on and turn_off. Both methods accept no arguments and return no value. There is a reference variable my_ac to an object of this class, which has already been created. Invoke a method to turn the air conditioner off.
answer
my_ac.turn_off()
question
Suppose there is a class AirConditioner. The class supports the following behaviors: turning the air conditioner on, off, and setting the desired temperature. The following methods are provided for these behaviors: turn_on and turn_off, which accept no arguments and return no value, and set_temp, which accepts an int argument and returns no value. There is a reference variable my_ac to an object of this class, which has already been created. Invoke a method telling the object to set the air conditioner to 72 degrees.
answer
my_ac.set_temp(72)
question
Suppose there is a class AirConditioner. The class supports the following behaviors: turning the air conditioner on, off, setting the desired temperature, and telling the previously set temperature. The following methods are provided for these behaviors: turn_on and turn_off, which accept no arguments and return no value, set_temp, which accepts an int argument and returns no value, and get_temp, which accepts no value and returns an int. There is a reference variable my_ac to an object of this class, which has already been created. There is also a variable called current_temp, which has already been initialized. Invoke a method using the reference variable which returns the previously set temperature of the air conditioner, and store the returned value in current_temp.
answer
current_temp = my_ac.get_temp()
question
Suppose there is a class AirConditioner. The class supports the following behaviors: turning the air conditioner on, off, and checking if the air conditioner is on or off. The following methods are provided for these behaviors: turn_on and turn_off, which accept no arguments and return no value, and is_on, which accepts no argument and returns a bool indicating whether the air conditioner is on or off. There is a reference variable my_ac to an object of this class, which has already been created. There is also a variable status, which has already been initialized which refers to a bool. Invoke a method of this object using the reference variable, asking the object to tell whether the air conditioner is on or off, and store the result in status.
answer
status = my_ac.is_on()
question
Suppose there is a class AirConditioner. The class supports the following behaviors: turning the air conditioner on and off. The following methods are provided for these behaviors: turn_on and turn_off. Both methods accept no arguments and return no value. There is a reference variable office_a_c of type AirConditioner. Create a new object of type AirConditioner using the office_a_c reference variable. After that, turn the air conditioner on using the reference to the new object.
answer
office_a_c = AirConditioner() office_a_c.turn_on()
question
Suppose there is a class AirConditioner. The class supports the following behaviors: turning the air conditioner on, off, and setting the desired temperature. The following methods are provided for these behaviors: turn_on and turn_off, which accept no arguments and return no value, and set_temp, which accepts an int argument and returns no value. There is a reference variable office_a_c of type AirConditioner. Create a new object of type AirConditioner using the office_a_c reference variable. After that, turn the air conditioner on using the reference to the new object, and set the desired temperature to 69 degrees.
answer
office_a_c = AirConditioner() office_a_c.turn_on() office_a_c.set_temp(69)
question
Suppose there is a class Roster. Roster has one variable, roster, which is a List of tuples containing the names of students and their number grades--for example, [('Richard', 90), ('Bella', 67), ('Christine', 85), ('Francine', 97)]. Roster has a function calculateAverage that calculates the average of the class. Write a statement that returns the average of the Roster object englishClass, which has already been defined for you.
answer
def findValedictorian(englishClass): res = ( " ", 0 ) for tup in englishClass: if res[1] < tup[1] : res = tup return res
question
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".
answer
class Alarm: def __init__(self,code,armed = False): self.code = code self.armed = armed def changeCode(self,currentCode,newCode): if currentCode == self.code: self.code = newCodes
question
There is a class called Roster whose constructor takes a List of tuples with the names of students and their grades in the class, for example -- [('Richard', 98), ('Benny', 55), ('Katie', 87), ('Sally', 76)]. Create an instance of the Roster class, including the following students and their respective grades, and store it in the variable mathClass: Jacob, 65 Frankie, 86 Lina, 94 Arnold, 63 Amanda, 87
answer
mathClass = Roster([("Jacob", 65), ("Frankie", 86), ("Lina", 94), ("Arnold", 63), ("Amanda", 87)])
question
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. Create an Alarm object called myAlarm with the code "12345".
answer
myAlarm = Alarm myAlarm.code = "12345"
question
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. Create an Alarm object called myAlarm with the code "3579" and print the value of armed to standard output.
answer
myAlarm=Alarm("3579") print(myAlarm.armed)
question
Write the class Calculator including: a function called addthat takes two parameters containing double values and returns their sum a function called subtractthat takes two parameters containing double values and returns their difference (subtract the second from the first) a function called multiply that takes two parameters containing double values and returns their product a function called divide that takes two parameters containing double values and returns the value of the first divided by the second. If the second number is a zero, do not divide, and return "You can't divide by zero!"
answer
class Calculator: def add(self,x,y): return x+ y def subtract(self,x,y): return x - y def multiply(self,x,y): return x * y def divide(self,x,y): if y==0: return "You can't divide by zero!" else: return x/y