Class Picture Collection - Python Programming Exercise

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 practicing class definition, inheritance, and method implementation in Python. By implementing these classes, you will gain hands-on experience in handling class definitions, inheritance, and method implementation 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 Picture Collection

 Objective

Develop a Python a class named "PhotoAlbum" with a private attribute "pageCount."

Include a public method "getPageCount" that will return the total number of pages in the album.

The default constructor should create an album with 16 pages, but there should also be an alternative constructor to specify a custom page count when creating the album.

Also, create a subclass called "LargePhotoAlbum" where the constructor will set the album's pages to 64 by default.

Write a separate test class "AlbumTest" to create:

An album with the default constructor.
An album with 24 pages.
A "LargePhotoAlbum". Then, display the total number of pages in all three albums.

 Example Python Exercise

 Copy Python Code
# Base class PhotoAlbum
class PhotoAlbum:
    def __init__(self, page_count=16):
        # Initialize the album with a default or specified page count
        self.__pageCount = page_count  # Private attribute for page count

    def getPageCount(self):
        # Return the total number of pages in the album
        return self.__pageCount

# Subclass LargePhotoAlbum that inherits from PhotoAlbum
class LargePhotoAlbum(PhotoAlbum):
    def __init__(self):
        # Initialize the large photo album with a default of 64 pages
        super().__init__(64)  # Calls the constructor of PhotoAlbum with 64 pages

# Test class to demonstrate the functionality
class AlbumTest:
    @staticmethod
    def Main():
        # Create a PhotoAlbum with the default constructor (16 pages)
        album1 = PhotoAlbum()
        print(f"Album 1 has {album1.getPageCount()} pages.")

        # Create a PhotoAlbum with 24 pages
        album2 = PhotoAlbum(24)
        print(f"Album 2 has {album2.getPageCount()} pages.")

        # Create a LargePhotoAlbum (64 pages)
        large_album = LargePhotoAlbum()
        print(f"Large PhotoAlbum has {large_album.getPageCount()} pages.")

# Run the test class
AlbumTest.Main()

 Output

python photo_album.py
Album 1 has 16 pages.
Album 2 has 24 pages.
Large PhotoAlbum has 64 pages.

 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 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...

  •  Class Purchases

    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...

  •  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...