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