Question

In: Computer Science

Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance);...

Base Class

class TransportationLink {
 protected:
    string _name;
    double _distance;

 public:
    TransportationLink(const string &name, double distance);
    const string & getName() const;
    double getDistance() const;
    void setDistance(double);

    // Passes in the departure time (as minute) and returns arrival time (as minute)
    // For example: 
    //     8 am will be passed in as 480 minutes (8 * 60)
    //     2:30 pm will be passed in as 870 minutes (14.5 * 60) 
    virtual unsigned computeArrivalTime(unsigned minute) const = 0;
};

#endif

Derived Classes

Define and implement RoadSegment.h and RoadSegment.cpp

Define a RoadSegment class that inherits from the base TransportationClass from above. This class will have a vector<double> hourlySpeeds data field which will hold the speed for all 24 hours of the day (each index is 1 hour). The vector's default speed for all hours will be passed in to the constructor. You will be able to set/change a speed for a specific hour, or all 24 speeds at once using setHourSpeed(unsigned hour, double speed) or setAllHourSpeeds(const vector<double> &) respectively.

The length of time on the road segment is calculated by taking it's distance and dividing by the speed at the time of departure. This length of time is then added to the departureTime parameter to get the arrivalTime.

Define and implement RiverSegment.h and RiverSegment.cpp

Define a RiverSegment class that inherits from the base TransportationClass from above. This class will have a vector<double> scheduledDepartureTimes data field that will keep track of the departure time (e.g. 10.5 represents the time 10:30) of all ferries on the river. You can assume these are sorted. Additionally, there is a _speed value that is the speed of the ferry at all times (ferries tend to be consistently slow). There is a setSpeed function to change the _speed value. Additionally, there is an addDepartureTime(double hour) function to add a departure time to the vector. Remember, these times need to be sorted.

Computing the arrival time for the RiverSegment is a little more complicated since you need to wait for the next available departure time. Once you find the next available departure time (after the departure time passed in), you need to add the length of time on the river segment. This is done by dividing the _distance by the _speed. Add the time on the river to the departure time (from the vector of departure times) and this will give you the arrival time.

Note: If there is no departure time scheduled for after your planned departure, you need to take the first available departure the following day. Our solution assumes the departure times are the same every day.

\*TransportationLink.cpp*\

#include "TransportationLink.h"

#include "TransportationLink.h"

#include <string>

using namespace std;

TransportationLink::TransportationLink(const string &name, double distance)
: _name(name), _distance(distance)
{}

const string & TransportationLink::getName() const {
return _name;
}

double TransportationLink::getDistance() const {
return _distance;
}

void TransportationLink::setDistance(double distance) {
_distance = distance;
}

\*main.cpp*\

#include "RoadSegment.h"
#include "RiverSegment.h"

#include <iostream>

using namespace std;

void constructRoute(vector<TransportationLink *> &);
int routeArrivalTime(const vector<TransportationLink *> &, unsigned);
void addRoad(vector<TransportationLink *> &);
void addRiver(vector<TransportationLink *> &);

int main() {

vector<TransportationLink *> route1;
constructRoute(route1);

unsigned departureTime;
unsigned arrivalTime;

cout << "Enter route departure time (in minutes): ";
cin >> departureTime;
cout << endl;

arrivalTime = routeArrivalTime(route1, departureTime);

cout << "Arrival Time: " << arrivalTime << endl;
  
return 0;
}

void constructRoute(vector<TransportationLink *> &route) {
char option = 'q';

do {
cout << endl;
cout << "Menu" << endl;
cout << "- Add Road ('D' or 'd')" << endl;
cout << "- Add River ('R' or 'r')" << endl;
cout << "- Quit ('Q' or 'q')" << endl;
cout << "Choose an action: ";
cin >> option;
cout << endl;
cin.ignore(); // get rid of whitespace character
  
if (option == 'd' || option == 'D') {
   addRoad(route);
}
else if (option == 'r' || option == 'R') {
   addRiver(route);
}
} while (option != 'q' && option != 'Q');
}

int routeArrivalTime(const vector<TransportationLink *> &route, unsigned departureTime) {
unsigned arrivalTime = departureTime;
for (unsigned i = 0; i < route.size(); ++i) {
arrivalTime = route.at(i)->computeArrivalTime(arrivalTime);
}
return arrivalTime;
}

void addRoad(vector<TransportationLink *> &route) {
string name;
double speed;
double distance;
char answer = 'n';
unsigned hour;

cout << "Enter name of road: ";
getline(cin, name);
cout << endl;
cout << "Enter road's default speed: ";
cin >> speed;
cout << endl;
cout << "Enter road's distance: ";
cin >> distance;
cout << endl;
RoadSegment *road = new RoadSegment(name, speed, distance);

cout << "Enter non-default speeds by hour (if any):" << endl;
cout << "Enter new speed? (y or n): ";
cin >> answer;
cout << endl;
while (answer != 'n' && answer != 'N') {
cout << "Which hour? (0 to 23): ";
cin >> hour;
cout << endl;
if (hour > 23) {
cout << "Invalid hour. Speed not updated." << endl;
} else {
cout << "Enter speed for hour " << hour << ": ";
cin >> speed;
cout << endl;
road->setHourSpeed(hour, speed);
}
cout << "Enter new speed? (y or n): ";
cin >> answer;
cout << endl;
}
route.push_back(road);
}

void addRiver(vector<TransportationLink *> &route) {
string name;
double speed;
double distance;
char answer = 'n';
double hour;

cout << "Enter name of river: ";
getline(cin, name);
cout << endl;
cout << "Enter river's speed: ";
cin >> speed;
cout << endl;
cout << "Enter river's distance: ";
cin >> distance;
cout << endl;
RiverSegment *river = new RiverSegment(name, speed, distance);

cout << "Enter departure times:" << endl;

do {
cout << "Enter time as an hour: (0 to 23.99): ";
cin >> hour;
cout << endl;
if (hour >= 24) {
cout << "Invalid hour. Departure time not added." << endl;
} else {
river->addDepartureTime(hour);
}
cout << "Enter new departure time? (y or n): ";
cin >> answer;
cout << endl;
} while (answer != 'n' && answer != 'N');

route.push_back(river);
}

\*TransportationLink.h*\

#include <string>

using namespace std;

#ifndef __TRANSPORTATIONLINK_H__
#define __TRANSPORTATIONLINK_H__

const int HOURS_PER_DAY = 24;
const int MINS_PER_HOUR = 60;
const int MINS_PER_DAY = MINS_PER_HOUR * HOURS_PER_DAY; //24 * 60

class TransportationLink {
protected:
string _name;
double _distance;

public:
TransportationLink(const string &, double);
const string & getName() const;
double getDistance() const;
void setDistance(double);

// Passes in the departure time (as minute) and returns arrival time (as minute)
// For example:
// 8 am will be passed in as 480 (8 * 60)
// 2:30 pm will be passed in as 870 (14.5 * 60)
virtual unsigned computeArrivalTime(unsigned minute) const = 0;
};

#endif

Solutions

Expert Solution


Related Solutions

C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
protected String name;                                      &nbsp
protected String name;                                                                                                           protected ArrayList<Stock> stocks;     protected double balance;                                                                              + Account ( String name, double balance) //stocks = new ArrayList<Stock>() ; + get and set property for name; get method for balance + void buyStock(String symbol, int shares, double unitPrice ) //update corresponding balance and stocks ( stocks.add(new Stock(…..)); ) + deposit(double amount) : double //returns new balance                                                                 + withdraw(double amount) : double //returns new balance + toString() : String                                                                  @Override     public String toString(){         StringBuffer str =...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price;...
PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price; protected int howMany; // constructor public InventoryItem(String description, double price, int howMany) { this.description = description; this.price = price; this.howMany = howMany; } // copy constructor public InventoryItem(InventoryItem obj) { this.description = obj.description; this.price = obj.price; howMany = 1; } // clone method @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } // toString method @Override public String toString() { return description +...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x);...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x); void setY(double y); void setZ(double z); double getX() const; double getY() const; double getZ() const; point3d& operator=(const point3d& rhs); private: double x; double y; double z; }; Given the Point class above, complete the following: 1. Write just the signature for the overloaded addition operator that would add the respective x,y, and z from two point3d objects. 2. What is the output of the...
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT