HI I need help with this homework in Python and there are just Java examples Imp
ID: 3693573 • Letter: H
Question
HI I need help with this homework in Python and there are just Java examples Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code. Define the constructor such that an object can be created in one of two ways: with an apartment number or without. Supply a print method that prints the address with the street on one line and the city, state, and postal code on the next line. Supply a method def comesBefore(self, other) that tests whether this address comes before other when compared by postal code.
Explanation / Answer
# python class Address to use constructor to print address and find if one addreess comes before or not
class Address:
#constructor
def __init__(self, house_number, street, apartment_number, city, state, postal_code):
self.house_number = house_number
self.street = street
self.apartment_number = apartment_number
self.city = city
self.state = state
self.postal_code = postal_code
#print method
def printt(self):
print " "
print self.street
print self.city, self.state, self.postal_code
# comesbefore method
def comesBefore(self,other):
if self.postal_code < other.postal_code:
return "Address1 comes Before Address2"
else:
return "Address2 comes Before Address1"
Address1 = Address(
"321",
"park street",
"11",
"Greenville",
"katiyar",
"462003"
)
Address2 = Address(
"444",
"silk street",
"2",
"Nashville",
"Baltimore",
"721302"
)
Address1.printt()
Address2.printt()
print " "
print Address1.comesBefore(Address2)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.