A repository of exercises to support the training.
In this module, you will learn how to utilize lambda expressions to perform operations on collections effectively. You'll explore how to enhance your programming capabilities by employing functional programming techniques in a futuristic context.
Imagine a futuristic city with various modes of transportation, including hovercars, drones, and hyperloop trains. Each mode of transportation has different attributes such as speed, capacity, and fuel efficiency. In this exercise, you will create a collection of these transportation methods and use lambda expressions to filter, sort, and manipulate the data.
Instructions:
Transportation
with properties such as Name
, Speed
, Capacity
, and FuelEfficiency
. Make the class immutable by using readonly properties and a constructor to set the values.Example:
class Transportation
{
public string Name { get; set; }
public double Speed { get; set; } // Speed in mph
public int Capacity { get; set; } // Number of passengers
public double FuelEfficiency { get; set; } // MPG
public override string ToString()
{
return $"{Name}: {Speed} mph, Capacity: {Capacity}, Fuel Efficiency: {FuelEfficiency} MPG";
}
}
...
// Create a list of transportation methods
List<Transportation> transports = new List<Transportation>
{
new Transportation("Hovercar A", 220, 4, 30),
new Transportation("Drone B", 150, 2, 50),
new Transportation("Hyperloop C", 300, 50, 100),
new Transportation("Hovercar D", 180, 4, 25),
new Transportation("Drone E", 200, 1, 70)
};
Expected Outcome:
Next Steps:
Experiment further by adding more attributes to the Transportation
class, creating additional filtering and sorting criteria, or incorporating user input to enhance interactivity.