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: ...
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 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...
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...
# Declaring Node class class Node: def __init__(self, node_address): self.address = node_address self.next = None # Creating empty list forwarding_list = Node() # Recurssive Method for add address def addAddress(node): if node.address == old_address: # Address is same if node.next == None: # Next is None node.next = Node(new_address) # Add Next print("Added") # Addedd else: # Next is not none print("Entry already exists") elif node.next != None: # Check Next Node addAddress(node.next, old_address, new_address) def addEntry(forwarding_list):# Method for add...
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...
9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...