Implementing a Photo Album Class with Constructors in C#

In this exercise, you will implement a C# class named "PhotoAlbum" that manages the number of pages in a photo album. The class will include a private attribute "numberOfPages" and a public method "GetNumberOfPages" to return the number of pages. The class will feature a default constructor that sets the album to 16 pages and an additional constructor to specify a different number of pages. Additionally, you will create a "BigPhotoAlbum" class that initializes an album with 64 pages. Finally, you will implement a test class "AlbumTest" to create instances of these albums and display their page counts.



Group

Object-Oriented Programming in C#

Objective

1. Define a class "PhotoAlbum" with a private attribute "numberOfPages".
2. Implement a default constructor that initializes the album with 16 pages.
3. Implement an additional constructor that allows setting a custom number of pages.
4. Create a "BigPhotoAlbum" class that always initializes with 64 pages.
5. Implement a test class "AlbumTest" that creates three albums and displays their number of pages.

Write a C# class "PhotoAlbum" with a private attribute "numberOfPages" and implement constructors and methods as described.

Example Output:

Album with default pages: 16

Album with custom pages: 24
Big album pages: 64

Example C# Exercise

 Copy C# Code
using System;

// Class representing a photo album
class PhotoAlbum
{
    private int numberOfPages; // Private attribute to store the number of pages

    // Default constructor that sets the album to 16 pages
    public PhotoAlbum()
    {
        numberOfPages = 16;
    }

    // Constructor allowing a custom number of pages
    public PhotoAlbum(int pages)
    {
        numberOfPages = pages;
    }

    // Method to return the number of pages in the album
    public int GetNumberOfPages()
    {
        return numberOfPages;
    }
}

// Class representing a big photo album, always initialized with 64 pages
class BigPhotoAlbum : PhotoAlbum
{
    // Constructor that calls the base constructor with 64 pages
    public BigPhotoAlbum() : base(64)
    {
    }
}

// Test class to demonstrate the functionality
class AlbumTest
{
    static void Main()
    {
        // Creating an album using the default constructor (16 pages)
        PhotoAlbum album1 = new PhotoAlbum();
        Console.WriteLine("Album with default pages: " + album1.GetNumberOfPages());

        // Creating an album with a custom number of pages (24 pages)
        PhotoAlbum album2 = new PhotoAlbum(24);
        Console.WriteLine("Album with custom pages: " + album2.GetNumberOfPages());

        // Creating a big photo album (64 pages)
        BigPhotoAlbum album3 = new BigPhotoAlbum();
        Console.WriteLine("Big album pages: " + album3.GetNumberOfPages());
    }
}

 Output

Album with default pages: 16
Album with custom pages: 24
Big album pages: 64

Share this C# Exercise

More C# Practice Exercises of Object-Oriented Programming in C#

Explore our set of C# Practice Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. 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 C#.