SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Inheritance
Exercise:
Programming Exercises
Question:3 | ISBN:9780132576376 | Edition: 2

Question

Person and Customer Classes
Write a class named Person with data attributes for a person’s name, address, and telephone
number. Next, write a class named Customer that is a subclass of the Person class.
The Customer class should have a data attribute for a customer number and a Boolean data
attribute indicating whether the customer wishes to be on a mailing list. Demonstrate an
instance of the Customer class in a simple program.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Person and Customer Classes Program Code:

# Person class definition
class Person:
    def __init__(self, name, address, telephone_number):
        # Initialize data attributes
        self.name = name
        self.address = address
        self.telephone_number = telephone_number
    
    # Accessor methods
    def get_name(self):
        return self.name
    
    def get_address(self):
        return self.address
    
    def get_telephone_number(self):
        return self.telephone_number
    
    # Mutator methods
    def set_name(self, name):
        self.name = name
    
    def set_address(self, address):
        self.address = address
    
    def set_telephone_number(self, telephone_number):
        self.telephone_number = telephone_number


# Customer class definition, subclass of Person
class Customer(Person):
    def __init__(self, name, address, telephone_number, customer_number, mailing_list):
        # Call superclass constructor to initialize inherited attributes
        super().__init__(name, address, telephone_number)
        # Initialize additional data attributes
        self.customer_number = customer_number
        self.mailing_list = mailing_list
    
    # Accessor methods
    def get_customer_number(self):
        return self.customer_number
    
    def get_mailing_list(self):
        return self.mailing_list
    
    # Mutator methods
    def set_customer_number(self, customer_number):
        self.customer_number = customer_number
    
    def set_mailing_list(self, mailing_list):
        self.mailing_list = mailing_list


# Create an instance of the Customer class and demonstrate its usage
def main():
    # Create a Customer object
    customer = Customer("Charless Edan", "123 Main Street", "1111-4234", "C12345", True)
    
    # Display the customer information using accessor methods
    print("Customer Name:", customer.get_name())
    print("Address:", customer.get_address())
    print("Telephone Number:", customer.get_telephone_number())
    print("Customer Number:", customer.get_customer_number())
    print("Mailing List:", customer.get_mailing_list())


# Call the main function to start the program
if __name__ == "__main__":
    main()

Executed Output:

Customer Name: Charless Edan
Address: 123 Main Street
Telephone Number: 1111-4234
Customer Number: C12345
Mailing List: True

 

0 0

Discussions

Post the discussion to improve the above solution.