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:
Classes And Object Oriented Programming
Exercise:
Programming Exercises
Question:3 | ISBN:9780132576376 | Edition: 2

Question

Personal Information Class
Design a class that holds the following personal data: name, address, age, and phone number. Write appropriate accessor and mutator methods. Also, write a program that creates three instances of the class. One instance should hold your information, and the other two
should hold your friends’ or family members’ information.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Personal Information Class Program Code:

#Create a class name, PersonalData that holds 
#the following personal data: name, address, age, and phone number. 
class PersonalData:
    def __init__(self, name, address, age, phone_number):
        self.__name = name
        self.__address = address
        self.__age = age
        self.__phone_number = phone_number
    #accessor and mutator methods.
    def get_name(self):
        return self.__name
    
    def set_name(self, name):
        self.__name = name
    
    def get_address(self):
        return self.__address
    
    def set_address(self, address):
        self.__address = address
    
    def get_age(self):
        return self.__age
    
    def set_age(self, age):
        self.__age = age
    
    def get_phone_number(self):
        return self.__phone_number
    
    def set_phone_number(self, phone_number):
        self.__phone_number = phone_number

# Creates three instances of the class.
my_info = PersonalData("Charless", "Nyzira", 25, "1234567890")
friend1_info = PersonalData("Josh", "USA", 30, "9876543210")
friend2_info = PersonalData("Kevin", "Canda", 28, "4567891230")

# Access and modify the personal data
print("My Information:")
print("Name:", my_info.get_name())
print("Address:", my_info.get_address())
print("Age:", my_info.get_age())
print("Phone Number:", my_info.get_phone_number())

print("\nFriend 1 Information:")
print("Name:", friend1_info.get_name())
print("Address:", friend1_info.get_address())
print("Age:", friend1_info.get_age())
print("Phone Number:", friend1_info.get_phone_number())

print("\nFriend 2 Information:")
print("Name:", friend2_info.get_name())
print("Address:", friend2_info.get_address())
print("Age:", friend2_info.get_age())
print("Phone Number:", friend2_info.get_phone_number())

# Modify personal data using mutator methods
my_info.set_address("Tokyo")
friend1_info.set_age(32)
friend2_info.set_phone_number("9999999999")

# Display modified personal data
print("\nAfter Modification:")
print("My Information:")
print("Name:", my_info.get_name())
print("Address:", my_info.get_address())
print("Age:", my_info.get_age())
print("Phone Number:", my_info.get_phone_number())

print("\nFriend 1 Information:")
print("Name:", friend1_info.get_name())
print("Address:", friend1_info.get_address())
print("Age:", friend1_info.get_age())
print("Phone Number:", friend1_info.get_phone_number())

print("\nFriend 2 Information:")
print("Name:", friend2_info.get_name())
print("Address:", friend2_info.get_address())
print("Age:", friend2_info.get_age())
print("Phone Number:", friend2_info.get_phone_number())

Executed Output:

My Information:
Name: Charless
Address: Nyzira
Age: 25
Phone Number: 1234567890

Friend 1 Information:
Name: Josh
Address: USA
Age: 30
Phone Number: 9876543210

Friend 2 Information:
Name: Kevin
Address: Canda
Age: 28
Phone Number: 4567891230

After Modification:
My Information:
Name: Charless
Address: Tokyo
Age: 25
Phone Number: 1234567890

Friend 1 Information:
Name: Josh
Address: USA
Age: 32
Phone Number: 9876543210

Friend 2 Information:
Name: Kevin
Address: Canda
Age: 28
Phone Number: 9999999999

 

0 0

Discussions

Post the discussion to improve the above solution.