Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Looking for help with this C# program from expert Create a Trip class. Include a

ID: 3707052 • Letter: L

Question

Looking for help with this C# program from expert

Create a Trip class. Include as data members destination, distance traveled, total cost of gasoline, and number of gallons consumed. Include appropriate constructors and properties. Add additional methods that calculates miles per gallon and the cost per mile. Override the ToString ( ) method. Create a second class to test your Trip class. In order to aid your development, I will list the declaration of the data members here:

private string destination;

private double distanceTraveled;

private double fuelCostPerGallon;

private double gallonsConsumed;

Include appropriate constructors and properties.

Add a custom method to calculate the mileage per gallon, a second method to calculate cost per mile, and a third method to calculate total fuel cost.Again, I will list the method prototypes here, but you need to implement those methods.

       public double CalculateMilesPerGallon()

        {

            // Implement your code here

        }

        public double CalculateCostPerMile()

        {

// Implement your code here

        }

        public double CalculateTotalFuelCost()

        {

// Implement your code here

        }

Sample output attached

  

Explanation / Answer

// Code

using System.IO;
using System;

class Trip
{
    //class variables
    private string destination;
    private int distanceTraveled;
    private double fuelCostPerGallon;
    private int gallonsConsumed;

    //constructors
    public Trip()
    {
        destination = "n.a.";
        distanceTraveled = 1;
        fuelCostPerGallon = 1;
        gallonsConsumed = 1;
    }

    public Trip(string destinationValue, int distanceValue,
                double costOfGasValue, int gallonsValue)
    {
        Destination = destinationValue;
        Distance = distanceValue;
        CostOfGas = costOfGasValue;
        Gallons = gallonsValue;
    }

    //properties
    public string Destination
    {
        get { return destination; }
        set { destination = value; }
    }

    public int Distance
    {
        get { return distanceTraveled; }
        set { distanceTraveled = value; }
    }

    public double CostOfGas
    {
        get { return fuelCostPerGallon; }
        set { fuelCostPerGallon = value; }
    }

    public int Gallons
    {
        get { return gallonsConsumed; }
        set { gallonsConsumed = value; }
    }

    public double CalculateMilesPerGallon()
    {
        return distanceTraveled / gallonsConsumed;
    }

    public double CalculateCostPerMile()
    {
        return fuelCostPerGallon * CalculateMilesPerGallon();
    }
  
    public double CalculateTotalFuelCost()
    {
        return gallonsConsumed * fuelCostPerGallon;
    }

    override
        public string ToString()
    {
        return "Trip["
            + destination + ", "
            + distanceTraveled + ", "
            + fuelCostPerGallon + ", "
            + gallonsConsumed + "]"
            + " Miles Per Gallon = " + CalculateMilesPerGallon()
            + ", Cost Per Mile = " + CalculateCostPerMile()
            + ", Total Fuel Cost = " + CalculateTotalFuelCost();
    }
}

class Program
{
    static void Main(string[] args)
    {
        Trip t1 = new Trip("Columbus, OH", 150, 2.95, 5);
        Trip t2 = new Trip("Edmonton, AB", 3300, 2.75, 50);
        Trip t3 = new Trip("Calgary, AB", 3100, 2.50, 45);

        Console.WriteLine(t1);
        Console.WriteLine(t2);
        Console.WriteLine(t3);
    }
}

// Output

############### Please ask me if any queries. Thank you. #################

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote