Class Vehicles - C# Programming Exercise

This exercise involves creating a project in C# and defining the corresponding classes according to a class diagram. Each class must include the attributes and methods shown in the diagram, as well as Get and Set methods for the Vehicle class and Has methods (such as HasDualSlidingDoors) for the MiniVan class. The objective is to correctly implement inheritance among the classes and their respective functionalities. Additionally, you must create a test program that creates an object of each class and tells it to "Drive", invoking its "Drive" method to demonstrate the behavior of the classes in action.

 Category

OOP Object Oriented Programming

 Exercise

Class Vehicles

 Objective

Create a project and the corresponding classes (using several files) for this classes diagram. Each class must include the attributes and methods shown in the diagram, as well as Get and Set methods for Vehicle and "Has" methods ("HasDualSlidingDoors") for MiniVan.

You must create also a test program, which will create an object belonging to each class and tell it to "Drive".

 Write Your C# Exercise

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

// Define the main class containing all vehicle-related classes
public class VehiclesDemo
{
    // Define the base class Vehicle
    public class Vehicle
    {
        // Declare a private field for the vehicle's speed
        private int speed;
        // Declare a private field for the vehicle's color
        private string color;

        // Define a public method to set the vehicle's speed
        public void SetSpeed(int speed)
        {
            // Assign the speed parameter to the speed field
            this.speed = speed;
        }

        // Define a public method to get the vehicle's speed
        public int GetSpeed()
        {
            // Return the speed of the vehicle
            return speed;
        }

        // Define a public method to set the vehicle's color
        public void SetColor(string color)
        {
            // Assign the color parameter to the color field
            this.color = color;
        }

        // Define a public method to get the vehicle's color
        public string GetColor()
        {
            // Return the color of the vehicle
            return color;
        }

        // Define a virtual method for driving
        public virtual void Drive()
        {
            // Print a message indicating that the vehicle is driving
            Console.WriteLine("The vehicle is driving.");
        }
    }

    // Define the Car class, inheriting from Vehicle
    public class Car : Vehicle
    {
        // Declare a private field for the number of doors in the car
        private int numberOfDoors;

        // Define a public method to set the number of doors
        public void SetNumberOfDoors(int doors)
        {
            // Assign the doors parameter to the numberOfDoors field
            numberOfDoors = doors;
        }

        // Define a public method to get the number of doors
        public int GetNumberOfDoors()
        {
            // Return the number of doors
            return numberOfDoors;
        }

        // Override the Drive method to specify that a car is driving
        public override void Drive()
        {
            // Print a message indicating that the car is driving
            Console.WriteLine("The car is driving.");
        }
    }

    // Define the Truck class, inheriting from Vehicle
    public class Truck : Vehicle
    {
        // Declare a private field for the load capacity of the truck
        private int loadCapacity;

        // Define a public method to set the load capacity
        public void SetLoadCapacity(int capacity)
        {
            // Assign the capacity parameter to the loadCapacity field
            loadCapacity = capacity;
        }

        // Define a public method to get the load capacity
        public int GetLoadCapacity()
        {
            // Return the load capacity
            return loadCapacity;
        }

        // Override the Drive method to specify that a truck is driving
        public override void Drive()
        {
            // Print a message indicating that the truck is driving
            Console.WriteLine("The truck is driving.");
        }
    }

    // Define the MiniVan class, inheriting from Vehicle
    public class MiniVan : Vehicle
    {
        // Declare a private field to indicate if the minivan has dual sliding doors
        private bool hasDualSlidingDoors;

        // Define a public method to set if the minivan has dual sliding doors
        public void SetDualSlidingDoors(bool hasDoors)
        {
            // Assign the hasDoors parameter to the hasDualSlidingDoors field
            hasDualSlidingDoors = hasDoors;
        }

        // Define a public method to check if the minivan has dual sliding doors
        public bool HasDualSlidingDoors()
        {
            // Return true if the minivan has dual sliding doors
            return hasDualSlidingDoors;
        }

        // Override the Drive method to specify that a minivan is driving
        public override void Drive()
        {
            // Print a message indicating that the minivan is driving
            Console.WriteLine("The minivan is driving.");
        }
    }

    // Define the main entry point of the program
    public static void Main()
    {
        // Create a Vehicle object, set attributes, and call the Drive method
        Vehicle vehicle = new Vehicle();
        vehicle.SetSpeed(50);
        vehicle.SetColor("Red");
        vehicle.Drive();

        // Create a Car object, set attributes, and call the Drive method
        Car car = new Car();
        car.SetSpeed(80);
        car.SetColor("Blue");
        car.SetNumberOfDoors(4);
        car.Drive();

        // Create a Truck object, set attributes, and call the Drive method
        Truck truck = new Truck();
        truck.SetSpeed(60);
        truck.SetColor("Black");
        truck.SetLoadCapacity(1000);
        truck.Drive();

        // Create a MiniVan object, set attributes, and call the Drive method
        MiniVan minivan = new MiniVan();
        minivan.SetSpeed(70);
        minivan.SetColor("White");
        minivan.SetDualSlidingDoors(true);
        minivan.Drive();
    }
}

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

  •  Class Photo Album

    Write a C# class called "PhotoAlbum" with a private attribute "numberOfPages". The class should also have a public method named "GetNumberOfPages", whic...

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