Question

In: Computer Science

Imagine that you are working for a logistics company operating out in the frontiers of civilisation....

Imagine that you are working for a logistics company operating out in the frontiers of
civilisation. Your boss wants to be able to run some basic analytics on some of the
routes that you currently support and wants you to implement some systems to help
her. However the current limitations of the legacy system prevent you from making
fundamental changes. Instead, you will need to use inheritance in order to achieve your
goal.

Vehicle Class
The vehicle class is the parent class of the derived class:electricLocomotive. Their
inheritance will be public inheritance so reflect that appropriately in their .h files. The
description of the vehicle class is given in the simple UML diagram below:

vehicle
-map: char**
-name: string
-size:int
--------------------------
+vehicle()
+getSize():int
+setName(s:string):void
+getName():string
+getMap():char**
+setMap(s: string):void
+getMapAt(x:int, y:int):char
+∼vehicle()
+operator−−():void
+determineRouteStatistics()=0:void
The class variables are as follows:
• map: A 2D array of chars, it will represent the map that each vehicle will have to
travel on.
• name: The name of the vehicle. For example, ”Frontier Express”.
• size: The size of the map as a square matrix.
The class methods are as follows:
• vehicle: This is the constructor of the class. It is simply the default constructor
with no additional features.
• getSize: This returns the size of the map as a square matrix.
• setName: This will set the name of the vehicle as received.
• getName: This will return the vehicle as set.
• getMap(): This will return the entire map variable.
• setMap(): This method receives the name of a text file that contains an ASCII map.
The map will be a square, equal number of rows and columns. The first line of the
map will have the number of rows. Every line after will contain a number of ASCII
characters that you must read into the map. This must allocate memory before
assigning the map. The textfile that you will receive in input has no delimiters.
On each line, all the characters are written out with no spaces or commas or other
delimiters separating them. For example:
----
• getMapAt: This receives two coordinates, an x and y, and returns what character
is located at those coordinates. If the coordinates are out of bounds, return ’:’

∼vehicle: The destructor for the class. It has been made virtual.
• determineRouteStatistics: This function will be used to determine information from
the map based on requirements specific to the vehicle in question. As it stands, it
is made pure virtual.
• operator−−: The overload of this operator will deallocate the memory allocated for
the map.
electricLocomotive Class
The description of the electricLocomotive class is given by the simple UML diagram below:
electricLocomotive
-perUnitCost: double
---------------------------------
+electricLocomotive()
+~electricLocomotive()
+getUnitCost():double
+setUnitCost(s:double):void
+determineRouteStatistics():void
The class has the following variables:
• perUnitCost: A double value showing the cost per each rail unit.
The class methods have the following behaviour:
• electricLocomotive : The constructor of the class. It has no features beyond the
default.
• getUnitCost,setUnitCost: The getter and setter for the unit cost variable.
• ∼electricLocomotive : This is the class destructor that will deallocate the memory
assigned by the class. It will also print out, ”electric locomotive removed”, without
the quotation marks and ended by a new line.
• determineRouteStatistics: This function needs to calculate the specific statistics for
the locomotive based on the map it is provided. The following key shows all the
specific elements that are pertinent to the locomotive:
1. O: Origin Points. This is where the trains will be expected to leave from.
2. E: Exit Points. This is where the train is expected to go towards.
3. #: Railroad. This is traversable tracks for the train. Locomotives can only
travel on the map where there is track laid.
The function will then determine a number of statistics and print them to the screen
in a neatly formatted way:

Distance: Distance from the origin to exit in units, where one ”#” is one unit
so a track of ”### ” is a 3 unit long track. This does not include the origin
and exit points.
2. Cost: This will display the cost of the rail construction. The cost of the rail is
the number of tracks multiplied by perUnitCost. The cost should be converted
into a whole integer, rounding down.
Display the information as follows:
Name: Frontier Express
Origin Coordinates: 1,2
Exit Coordinates: 8,7
Distance: 16
Cost: 160000
Finally an example small map is provided below:
O#--
-#--
-#--
-##E
You will be allowed to use the following libraries: fstream, cstring, string, iostream.
You will have a maximum of 10 uploads for this task. Your submission must contain
vehicle.h, vehicle.cpp, electricLocomotive.cpp, electricLocomotive.h, map1.txt
,main.cpp and a makefile.

Solutions

Expert Solution

#######################################
         vehicle.cpp
#######################################
#include "vehicle.h"
#include<fstream>
#include<cstring>

Vehicle::Vehicle() {
    size = 0;
    map = NULL;
    name = "";
}

void Vehicle::setName(string s) {
    name = s;
}
string Vehicle::getName() {
    return name;
}
char** Vehicle::getMap() {
    return map;
}
int Vehicle::getSize() {
    return size;
}
void Vehicle::setMap(string s) {
    ifstream f(s.c_str());
    
    if(!f.fail()) {
        f >> size;
        map = new char*[size];

        for(int i=0; i<size; i++) {
            string x;
            f >> x;
            map[i] = new char[size];
            strcpy(map[i], x.c_str());
        }
    }
}
char Vehicle::getMapAt(int x, int y) {
    return map[x][y];
}
Vehicle::~Vehicle() {
    for(int i=0; i<size; i++) {
        delete [] map[i];
    }
    delete [] map;
}

void Vehicle::operator--() {
    for(int i=0; i<size; i++) {
        delete [] map[i];
    }
    delete [] map;
}




#######################################
           vehicle.h
#######################################
#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>

using namespace std;

class Vehicle {
    char **map;
    string name;
    int size;

    public:
    Vehicle();
    void setName(string s);
    string getName();
    char **getMap();
    int getSize();
    void setMap(string s);
    char getMapAt(int x, int y);
    ~Vehicle();
    virtual void determineRouteStatisitcs() = 0;
    void operator--();
};


#endif



#######################################
electricLocomotive.cpp
#######################################
#include "electricLocomotive.h"
#include<fstream>
#include<cstring>

ElectricLocomotive::ElectricLocomotive() {
        perUnitCost = 0;
}
ElectricLocomotive::~ElectricLocomotive() {
        cout << "Electric Locomotive removed" << endl;
}
int ElectricLocomotive::getUnitCost() {
        return perUnitCost;
}
void ElectricLocomotive::setUnitCost(double s) {
        perUnitCost = s;
}

// position(r, c) is where we start, We need to reach till
// we get cell 'E'
// Also, We keep a boolean array to mark the cells which
// are already visited
int distanceHelperEL(char **map, int size, int r, int c, bool **visited) {
        // if current cell is invalid or visited, Then return -1 to show error
        if(r < 0 || r >= size || c < 0 || c >= size 
                || visited[r][c] || map[r][c] == '-') {
                return -1;
        }

        visited[r][c] = true;
        if(map[r][c] == 'E') {
                return 0;
        }

        // Try visiting to neighbors.
        int dis = distanceHelperEL(map, size, r+1, c, visited);
        if(dis != -1) {
                return 1 + dis;
        }
        dis = distanceHelperEL(map, size, r-1, c, visited);
        if(dis != -1) {
                return 1 + dis;
        }
        dis = distanceHelperEL(map, size, r, c+1, visited);
        if(dis != -1) {
                return 1 + dis;
        }
        dis = distanceHelperEL(map, size, r, c-1, visited);
        if(dis != -1) {
                return 1 + dis;
        }

        return -1; // Not possible to reach
}

void ElectricLocomotive::determineRouteStatisitcs() {
        char **map = Vehicle::getMap();
        int size = Vehicle::getSize();

        int originRow, originCol;
        int exitRow, exitCol;
        
        for(int i=0; i<size; i++) {
                for(int j=0; j<size; j++) {
                        if(map[i][j] == 'O') {
                                originRow = i;
                                originCol = j;
                        }
                        if(map[i][j] == 'E') {
                                exitRow = i;
                                exitCol = j;
                        }
                }
        }

        // TODO, Find the logic for reaching to the destination
        // And it the trip is viable
        bool **visited = new bool*[size];
        for(int i=0; i<size; i++) {
                visited[i] = new bool[size];
                for(int j=0; j<size; j++) {
                        visited[i][j] = false;
                }
        }

        cout << "Name: " << Vehicle::getName() << endl;
        cout << "Origin Coordinate: " << originRow << "," << originCol << endl;
        cout << "Exit Coordinate: " << exitRow << "," << exitCol << endl;

        int distance = distanceHelperEL(map, size, originRow, originCol, visited);
        if(distance == -1) {
                cout << "End location can not be reached from origin." << endl;
        } else {
                cout << "Distance: " << distance << endl;
                cout << "Cost: " << distance * perUnitCost << endl;
        }
        
        // free the visited array we created for tracking.
        for(int i=0; i<size; i++) {
                delete [] visited[i];
        }
        delete [] visited;
}




#######################################
electricLocomotive.h
#######################################
#ifndef ELECTRIC_LOCOMOTIVE_H
#define ELECTRIC_LOCOMOTIVE_H

#include <iostream>
#include "vehicle.h"

using namespace std;

class ElectricLocomotive: public Vehicle {
        
        double perUnitCost;

        public:
        ElectricLocomotive();
        ~ElectricLocomotive();
        int getUnitCost();
        void setUnitCost(double s);
        void determineRouteStatisitcs();
};


#endif


#######################################
            main.cpp
#######################################
#include "locomotive.h"
#include "dieselLocomotive.h"
#include "electricLocomotive.h"

int main() {
    ElectricLocomotive e;
    e.setUnitCost(1000);
    e.setName("Frontier Express");
    e.setMap("map3.txt");
    e.determineRouteStatisitcs();
    return 0;
}

#######################################
            map3.txt
#######################################
4
O#--
-#--
-#--
-##E


**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

clang version 7.0.0-3~ubuntue. E clang++-7 -pthread -o main o otive.cpp main.cpp vehicle.cp : ./main Name: Frontier Express Supply Range: 12 Origin Coordinate: 0,0 Exit Coordinate: 3,3 Distance: 6 Number of Stations: 1 #include "electricLocomotive.h" 1 #include<fstream> 2 #include<cstring> 4 ElectricLocomotive: : ElectricLocomotive() { 5 perUnitCost 6 0; = } ElectricLocomotive::ElectricLocomotive() { CO Status: Viable cout < "Electric Locomotive removed" << endl; Name: Frontier Express Origin Coordinate: 0,0 Exit Coordinate: 3,3 Distance: 6 } 10 int ElectricLocomotive::getUnitCost(O { 11 return perUnitCost; 12 Passengers Carried: 60 Status: Viable 13 void ElectricLocomotive: : setUnitCost(double s) { 14 perUnitCost = s; 15 Name: Frontier Express Origin Coordinate: 0,0 Exit Coordinate: 3,3 } 16 17 Distance: 6 // position(r, c) is where we start, We need to reach till //we get cell 'E' // Also, We keep 18 Cost: 6000 Electric Locomotive removed 19 Diesal Locomotive removed Locomotive removed a boolean array to mark the cells which 20 // are already visited int distanceHelperEL(char **map, int size, int r, int c, bool *visited) { 21 22 //if current cell is invalid or visited, Then return 23 -1 to show error if (r < 0 || r >= size || c < 0 || c >= size I| visited[r][c] || map[r][c] 24 '-') { 25 return -1; 26


Related Solutions

imagine that you are working for a logistics company operating out in the frontiers of civilisation....
imagine that you are working for a logistics company operating out in the frontiers of civilisation. Your boss wants to be able to run some basic analytics on some of the routes that you currently support and wants you to implement some systems to help her. However the current limitations of the legacy system prevent you from making fundamental changes. Instead, you will need to use inheritance in order to achieve your goal. 2.2.1 Vehicle Class The vehicle class is...
Imagine that you are working for a logistics company operating out in the frontiers of civilisation....
Imagine that you are working for a logistics company operating out in the frontiers of civilisation. Your boss wants to be able to run some basic analytics on some of the routes that you currently support and wants you to implement some systems to help her. However the current limitations of the legacy system prevent you from making fundamental changes. Instead, you will need to use inheritance in order to achieve your goal. Vehicle Class The vehicle class is the...
Imagine that you are working for a logistics company operating out in the frontiers of civilisation....
Imagine that you are working for a logistics company operating out in the frontiers of civilisation. Your boss wants to be able to run some basic analytics on some of the routes that you currently support and wants you to implement some systems to help her. However the current limitations of the legacy system prevent you from making fundamental changes. Instead, you will need to use inheritance in order to achieve your goal. Vehicle Class The vehicle class is the...
Imagine that you are working for a company that asks you to develop a political and...
Imagine that you are working for a company that asks you to develop a political and economic risk checklist for any direct investment decision your company might undertake in a foreign country – your company wants to make sure it has considered all significant external risks (as opposed to strategic, competitive, financial, or market risks) that might affect the company's ability to generate positive returns on its investment. One example of a risk that needs to be considered is the...
Discuss the benefits of an effective reverse logistics process. Would you consider working in reverse logistics...
Discuss the benefits of an effective reverse logistics process. Would you consider working in reverse logistics as a career choice?
Imagine you are working for a Korean company. How you would be able to deal with...
Imagine you are working for a Korean company. How you would be able to deal with the Korean management style, and what would probably be the biggest cultural challenges for you to accommodate to such an environment?
Imagine that your company is switching to a matrix structure . Before, you were working in...
Imagine that your company is switching to a matrix structure . Before, you were working in a functional structure. Now, every employee is going to report to a product/team leader (Product A, B and C) as well as a department manager (ex: Marketing, Research & Development, Production, Procurement, and Finance). What are the sources of resistance you foresee for a change such as this? List and explain at least 5 steps/things to do?
Imagine that your company is switching to a matrix structure . Before, you were working in...
Imagine that your company is switching to a matrix structure . Before, you were working in a functional structure. Now, every employee is going to report to a product/team leader (Product A, B and C) as well as a department manager (ex: Marketing, Research & Development, Production, Procurement, and Finance). Create a list of things that need to be done after the change occurs. List and explain at least 5 things/steps to do?
Har Ltd. is a logistics company operating in Subang Jaya. They have recently bid for a...
Har Ltd. is a logistics company operating in Subang Jaya. They have recently bid for a Selangor wide contract of providing medicines to all the health units. This is an excellent opportunity for the company to expand their business. To fulfill this requirement, Omair, who is the CEO of the company is analyzing, how he should expand his fleet. The basic question in front of him is whether he should buy his own fleet of trucks, or lease them. And...
Har Ltd. is a logistics company operating in Subang Jaya. They have recently bid for a...
Har Ltd. is a logistics company operating in Subang Jaya. They have recently bid for a Selangor wide contract of providing medicines to all the health units. This is an excellent opportunity for the company to expand their business. To fulfill this requirement, Omair, who is the CEO of the company is analyzing, how he should expand his fleet. The basic question in front of him is whether he should buy his own fleet of trucks, or lease them. And...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT