TripComputer to compute the time of a trip. TripComputer should have the private
ID: 3806618 • Letter: T
Question
TripComputer to compute the time of a trip. TripComputer should have the private attributes total Time-the total time for the trip a boolean flag that indicates whether a rest stop has been taken at the restStop Taken end of the current leg and the following methods: computeLegTime (distance, speed-computes the time for a leg of the trip having a given distance in miles and speed in miles per hour. If either the distance or the speed is negative, throws an exception. takeRestStop (time)-takes a rest stop for the given amount of time. If the time is negative, throws an exception. Also throws an exception if the client code attempts to take two rest stops in a row. getTrip Time-returns the current total time for the trip. Here is one possible configuration of the labels, buttons, and text fields required by the trip- time calculator TRIP TinE CALCULATOR RODLEG ADD STOP STOP TIME DISTRACE yOUR TRIP TIME SPEED SO FAR:
Explanation / Answer
#include <stdexcept>
class TripCalculator{
private:
float totalTime;
bool restStopTaken;
public:
TripCalculator(){
totalTime = 0;
restStopTaken = false;
}
void computeLegTime(int dist, int speed)
{
if (speed < 0 || dist < 0)
{
throw std::invalid_argument( "received negative value" );
}
else
{
totalTime = totalTime + dist/speed
}
}
void takeRestStop(int time)
{
if (time < 0)
{
throw std::invalid_argument( "received negative value" );
}
else
{
if (restStopTaken){
throw "Trying to take rest in a row";
}
else{
restStopTaken = false;
}
}
}
float getTripTime(){
return totalTime;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.