Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Overview Automobile Speed/Distance Application Program This programming project involves creating a C++ program which simulates the speed of a car and calculates the distance traveled
Overview Automobile Speed/Distance Application Program This programming project involves creating a C++ program which simulates the speed of a car and calculates the distance traveled over a period of time. This should be an interactive program which accepts keyboard input from the user and produces text output on the console. Each command entered by the user should execute one "time interval". For example, the "a" (accelerate) command should increase the speed by 5 miles per hour (MPH) over a time interval of 1 second. Similarly, the "b" (brake) command should decrease the speed by 5 MPH over a time interval of 1 second, and the "c" (cruise) command should keep the speed constant for 1 second. All of these operations include calculation of how far the car travels during the 1 second interval, and the total distance traveled since the program started. A separate "d" (demo) command outputs a sequence of steps, similar to that shown in the sample data, simply by executing the other operations in a predetermined sequence. This document discusses the requirements for the program, as well as possible implementation details. Be sure you understand this document before you begin coding your final solution. General Discussion of the Project Imagine a car which starts out "Stopped", at some initial position. We could say that the car is at "position zero", and its current speed is zero Miles-per-hour. If the driver of the car does nothing, then the car will remain at its initial position. However, if the driver steps on the gas pedal, the car will "accelerate": its speed will increase by some amount. Now that the car is moving, its position will change over time. How much will the position change? This depends on the speed, and how much time has elapsed. It turns out that a car moving at a constant speed of 25 miles per hour (36.7 feet per second) will travel 367 feet in 10 seconds. (Multiply 36.7 feet per second times 10 seconds.) But what if the speed is not constant? In this case we need to know the average speed over the specified time interval. If the speed changes at a constant rate, perhaps 5 miles-per-hour each second, then the average speed the speed at the beginning of the time interval (previousSpeed), plus the speed at the end of the time interv (currentSpeed), divided by 2: averageSpeed = (previousSpeed + currentSpeed) / 2; averageSpeed_FeetPerSecond = averageSpeed * 5280.0/3600.0; intervalFeetTraveled = averageSpeed_FeetPerSecond * timeInterval; For convenience, we can keep the timeInterval value fixed at one second. The calculations described above mention speed in "miles per hour" and distance in "feet". This is deliberate esthetic reasons: people are accustomed to thinking of the speed of a car in "miles per hour", at least in the United States. Yet, if we were to express the distance traveled over a few seconds in miles, then the numbers would be tiny fractions of a mile, and would therefore not be intuitively appropriate to the person using theprogram. This is why the averageSpeed value is converted from miles per hour (MPH) to feet per second (FPS). In general, the formula for converting miles per hour (MPH) to feet per second (FPS) is: (5280.0 FPS = MPH 3600.0) Possible Program Variables Description These variable names are a suggestion, intended to get you thinking about the problem. The implementation which you choose may be different. The contents of each variable to represent the car could be as follows: Variable Description currentSpeed Current speed of the car (miles per hour) previousSpeed Speed of the car at the end of the "previous" time interval (miles per hour) totalFeet Traveled Total feet traveled by the car (since the program started) intervalFeet Traveled Total feet traveled during the most recent time interval timeInterval The amount of elapsed time for each calculation interval (fixed at 1 second) currentState Current state of the car's motion. Valid values are: "Stopped", "Accelerating", "Cruising" (moving at a steady speed), or "Braking". delta The amount which the speed of the car will increase (during acceleration) or decrease (during braking). By default, this value should be 5 miles per hour. The "accelerate", "brake", and "cruise" operations can be calculated as shown. (In this table, the default value of "delta" is 5 MPH.) Operation Calculation accelerate previousSpeed = currentSpeed; currentSpeed = currentSpeed + delta; brake previousSpeed = currentSpeed; currentSpeed = currentSpeed - delta; cruise previousSpeed = currentSpeed; Design Note: You must write separate functions for the accelerate, brake, and cruise operations. Not only does this improve the modularity of the program, but it will also make it very easy for you to write the code for the demo operation described later in this document.Possible Function Descriptions Below is a list of possible functions, and their descriptions. Function outputStatusHeader Member Function Description outputStatus Output column headings to the console (see sample output on the next page) updateDistance Traveled Output current data values to the console. Calculate the intervalFeet Traveled (over the most recent calculation interval) and totalFeetTraveled (since the program started) using the following formula: averageSpeed = (previousSpeed + currentSpeed) / 2; averageSpeed_FeetPerSecond = averageSpeed * 5280.0/3600.0; intervalFeetTraveled = averageSpeed_FeetPerSecond * timeInterval; totalFeetTraveled = totalFeetTraveled + intervalFeetTraveled; Remember to convert Miles-per-hour to feet-per-second before calculating the distance accelerate You must write separate functions for accelerate, brake, and cruise. This will make it brake easier for you to write the code for the demo command. cruise demo Execute a predetermined sequence of the accelerate, brake, and cruise operations. (See the Sample Output section of this document for one possible sequence.) Some Thoughts about Modeling a Physical System Whenever writing code to simulate the operation of a physical system, keep in mind how the real physical system would behave and try to make your simulation match that behavior as closely as reasonably possible. In this project, this issue comes up when maintaining the "currentState" variable. For example, if the car is currently "Stopped", and the main program executes the "cruise" command, or the "brake" command, then the car should remain "Stopped". Similarly, if the car is moving and the main program executes the "brake" command, then the car should enter the "Braking" state and the speed should decrease. If the main program continues to execute the "brake" command repeatedly, then eventually the speed will reach zero. At this time, the car should transition to the "Stopped" state, and the car should remain "Stopped" even if the "brake" command continues to get executed again and again. This is how a real car would behave, so it is how our simulation should hopefully behave. (This is the reason why the State Transition Diagram on the next page has two variations of the "brake" command: "Bf' is a brake command when the initial speed is greater than the "delta" value. "Bs" is a brake command when the initial speed is less than or equal to the "delta" value. The "delta" value is the amount which the speed changes, whenever the accelerate or brake command is used. By default, "delta" should be 5 MPH.) An example of incorrect behavior would be for the car to start moving backwards (speed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started