Vehicle Programming Assignment
Vehicle Interface
Create an interface `IVehicle` with two methods:
- Drive() of type void
- Refuel(int amount) of type void
Code:
------
public interface IVehicle
{
void Drive();
void Refuel(int amount);
}
LandVehicle Class
The LandVehicle class inherits from IVehicle. It includes:
- Fields: brandOrModel, noOfSeats, color (private with public properties)
- Static Field: Count for tracking the number of vehicles
- Methods:
- Drive() and Refuel(int amount) implementations
- A Break() method for stopping
Code:
------
public class LandVehicle : IVehicle
{
private string brandOrModel;
private int noOfSeats;
private string color;
public string BrandOrModel { get => brandOrModel; set => brandOrModel = value; }
public int NoOfSeats
{
get => noOfSeats;
set
{
if (value < 1 || value > 70)
throw new ArgumentException("Number of seats must be between 1 and 70.");
noOfSeats = value;
}
}
public string Color { get => color; set => color = value; }
public static int Count { get; private set; }
public LandVehicle()
{
Count++;
}
public LandVehicle(string brand, int seats, string color)
{
BrandOrModel = brand;
NoOfSeats = seats;
Color = color;
Count++;
}
public void Drive()
{
[Link]($"{brandOrModel} is Driving.");
}
public void Refuel(int amount)
{
[Link]($"Refueled with {amount} liters of fuel.");
}
public void Break()
{
[Link]($"{brandOrModel} has stopped.");
}
}
Automobile Class
The Automobile class inherits from LandVehicle and implements IVehicle.
- Fields: plateNumber, ownerName
- Static Field: Count for tracking the number of automobiles.
Code:
------
public class Automobile : LandVehicle
{
public string PlateNumber { get; set; }
public string OwnerName { get; set; }
public static int AutomobileCount { get; private set; }
public Automobile()
{
AutomobileCount++;
}
public Automobile(string brand, int seats, string color, string plate, string owner)
: base(brand, seats, color)
{
PlateNumber = plate;
OwnerName = owner;
AutomobileCount++;
}
}