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
Test your code with the following driver:
# Driver my_student = Student(900111111, 'Song', 'River') print(my_student.display()) my_student = Student(900111111, 'Song', 'River', 'Computer Engineering') print(my_student.display()) my_student = Student(900111111, 'Song', 'River', 'Computer Engineering', 4.0) print(my_student.display()) del my_student
With the following OUTPUT:
Song, River:(900111111) Computer Science gpa: 0.0
Song, River:(900111111) Computer Engineering gpa: 0.0
Song, River:(900111111) Computer Engineering gpa: 4.0
Notes/Hints
Python 3 code
============================================================================================
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
class Student(Person):
def __init__(self, stuid,fname, lname,major='Computer
Science',gpa='0.0'):
super().__init__(fname, lname)
self._studentid=stuid
self._major=major
self._gpa=gpa
def display(self):
return self._last_name + ", " + self._first_name + ":" +
self._address +"("+str(self._studentid)+")"+" "+self._major+" gpa:
"+str(self._gpa)
# Driver
my_student = Student(900111111, 'Song', 'River')
print(my_student.display())
my_student = Student(900111111, 'Song', 'River', 'Computer
Engineering')
print(my_student.display())
my_student = Student(900111111, 'Song', 'River', 'Computer
Engineering', 4.0)
print(my_student.display())
del my_student
============================================================================================
Output

Take the class Person. class Person: """Person class""" def __init__(self, lname, fname, addy=''): self._last_name = lname...
Consider following flower class. class Flower: def __init__(self, Fname, numberofpetals, Fprice): self.Fname = Fname self.numberofpetals = numberofpetals self.Fprice = Fprice def getName(self): return self.Fname def getPetalsCount(self): return self.numberofpetals def getPrice(self): return self.Fprice def setName(self, Fname): self.Fname = Fname def setPetalsCount(self, numberofpetals): self.numberofpetals = numberofpetals def setPrice(self, Fprice): self.Fprice = Fprice def main(): flower f, print("name = ", flower.getName()) flower.setName("jasmine") print("name = ", flower.getName()) main() Q. What is the statement to set an object flower with rose , 4 and price...
PYTHON 3.8 class student(): def __init__(self, name = 'Bill', grade = 9, subjects = ['math','science']): self.name = name self.grade = grade self.subjects = subjects def add_subjects(self): print('Current subjects:') print(self.subjects) more_subjects = True while more_subjects == True: subject_input = input('Enter a subject to add: ') if subject_input == '': more_subjects = False print(self.subjects) else: self.subjects.append(subject_input) print(self.subjects) m = student()...
class Leibniz: def __init__(self): self.total=0 def calculate_pi(self,n): try: self.total, sign = 0, 1 for i in range(n): term = 1 / (2 * i + 1) self.total += term * sign sign *= -1 self.total *= 4 return self.total except Exception as e: ...
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 ...
class FileUtility: def __init__ (self, filename): self.__filename = filename def displayFile(self): # use loop structure to read and print each line of the file def main(): #1. create a new file and write the follwing lines to it. # #I never saw a Purple Cow, #I never hope to see one, #But I can tell you, anyhow, #I’d rather see than be one! # #2. create an object of FileUtility class #3. call the object's displayFile method main()
In Python Exercise – Overriding class Address: def __init__(self, street, num): self.street_name = street self.number = num Now make a subclass of the class Address called CampusAddress that has a new attribute, office number, that can vary. This subclass will always have the street attribute set to Massachusetts Ave and the num attribute set to 77. Use the class as follows: >>> Sarina_addr = CampusAddress("32-G904") >>> Sarina_addr.office_number ’32G-904’ >>> Sarina_addr.street_name ’Massachusetts Ave’ >>> Sarina_addr.number 77
PYTHON
--------------------------------------------------------
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size ==
0:
return
None
else:
return
self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size ==
0:
return
None
else:
return
self.__tail.element
# Add an element to the beginning of the
list
def addFirst(self, e):
newNode = Node(e) #
Create a new node
newNode.next =
self.__head # link...
In Python: Which one is the arguments of the ShowInfo method? class Car(): def __init__(self,model, year): self._model = model self._year = year def ShowInfo( ): print(self._model + str(self._year)) Question 16 options: model, year No argument is needed self,model, year self
in
python and according to this
#Creating class for stack
class My_Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def Push(self, d):
self.items.append(d)
def Pop(self):
return self.items.pop()
def Display(self):
for i in reversed(self.items):
print(i,end="")
print()
s = My_Stack()
#taking input from user
str = input('Enter your string for palindrome checking:
')
n= len(str)
#Pushing half of the string into stack
for i in range(int(n/2)):
s.Push(str[i])
print("S",end="")
s.Display()
s.Display()
#for the next half checking the upcoming string...
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...