In: Computer Science
Download Jupyter and complete your Assignment. Be sure to write and run the code separately for each part. Please provide explanation for the code logic behind the code you write / design.
First part: Create Simple Student Class with constructor to get first name and last name.
Second part: Then create Address class with the constructor to get Student Address, the parameters can be , House/Apt Number, Street, Zip, State.
Third part: Modify Student Class so that it will get House/Apt Number, Street, Zip, State. and save address inside the Student as dictionary. Also implement the function in student class that print all addresses in the Student Class.
First Part:
class student: # declare class named student
def __init__(self,first_name,last_name): #define constructor with
two para meters first name & last name
self.firstname=first_name
self.lastname=last_name
m=student("John","Wick") #creating object named m of student class
and passes first name & last name
print(m.firstname,m.lastname) #print firstname and lastname which
are attributes of object m
Output:
Second part:
class Address: #declaring class Address
def __init__(self,House_no,street_no,zip_no,state,country): #define
constructor
self.houseno=House_no #constructor contains five parameters
self.street=street_no #parameters
House_no,street_no,zip_no,state,country
self.zip=zip_no
self.state=state
self.country=country
n=Address("Block no.-14","Goveernment
residence","360004","Gujarat","India") #creating object named n of
class Address
#and passing parameter
print(n.houseno,",",n.street,",",n.zip,",",n.state,",",n.country)#print
attributes of object n
Output:
Third Part:
class student: # declare class named student def __init__(self,first_name,last_name): #define constructor with two para meters first name & #last name self.firstname=first_name self.lastname=last_name def getAddress(self,House_no,street_no,zip_no,state,country): #set the values in dictionary self.address={"Houseno":House_no,"Street":street_no,"zip":zip_no,"State":state,"Country":country} def show(self): #define function to print address for keys in self.address: #print values of dictionary through its keys print(self.address[keys],end=",") #value seprated by comma print() #go on next line n=student("John","Wick") #creating object from calling constructor passes first name &last name n.getAddress("Block no.-14","Goveernment residence","360004","Gujarat","India") #calling function #getAddress() and passing parameter n.show() #print address of object
Output: