Question
Hello, please solve this problem for object oriented programming in C++ program language. I have final tomorrow so it will be very helpful. thank you.
PROBLEM 1: application that simulates the highway Create an In order to solve above mentioned requirements, it is necessary t
output for the following example 4. Test your application in main. You should produce same run: int main() Highway testHighwa
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>
#include <vector>
#include <iomanip>
#include <string>

using namespace std;

enum class Direction
{
   NORTH,
   SOUTH,
   EAST,
   WEST
};

class Track
{
public:
   Track(Direction direction, bool bmotorOnly) :m_direction(direction), m_bMotorcyclesOnly(bmotorOnly) {}
   Direction getTrackDirection() const
   {
       return m_direction;
   }
   bool isMotorCyclesOnlyTrack() const
   {
       return m_bMotorcyclesOnly;
   }
private:
   Direction m_direction;
   bool m_bMotorcyclesOnly;
};

class Vehicle
{
public:
   Vehicle(string licenseNumber):m_licenseNumber(licenseNumber){}
   virtual void accelerate() = 0;
   virtual void decelerate() = 0;
   virtual void calculateCurrPosition() = 0;
   void setTrack(Track* aTrack)
   {
       m_track = aTrack;
   }
   Track* getTrack()
   {
       return m_track;
   }
   double getCurPosition()
   {
       return m_curPosition;
   }
   void setCurPosition(double pos)
   {
       m_curPosition = pos;
   }
   void setCurSpeed(double curSpeed)
   {
       m_curSpeed = curSpeed;
   }
   string getType() const
   {
       return m_type;
   }
protected:
   string m_type;
   string m_licenseNumber;
   double m_maxSpeed;
   double m_curSpeed = 0.0;
   double m_curPosition = 0.0;
   Track* m_track;
};

class Car:public Vehicle
{
public:
   Car(string licenseNumber) :Vehicle(licenseNumber)
   {
       m_maxSpeed = 7.0;
       m_type = "C";
   }
   void accelerate() override
   {
       if (m_curSpeed < m_maxSpeed)
           m_curSpeed += 2;

       if (m_curSpeed > m_maxSpeed)
           m_curSpeed = m_maxSpeed;
   }

   void decelerate() override
   {
       if(m_curSpeed > 0)
           m_curSpeed -= 2;

       if (m_curSpeed < 0)
           m_curSpeed = 0;
   }

   void calculateCurrPosition() override
   {
       if (m_track->getTrackDirection() == Direction::EAST)
       {
           m_curPosition += m_curSpeed * 1;
       }
       else if (m_track->getTrackDirection() == Direction::WEST)
       {
           m_curPosition -= m_curSpeed * 1;
       }
   }

};

class Truck :public Vehicle
{
public:
   Truck(string licenseNumber) :Vehicle(licenseNumber)
   {
       m_maxSpeed = 4.0;
       m_type = "T";
   }
   void accelerate() override
   {
       if(m_curSpeed < m_maxSpeed)
           m_curSpeed += 1;

       if (m_curSpeed > m_maxSpeed)
           m_curSpeed = m_maxSpeed;
   }

   void decelerate() override
   {
       if (m_curSpeed > 0)
           m_curSpeed -= 1;

       if (m_curSpeed < 0)
           m_curSpeed = 0;
   }

   void calculateCurrPosition() override
   {
       if (m_track->getTrackDirection() == Direction::EAST)
       {
           m_curPosition += m_curSpeed * 1;
       }
       else if (m_track->getTrackDirection() == Direction::WEST)
       {
           m_curPosition -= m_curSpeed * 1;
       }
   }
};

class Motorcycle :public Vehicle
{
public:
   Motorcycle(string licenseNumber) :Vehicle(licenseNumber)
   {
       m_maxSpeed = 8.0;
       m_type = "M";
   }
   void accelerate() override
   {
       if (m_curSpeed < m_maxSpeed)
           m_curSpeed += 2;

       if (m_curSpeed > m_maxSpeed)
           m_curSpeed = m_maxSpeed;
   }

   void decelerate() override
   {
       if (m_curSpeed > 0)
           m_curSpeed -= 2;

       if (m_curSpeed < 0)
           m_curSpeed = 0;
   }

   void calculateCurrPosition() override
   {
       if (m_track->getTrackDirection() == Direction::EAST)
       {
           m_curPosition += m_curSpeed * 1;
       }
       else if (m_track->getTrackDirection() == Direction::WEST)
       {
           m_curPosition -= m_curSpeed * 1;
       }
   }
};


class Highway
{
public:
   void addVehicle(Vehicle* vehicle,Track* track)
   {
       //Do not add any other vehicle other than the Motocycle in Motorcycle only track
       if (track->isMotorCyclesOnlyTrack())
       {
           if (vehicle->getType() != "M")
               return;
       }
       vehicle->setCurSpeed(3);
       if (track->getTrackDirection() == Direction::EAST)
       {
           vehicle->setCurPosition(0);
           for (Vehicle* aVehicle : vVehicles)
           {
               //Safety check whether the vehicle belongs to the same track and the distance
               if (aVehicle->getTrack() == track && aVehicle->getCurPosition() <= 3)
                   return;
           }
       }
       else if (track->getTrackDirection() == Direction::WEST)
       {
           vehicle->setCurPosition(30);
           for (Vehicle* aVehicle : vVehicles)
           {
               if (aVehicle->getTrack() == track && aVehicle->getCurPosition() <= 27)
                   return;
           }
       }
       vehicle->setTrack(track);
       vVehicles.push_back(vehicle);
   }

   void addTrack(Track* track)
   {
       vTracks.push_back(track);
   }

   void show()
   {
       cout << "----------------------------------------------------------------";
       for (size_t i = 0; i < vTracks.size(); i++)
       {
           cout << "\n-----------------------------------------------------------------------";
           cout << "\n";
           cout << "\n";
           for (size_t j = 0; j < vVehicles.size(); j++)
           {
               if (vVehicles[j]->getTrack() == vTracks[i] &&
                   vVehicles[j]->getCurPosition() < 30)
               {
                   cout << setw(vVehicles[j]->getCurPosition()) << vVehicles[j]->getType();
               }
           }
           cout << "\n";
           cout << "\n";
           cout << "\n-----------------------------------------------------------------------";
       }
      
   }

   void advanceTime()
   {
       for (Vehicle*& aVehicle : vVehicles)
       {
           aVehicle->calculateCurrPosition();
       }
   }
private:
   vector<Track*> vTracks;
   vector<Vehicle*> vVehicles;
};

int main(int argc, char** argv)
{
   Highway testHighway;

   Track t1(Direction::EAST, false);
   Track t2(Direction::EAST, true);
   Track t3(Direction::WEST, false);

   testHighway.addTrack(&t1);
   testHighway.addTrack(&t2);
   testHighway.addTrack(&t3);

   Car c1("ABC-123");
   Truck tr1("REG-ABC");
   Motorcycle m1("MC-AI00");

   testHighway.addVehicle(&c1, &t1);
   testHighway.addVehicle(&tr1, &t3);
   testHighway.addVehicle(&m1, &t2);

   m1.accelerate();
   testHighway.advanceTime();
   c1.accelerate();
   c1.accelerate();
   testHighway.advanceTime();
   testHighway.advanceTime();
   testHighway.show();

   return 0;
}

OUTPUT:

Microsoft Visual Studio Debug Console C M

Add a comment
Know the answer?
Add Answer to:
Hello, please solve this problem for object oriented programming in C++ program language. I have final...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3 Problem: I hope you can help with this please answer the problem using python...

    Python 3 Problem: I hope you can help with this please answer the problem using python 3. Thanks! Code the program below . The program must contain and use a main function that is called inside of: If __name__ == “__main__”: Create the abstract base class Vehicle with the following attributes: Variables Methods Manufacturer Model Wheels TypeOfVehicle Seats printDetails() - ABC checkInfo(**kwargs) The methods with ABC next to them should be abstracted and overloaded in the child class Create three...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one as...

    java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one assignment per team of two via Omnivox and NOT MIO.Assignments sent via MIO will be deducted marks. Assignments must be done alone or in groups and collaboration between individuals or groups is strictly forbidden. There will be a in class demo on June 1 make sure you are prepared, a doodle will be created to pick your timeslot. If you submit late, there...

  • Please i need help with with this php files, the css does not have to look...

    Please i need help with with this php files, the css does not have to look like in the screen shot in the button. Create a file named paint.class.php and within it define a class named Paint, which has the following private properties: imgName title artist year gender paintID Define a static member variable named id, which will be used to set each instance’s paintID value and then be incremented, all inside the class constructor. Create a constructor that takes...

  • Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in...

    Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in interesting ways. Now you’ll get a chance to use more of what objects offer, implementing inheritance and polymorphism and seeing them in action. You’ll also get a chance to create and use abstract classes (and, perhaps, methods). After this project, you will have gotten a good survey of object-oriented programming and its potential. This project won’t have a complete UI but will have a...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT