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
C# Exercise Example
// 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());
}
}