Challenge

Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear

#include <iostream>

#include <string>

// Define the states of the landing gear

enum LandingGearState {

RETRACTED,

EXTENDED,

MOVING_UP,

MOVING_DOWN

};

// Define the class that controls the landing gear

class LandingGearControl {

private:

LandingGearState currentState;

public:

LandingGearControl() : currentState(RETRACTED) {} // Initial state is RETRACTED

// Function to change the state based on current state and valve action

void controlLandingGear(bool valveDirection) {

switch (currentState) {

case RETRACTED:

if (valveDirection) {

std::cout << "Gear is retracting...\n";

currentState = MOVING_UP; // Gear is moving up

} else {

std::cout << "Gear is already retracted.\n";

}

break;

case EXTENDED:

if (!valveDirection) {

std::cout << "Gear is extending...\n";

currentState = MOVING_DOWN; // Gear is moving down

} else {

std::cout << "Gear is already extended.\n";

}

break;

case MOVING_UP:

std::cout << "Gear is retracting...\n";

currentState = RETRACTED; // Gear has been retracted

break;

case MOVING_DOWN:

std::cout << "Gear is extending...\n";

currentState = EXTENDED; // Gear has been extended

break;

}

}

// Function to get the current state as a string

std::string getCurrentState() {

switch (currentState) {

case RETRACTED: return "Retracted";

case EXTENDED: return "Extended";

case MOVING_UP: return "Moving Up";

case MOVING_DOWN: return "Moving Down";

default: return "Unknown State";

}

}

};

int main() {

LandingGearControl landingGear;

// Simulate control of the landing gear

std::cout << "Initial state: " << landingGear.getCurrentState() << std::endl;

// Extend the gear

landingGear.controlLandingGear(false);

std::cout << "Current state: " << landingGear.getCurrentState() << std::endl;

// Retract the gear

landingGear.controlLandingGear(true);

std::cout << "Current state: " << landingGear.getCurrentState() << std::endl;

return 0;

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.