Class Vehicles C# Exercise - C# Programming Course

 Exercise

Class Vehicles

 Objetive

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

 Example Code

// 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();
    }
}

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.