Question

Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...

Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a C# app that will input the miles driven and gallons used (both as integers) for each tankful. The app should calculate and display the miles per gallon obtained for each tankful and display the combined miles per gallon obtained for all tankfuls up to this point. All averaging calculations should produce floating-point results. Display the results rounded to the nearest hundredth. Use the Console class’s ReadLine method and sentinel-controlled repetition to obtain the data from the user.

Write the code where the Bold comments are.

The code from Gas.cs is:

/ Exercise 5.17 Solution: Gas.cs
// Application calculates average mpg
using System;

public class Gas
{
   // perform miles per gallon calculations
   public static void Main( string[] args )
   {
      int miles; // miles for one tankful
      int gallons; // gallons for one tankful
      int totalMiles = 0; // total miles for trip
      int totalGallons = 0; // total gallons for trip

      double milesPerGallon; // miles per gallon for tankful
      double totalMilesPerGallon; // miles per gallon for trip

      // prompt user for miles and obtain the input from user

      // exit if the input is -1 otherwise, proceed with the program
      while ( miles != -1 )
      {
         // prompt user for gallons and obtain the input from user
         Console.Write( "Enter gallons: " );
         gallons = Convert.ToInt32( Console.ReadLine() );

         // add gallons and miles for this tank to totals

         // calculate miles per gallon for the current tank
         if ( gallons != 0 )
         {
            milesPerGallon = ( double ) miles / gallons;
            Console.WriteLine( "MPG this tankful: {0:F}",
               milesPerGallon );
         } // end if statement

         if ( totalGallons != 0 )
         {
                     // calculate miles per gallon for the total trip

         } // end if statement

         // prompt user for new value for miles
         Console.Write( "Enter miles (-1 to quit): " );
         miles = Convert.ToInt32( Console.ReadLine() );
      } // end while loop      
   } // end Main
} // end class Gas

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

code:

using System;

namespace GasOfMiles
{
    public class Gas
    {
        static void Main(string[] args)
        {
            int miles; // miles for one tankful
            int gallons; // gallons for one tankful
            int totalMiles = 0; // total miles for trip
            int totalGallons = 0; // total gallons for trip

            double milesPerGallon; // miles per gallon for tankful
            double totalMilesPerGallon; // miles per gallon for trip

            // prompt user for miles and obtain the input from user
            Console.Write("Enter miles (-1 to quit): ");
            miles = Convert.ToInt32(Console.ReadLine());
            // exit if the input is -1 otherwise, proceed with the program
            while (miles != -1)
            {
                // prompt user for gallons and obtain the input from user
                Console.Write("Enter gallons: ");
                gallons = Convert.ToInt32(Console.ReadLine());

                // add gallons and miles for this tank to totals
                totalMiles += miles;
                totalGallons += gallons;

                // calculate miles per gallon for the current tank
                if (gallons != 0)
                {
                    milesPerGallon = (double)miles / gallons;
                    Console.WriteLine("MPG this tankful: {0:F}",
                       milesPerGallon);
                } // end if statement

                if (totalGallons != 0)
                {
                    // calculate miles per gallon for the total trip
                    totalMilesPerGallon = (double)totalMiles / totalGallons;
                    Console.WriteLine("Total MPG: {0:F}\n", totalMilesPerGallon);

                } // end if statement

                // prompt user for new value for miles
                Console.Write("Enter miles (-1 to quit): ");
                miles = Convert.ToInt32(Console.ReadLine());
            } // end while loop
            Console.ReadKey();
        }
    }
}

sample output:

Add a comment
Know the answer?
Add Answer to:
Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...
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
  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls....

  • 7.11 Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track...

    7.11 Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording the number of miles driven and the number of gallons used for each tankful. Develop a script that will take as input the miles driven and gallons used (both as integers) for each tankful. The script should calculate and output HTML5 text that displays the number of miles per gallon obtained for each tankful and the combined...

  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tanks of gasoline by recording miles driven and gallons used for each tank. Develop a program using a while loop that will input the miles driven and the gallons used for each tank. The program should calculate and display the miles per gallon obtained for each tank. After processing all input information, the program should calculate and print the overall miles per gallon...

  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that inputs the miles driven and gallons us

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that inputs the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point.

  • Use a java program that does the following: . (10 points) Write a program as follows...

    Use a java program that does the following: . (10 points) Write a program as follows a. Prompt the user to input two positive integers: nl and n2 (nl should be less than n2) b. If the user enters the negative number(s), convert it/them to positive number(s) c. If nl is greater than n2, swap them. d. Use a while loop to output all the even numbers between nl and n2 e. Use a while loop to output the sum...

  • create an application in VISUAL BASIC that calculates a cars gas mileage. The formula for calculating...

    create an application in VISUAL BASIC that calculates a cars gas mileage. The formula for calculating the miles that a car can travel per gallon of gas is MPG = Miles/Gallon In the formula MPG is miles-per-gallon, miles is the number of miles that can be driven on a full tank of gas, and gallons is the number of gallons that the tank holds. The applications form should have TextBox controls that let the user enter the number of gallons...

  • // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter...

    // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter your class section, and time /* The program TripCost.cpp uses pointers to calculate    the cost of a trip depending on the cost of a gallon    of gas, the number of miles, and the number of miles    per gallon a car gets. */ /* The cost of gas, the number of miles, and number of    miles per gallon is entered. */...

  • Compute for Miles Per Gallon in C++

    Make a program that will calculate and compute for the quotient of miles and gallons (mpg: miles per gallons). Your program should must ask the user to specify the size of the array (using dynamic array) for the following variable: miles ,gallons and mpg. Prompt the user to Initialize  the value of miles (value for miles should be 100-250) and gallons (values should be from 5-25). Use pointer galPtr for gallons, milPtr for miles and mpgPtr for mpg.  Use function...

  • A liter is 0.264179 gallons. Write a program that will read in the number of liters...

    A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon. After doing this... Modify your...

  • Write a Python program that does the following: Obtains the following input from a user: Mileage...

    Write a Python program that does the following: Obtains the following input from a user: Mileage at beginning of measurement period Mileage at end of measurement period Gallons of fuel consumed Calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Incorporate some Selection structure logic into it: If a vehicle gets less than 15 miles per gallon, display the message:       "Your vehicle has criminally low fuel efficiency." If it gets 15...

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