Class Purchases - Python Programming Exercise

In this exercise, you will develop a Python project with the necessary classes (distributed across multiple files) based on the given class diagram. Each class should contain the attributes and methods outlined in the diagram. This exercise is perfect for practicing class definition, inheritance, and project organization in Python. By implementing this project, you will gain hands-on experience in handling class definitions, inheritance, and project organization in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

OOP Object-Oriented Programming

 Exercise

Class Purchases

 Objective

Develop a Python project with the necessary classes (distributed across multiple files) based on the given class diagram.

Each class should contain the attributes and methods outlined in the diagram. Keep in mind that all relationships between classes are one-to-one.

 Example Python Exercise

 Copy Python Code
# Combined code for Address, Job, Person, and Main in one file

# Address class
class Address:
    def __init__(self, street, city, state, zip_code):
        self.street = street  # Street address
        self.city = city  # City name
        self.state = state  # State name
        self.zip_code = zip_code  # ZIP code

    def __str__(self):
        return f"{self.street}, {self.city}, {self.state} - {self.zip_code}"

# Job class
class Job:
    def __init__(self, job_title, company, salary):
        self.job_title = job_title  # Job title
        self.company = company  # Company name
        self.salary = salary  # Annual salary

    def __str__(self):
        return f"{self.job_title} at {self.company}, Salary: ${self.salary}"

# Person class
class Person:
    def __init__(self, name, age, address, job):
        self.name = name  # Name of the person
        self.age = age  # Age of the person
        self.address = address  # Address object
        self.job = job  # Job object

    def display_info(self):
        print(f"Name: {self.name}")
        print(f"Age: {self.age}")
        print(f"Address: {self.address}")
        print(f"Job: {self.job}")

# Main program to create and display information
if __name__ == "__main__":
    # Create an Address object
    address1 = Address("123 Main St", "Springfield", "IL", "62704")

    # Create a Job object
    job1 = Job("Software Developer", "TechCorp", 95000)

    # Create a Person object
    person1 = Person("John Doe", 30, address1, job1)

    # Display information about the person
    person1.display_info()

 Output

Name: John Doe
Age: 30
Address: 123 Main St, Springfield, IL - 62704
Job: Software Developer at TechCorp, Salary: $95000

 Share this Python Exercise

 More Python Programming Exercises of OOP Object-Oriented Programming

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Class Colored Sphere

    In this exercise, you will develop a Python project and extend the previous exercise to include "Colored Circles". Each circle should have a radius, center coordinate...

  •  Classes: Learner and Instructor

    In this exercise, you will develop a Python program that incorporates a Person class. Then, create two additional classes, Student and Teacher, that inherit from Pers...

  •  Class Picture Collection

    In this exercise, you will develop a Python program with a class named "PhotoAlbum" that includes a private attribute "pageCount." This exercise is perfect for...

  •  Class GeometricShapes

    In this exercise, you will develop a Python project with the required classes, organizing them into separate files, according to a class diagram. This exercise...

  •  Vehicle Classes

    In this exercise, you will develop a Python project with corresponding classes, spread across multiple files, based on the provided class diagram. Each class should h...

  •  Class Rectangle

    In this exercise, you will develop a Python project called "Shapes," incorporating a class named "Square." Each square will store the starting X and Y coordinates (re...