Answer to Question #156634 in C++ for Lim

Question #156634

Turn this program into C++ CLASS AND OBJECTS.

Company ABC, an airline company is closely monitoring the distance between airplanes as they take-off on the same airport. You are asked to create a system that would calculate the distance between two planes A and B given the following plane attributes: Take-off Time, Speed (mi/hr), direction (north, east, west, south). Also, given the time where the navigation officer would like to check the distance.

.


#include <iostream>


using namespace std;


int main()

{

    int flightNo1=0, flightNo2=0, speed1=0, speed2=0;

    char direction1[5];

    char direction2[5];

    int time1=0, time2=0, distance=0;

    cout<<"-----------You are welcome in ABC Airline Co.------------"<<endl;

    // First flight details

    cout<<" Please enter the flight name: "<< endl;

    cin>>flightNo1;

    cout<<"Please enter flight number:"<<flightNo1 <<"take off time:"<< endl;

    cin>>time1;

    cout<<"Please enter the speed of flight number:"<<flightNo1 << endl;

    cin>>speed1;

    cout<<"The speed of the flight number:"<<flightNo1<<"is:"<<speed1<<endl;

    cout<<"Please enter the direction of flight number:"<<flightNo1 << endl;

    cin>>direction1;

    cout<<"The direction of the flight number:"<<flightNo1<<"is:"<<direction1<<endl;

    

    // second flight detail

    cout<<" Please enter the flight Number of second flight "<< endl;

    cin>>flightNo2;

    cout<<"Please enter the take off time of the flightNo2 :"<<flightNo2<< endl;

    cin>>time2;

    cout<<"The take off time of flight number:"<<flightNo2<<"is:"<<time2<<endl;

    cout<<"Please enter the speed of flight number:"<<flightNo2 << endl;

    cin>>speed2;

    cout<<"The speed of the flight number:"<<flightNo2<<"is:"<<speed2<<endl;

    cout<<"Please enter the direction of flight number:"<<flightNo2 << endl;

    cin>>direction2;

    cout<<"The direction of the flight number:"<<flightNo2<<"is:"<<direction2<<endl;

    

    distance=speed1*time1-speed2*time2;

    cout<<"The distance between the two flights be:"<<distance<<endl;

    return 0;

}




1
Expert's answer
2021-01-21T17:06:51-0500
#include <iostream>
#include <cmath>
using namespace std;

class Plane {
    enum Direction {north, east, south, west};
    int number;
    Direction direction;
    double speed;
    int takeOffH, takeOffM;
public:
    void read();
    void write() const;
    double distance(const Plane& other, int h, int m) const;
};

void Plane::read() {
    cout << "Flight number ";
    cin >> number;
    while (true) {
        cout << "Flight direction (n/e/w/s) ";
        char ch;
        cin >> ch;
        if (ch == 'n' || ch == 'N') {
            direction = north;
            break;
        }    
        else if (ch == 'e' || ch == 'E') {
            direction = east;     
            break;
        }    
        else if (ch == 's' || ch == 'S') {
            direction = south;     
            break;    
        }    
        else if (ch == 'w' || ch == 'W') {
            direction = west;     
            break;    
        }
    }
    cout << "Plane speed ";
    cin >> speed;
    cout << "Take off time (houres) ";
    cin >> takeOffH;
    cout << "Take off time (minutes) ";
    cin >> takeOffM;
}

void Plane::write() const
{
    cout << "Flight #" << number << " follow to ";
    if (direction == north)
        cout << "north";
    else if (direction == east)    
        cout << "east";
    else if (direction == south)
        cout << "south";
    else
        cout <<  "west";
    cout << " with speed  " << speed << " ml/hr;  " ;
    cout << "Take off time " << takeOffH << ":" << takeOffM << endl;
}

double Plane::distance(const Plane& other, int h, int m) const
{
    double time = h + m /60.0;
    double timeFlyMy = time - (takeOffH + takeOffM / 60.0);
    double timeFlyOther = time - (other.takeOffH + other.takeOffM / 60.0);
    if (timeFlyMy < 0) timeFlyMy = 0.0;
    if (timeFlyOther < 0) timeFlyOther = 0.0;
    
    double myX = 0.0, myY = 0.0;
    if (direction== north)
        myY = timeFlyMy * speed;
    else if (direction == east)   
        myX = timeFlyMy * speed;     
    else if (direction== south)
        myY = -timeFlyMy * speed;
    else    
        myX = -timeFlyMy * speed;    
    
    double otherX = 0.0, otherY = 0.0;
    if (other.direction== north)
        otherY = timeFlyOther * other.speed;
    else if (other.direction == east)   
        otherX = timeFlyOther * other.speed;     
    else if (other.direction== south)
        otherY = -timeFlyOther * other.speed;
    else    
        otherX = -timeFlyOther * other.speed;
 
    return sqrt((myX-otherX)*(myX-otherX) + (myY-otherY)*(myY-otherY));      
}


int main()

{
    cout<<"-----------You are welcome in ABC Airline Co.------------"<<endl;
    
    while (true) {
        Plane f1, f2;
        cout << "Enter data for plane 1" << endl;
        f1.read();
        cout << endl << "Enter data for plane 2" << endl;
        f2.read();
        
        cout << endl << "Which time you are interested in (Hr, min) ";
        int hr, min;
        cin >> hr >> min;
        
        double dist = f1.distance(f2, hr, min);
        cout << endl;
        cout << "===========================================================" << endl;
        cout << "At " << hr << ":" << min << " the disstance between" << endl;
        f1.write();
        cout << "and" << endl;
        f2.write();
        cout <<  "will be " << dist << " miles." << endl;
        cout << "===========================================================" << endl << endl;
        
        cout << "Whould you like to check another flights? ";
        char ans;
        cin >> ans;
        if (ans != 'y' && ans != 'Y')
            break;
    }
    

    cout << "Have a nice day!"  << endl;

    return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog