Class Photo Album - C# Programming Exercise

Write a C# class called "PhotoAlbum" with a private attribute "numberOfPages". The class should also have a public method named "GetNumberOfPages", which will return the number of pages. The default constructor will create an album with 16 pages. There will be an additional constructor where we can specify the number of pages we want in the album. Then, create a class called "BigPhotoAlbum" whose constructor will create an album with 64 pages. Finally, create a test class called "AlbumTest" that will create an album using the default constructor, one with 24 pages, a "BigPhotoAlbum", and display the number of pages each of the three albums has.

 Category

OOP Object Oriented Programming

 Exercise

Class Photo Album

 Objective

Write a C# class "PhotoAlbum" with a private attribute "numberOfPages."

It should also have a public method "GetNumberOfPages", which will return the number of pages.

The default constructor will create an album with 16 pages. There will be an additional constructor, with which we can specify the number of pages we want in the album.

Create a class "BigPhotoAlbum" whose constructor will create an album with 64 pages.

Create a test class "AlbumTest" to create an album with its default constructor, one with 24 pages, a "BigPhotoAlbum" and show the number of pages that the three albums have.

 Write Your C# Exercise

// Import the System namespace for basic functionality
using System;

// Define the PhotoAlbum class
public class PhotoAlbum
{
    // Declare a private integer field for the number of pages
    private int numberOfPages;

    // Define the default constructor to initialize with 16 pages
    public PhotoAlbum()
    {
        // Set numberOfPages to 16 by default
        numberOfPages = 16;
    }

    // Define an additional constructor to specify the number of pages
    public PhotoAlbum(int pages)
    {
        // Set numberOfPages to the specified value
        numberOfPages = pages;
    }

    // Define a public method to get the number of pages
    public int GetNumberOfPages()
    {
        // Return the current number of pages
        return numberOfPages;
    }
}

// Define the BigPhotoAlbum class, inheriting from PhotoAlbum
public class BigPhotoAlbum : PhotoAlbum
{
    // Define the constructor for BigPhotoAlbum with 64 pages
    public BigPhotoAlbum()
        : base(64) // Initialize the base class with 64 pages
    {
    }
}

// Define the test class to execute the program
public class AlbumTest
{
    // Main method to run the program
    public static void Main()
    {
        // Create a PhotoAlbum object with the default constructor
        PhotoAlbum defaultAlbum = new PhotoAlbum();
        // Display the number of pages for the default album
        Console.WriteLine("Default album pages: " + defaultAlbum.GetNumberOfPages());

        // Create a PhotoAlbum object with 24 pages
        PhotoAlbum customAlbum = new PhotoAlbum(24);
        // Display the number of pages for the custom album
        Console.WriteLine("Custom album pages: " + customAlbum.GetNumberOfPages());

        // Create a BigPhotoAlbum object
        BigPhotoAlbum bigAlbum = new BigPhotoAlbum();
        // Display the number of pages for the big album
        Console.WriteLine("Big album pages: " + bigAlbum.GetNumberOfPages());
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP Object Oriented Programming

Explore our set of C# programming 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#.

  •  Class Shapes

    This exercise involves creating a project in C# that implements several classes based on a class diagram. The goal is to organize the code by splitting the ...

  •  Class Vehicles

    This exercise involves creating a project in C# and defining the corresponding classes according to a class diagram. Each class must include the attr...

  •  Class Square

    This exercise consists of completing the project called "Shapes" by adding a class named Square. In this class, we will store the starting X and Y coordinat...

  •  Class Orders

    In this exercise, you must create a project and the corresponding classes according to the class diagram. Each class must include the attributes and ...

  •  Class Colored Circle

    In this expanded exercise, you must modify the shapes and square project to also include a new class that stores data about colored circles. You need to...

  •  Classes Student + Teacher

    In this exercise in C#, you will need to write a program that includes the Person class you just created. From this class, you will create...