Address Class Application Using Python Implement the class Address. An address o
ID: 3720527 • Letter: A
Question
Address Class Application
Using Python Implement the class Address. An address object 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 the method def comesBefore(self, other) that tests whether an address comes before another when compared by postal code. Finally, supply the def getCountry(self) method that indicates the country in which the address is located.
Note: To complete this assignment, you will need to use the addressdemo.py file located in Project 11 folder on Blackboard. You will not be allowed to make any changes to addressdemo.py. All addresses are assumed to be in the USA by default.
-------------------------------------------------------------------------------------------
SAMPLE RUN
1st address:
2500 University Drive
Turlock, CA 95382
2nd address:
Apt 12 - 1200 College Blvd
Merced, CA 95340
2nd address comes before 1st address
1st address is in the USA
2nd address is in the USA
Explanation / Answer
address.py
class Address:
def __init__(self,housenumber,street,city,state,postalcode,apartmentno=None):
self.housenumber=housenumber
self.street=street
self.city=city
self.state=state
self.postalcode=postalcode
self.apartmentno=apartmentno
def comesBefore(self,other):
if self.postalcode<other.postalcode:
print("1st address comes before 2nd address")
else:
print("2nd address comes before 1st address")
def print(self):
if self.apartmentno is not None:
print(self.apartmentno,"-",self.housenumber,self.street)
print(self.city+",",self.state,self.postalcode)
else:
print(self.housenumber,self.street)
print(self.city+",",self.state,self.postalcode)
def getCountry(self):
return "USA"
addressdemo.py
from address import Address
# Construct two objects.
a = Address(2500, "University Drive", "Turlock", "CA", "95382")
b = Address(1200, "College Blvd", "Merced", "CA", "95340", "Apt 12")
# Demonstrate the print method.
print("1st address:")
a.print()
print()
print("2nd address:")
b.print()
print()
# Demonstrate the comesBefore method.
a.comesBefore(b)
# Demonstrate that all addresses are in the USA.
print("1st address is in the", a.getCountry())
print("2nd address is in the", b.getCountry())
Output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.