Code Example 14-2
class Die:
def __init__(self):
self.__value = 1
def getValue(self):
return self.__value
def roll(self):
self.__value = random.randrange(1, 7)
Refer to Code Example 14-2: Given a Die object named die, which of
the following will print the value of the __value attribute to the
console?
a.
print(die.getValue())
b.
print(die.roll())
c.
print(die.value)
d.
print(die.__value)
To prevent another programmer from directly accessing the
attributes of an object, you use
a.
instantiation
b.
composition
c.
annotation
d.
encapsulation
Code Example 14-2 class Die: def __init__(self): self.__value = 1 def getValue(self): return self.__value def...
Code Example 14-3 class Multiplier: def __init__(self): self.num1 = 0 self.num2 = 0 def getProduct(self): return self.num1 * self.num2 def main(): m = Multiplier() m.num1 = 7 m.num2 = 3 print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": main() Refer to Code Example 14-3: When this code is executed, what does it print to the console? a. 7 X 3 = 0 b. 3 X 7 = 0 c. 7 X 3...
Take the class Person. class Person: """Person class""" def __init__(self, lname, fname, addy=''): self._last_name = lname self._first_name = fname self._address = addy def display(self): return self._last_name + ", " + self._first_name + ":" + self._address Implement derived class Student In the constructor Add attribute major, default value 'Computer Science' Add attribute gpa, default value '0.0' Add attribute student_id, not optional Consider all private Override method display() Test your code with the following driver: # Driver my_student = Student(900111111, 'Song', 'River')...
Can you complete the following
code, please? Thank you.
class BinaryNode:
def __init__(self, value):
self.__value = value
self.__left = None
self.__right = None
self.__parent = None
self.__height = 1
def getValue(self):
return self.__value
def setHeight(self, height):
self.__height = height
def getHeight(self):
return self.__height
def setParent(self, node):
self.__parent = node
def getParent(self):
return self.__parent
def setLeftChild(self, child):
self.__left = child
child.setParent(self)
def setRightChild(self, child):
self.__right = child
child.setParent(self)
def createLeftChild(self, value):
self.__left = BinaryNode(value)
def createRightChild(self, value):
self.__right = BinaryNode(value)
def...
***IN PYTHON 2.7****Augment the following code with a new class named 'Coin'. Coin should inherit from Die, with the following modifications; The constructor should not take any arguments; a coin always has two sides add a flip() method that uses the roll() method from the parent class. If roll returns 1; flip should return "HEADS". If roll returns a 2, flip should return "TAILS" Do not override the roll or rollMultiple methods from the parent class #!/usr/bin/python # your class...
class BinaryTree:
def __init__(self, data, left=None, right=None):
self.__data = data
self.__left = left
self.__right = right
def insert_left(self, new_data):
if self.__left == None:
self.__left = BinaryTree(new_data)
else:
t = BinaryTree(new_data, left=self.__left)
self.__left = t
def insert_right(self, new_data):
if self.__right == None:
self.__right = BinaryTree(new_data)
else:
t = BinaryTree(new_data, right=self.__right)
self.__right = t
def get_left(self):
return self.__left
def get_right(self):
return self.__right
def set_data(self, data):
self.__data = data
def get_data(self):
return self.__data
def set_left(self, left):
self.__left = left
def set_right(self, right):
self.__right...
In python class Customer: def __init__(self, customer_id, last_name, first_name, phone_number, address): self._customer_id = int(customer_id) self._last_name = str(last_name) self._first_name = str(first_name) self._phone_number = str(phone_number) self._address = str(address) def display (self): return str(self._customer_id) + ", " + self._first_name + self._last_name + "\n" + self._phone_number + "\n" + self._address customer_one = Customer("694", "Abby", "Boat", "515-555-4289", "123 Bobby Rd", ) print(customer_one.display()) customer_two = Customer ("456AB", "Scott", "James", "515-875-3099", "23 Sesame Street Brooklyn, NY 11213") print(customer_two.display()) Use Customer class remove the address attribute.Place the code...
class Livestock: def __init__(self,name,price_in,utilizations): self.name,self.price_in,self.utilizations = name,float(price_in),utilizations def __lt__(self,other): if self.utilizations is None: return True elif other.utilizations is None: return False else: return self.utilizations.count(';') < other.utilizations.count(';') def __eq__(self,other): return self.name == other.name def __repr__(self): return ("{}, {}".format(self.name,self.price_in)) raw_livestock_data = [ # name, price_in, utilizations ['Dog', '200.0', 'Draught,Hunting,Herding,Searching,Guarding.'], ['Goat', '1000.0', 'Dairy,Meat,Wool,Leather.'], ['Python', '10000.3', ''], ['Cattle', '2000.75', 'Meat,Dairy,Leather,Draught.'], ['Donkey', '3400.01', 'Draught,Meat,Dairy.'], ['Pig', '900.5', 'Meat,Leather.'], ['Llama', '5000.66', 'Draught,Meat,Wool.'], ['Deer', '920.32', 'Meat,Leather.'], ['Sheep', '1300.12', 'Wool,Dairy,Leather,Meat.'], ['Rabbit', '100.0', 'Meat,Fur.'], ['Camel', '1800.9', 'Meat,Dairy,Mount.'], ['Reindeer', '4000.55', 'Meat,Leather,Dairy,Draught.'],...
Previous code:
class BinarySearchTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def search(self, find_data):
if self.data == find_data:
return self
elif find_data < self.data and self.left != None:
return self.left.search(find_data)
elif find_data > self.data and self.right != None:
return self.right.search(find_data)
else:
return None
def get_left(self):
return self.left
def get_right(self):
return self.right
def set_left(self, tree):
self.left = tree
def set_right(self, tree):
self.right = tree
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def traverse(root,order):...
URGENT!
Consider the following code for the definition of a Home object: 1 This class also contains getters, setters and a _str_method for it's owner and address attributes. class Home: def __init__(self, owner, address) self_owner owner, sell_address address Create a (derived) Apartment class that inherits from the Home class, but also has an attribute called rental price. Implement the Apartment class to inherit from the base Home class and contains all appropriate methods that need to be written for this...
Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def __init__(self, make, model, year) Methods in BMW parent class (can leave as method stubs for now): startEngine() -must print "The engine is now on." stopEngine() -must print "The engine is now off." Make another class called ThreeSeries -Must inherit BMW Hint: class ThreeSeries(BMW): -Also passes the three attributes of the parent class and invoke parent Hint: BMW.__init__(self, cruiseControlEnabled, make, model, year) -Constructor must have...