Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
#include <iostream>
using namespace std;
// Enumeration to represent the states of the landing gear
enum LandingGearState {
RETRACTED,
EXTENDED
};
// Function to control the landing gear
void controlLandingGear(LandingGearState& currentState, bool switchUp, bool switchDown) {
switch (currentState) {
case RETRACTED:
if (switchUp) {
cout << "Switch up detected. Extending landing gear." << endl;
currentState = EXTENDED;
}
break;
case EXTENDED:
if (switchDown) {
cout << "Switch down detected. Retracting landing gear." << endl;
currentState = RETRACTED;
}
break;
}
}
int main() {
LandingGearState currentState = RETRACTED;
bool switchUp = false;
bool switchDown = false;
// Simulating the operation of the landing gear
cout << "Current landing gear state: RETRACTED" << endl;
switchUp = true;
controlLandingGear(currentState, switchUp, switchDown);
cout << "Current landing gear state: " << (currentState == RETRACTED ? "RETRACTED" : "EXTENDED") << endl;
switchUp = false;
switchDown = true;
controlLandingGear(currentState, switchUp, switchDown);
cout << "Current landing gear state: " << (currentState == RETRACTED ? "RETRACTED" : "EXTENDED") << endl;
return 0;
}

In this code, the landing gear control is implemented using a controlLandingGear function that takes the current state of the landing gear (currentState) and the status of the switches (switchUp and switchDown) as input.
The function uses a switch-case statement to handle different states of the landing gear. If the current state is RETRACTED and the switchUp is true, the landing gear is extended. If the current state is EXTENDED and the switchDown is true, the landing gear is retracted.
In the main function, the operation of the landing gear is simulated by setting the values of switchUp and switchDown variables. The current state of the landing gear is displayed before and after calling the controlLandingGear function to show the state transitions.
In the code, we have two switches: switchUp and switchDown, which represent the switches for raising and lowering the landing gear, respectively.
Initially, the landing gear is in the retracted state (RETRACTED).
The code then checks the state of the landing gear and the status of the switches. If the landing gear is in the retracted state and the switchUp is true, it means the pilot wants to extend the landing gear. So, the code changes the state of the landing gear to EXTENDED.
Similarly, if the landing gear is in the extended state and the switchDown is true, it means the pilot wants to retract the landing gear. The code then changes the state of the landing gear back to RETRACTED.
After each operation, the code displays the current state of the landing gear.
So, when you run the program, it will output the following:
- Initially, the landing gear is in the
RETRACTEDstate. - The program detects the
switchUpsignal and extends the landing gear, changing the state toEXTENDED. - The program then detects the
switchDownsignal and retracts the landing gear back to theRETRACTEDstate.
The final output will show that the landing gear is in the RETRACTED state.
Overall, the code demonstrates a simplified simulation of controlling an airplane's landing gear using two switches, where the gear is extended or retracted based on the signals received from the switches.
- Initially, the landing gear state is set to
RETRACTED. - When
switchUpis set totrue, the code detects the switch up and extends the landing gear. The current state changes toEXTENDED. - Next,
switchUpis set tofalseandswitchDownis set totrue. The code detects the switch down and retracts the landing gear. The current state changes back toRETRACTED. - The final output shows the current landing gear state, which is
RETRACTEDafter the retraction operation.