Question: Subject: C# .NET Create a Trip class . Include as data members destination, distance traveled, total cost of gasoline, and number of gallons consumed. Include
Subject: C# .NET
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
This is what I have so far... I'm not sure if I'm using the appropriate constructors and properties per the instructions. Thanks.
class Program { static void Main(string[] args) { double totalMiles, totalGallons, pricePerGallon; string destination
getInput(out destination, out totalMiles, out totalGallons, out pricePerGallon);
// Calculate results double milesPerGal, costPerMile, totalTripCost;
milesPerGal = calculateMpg(totalMiles, totalGallons);
totalTripCost = calculateTrip(pricePerGallon, totalGallons);
costPerMile = calculateCost(totalTripCost, totalMiles);
// Print results printOutput(milesPerGal, costPerMile); Console.ReadLine(); }
//calculate miles per gallon, cost per mile, and total gas cost static double calculateMpg(double totalMiles, double totalGallons) { double milesPerGal; milesPerGal = totalMiles / totalGallons; return milesPerGal; }
static double calculateCost(double totalTripCost, double totalMiles) { double costPerMile; costPerMile = totalTripCost / totalMiles; return costPerMile; }
static double calculateTrip(double pricePerGallon, double totalGallons) { double costPerMile; costPerMile = pricePerGallon * totalGallons; return costPerMile; }
//accept input from user static void getInput(out string dest, out double miles, out double gallons, out double unitPrice)
{ Console.Write("Enter destination: "); destination = (Console.ReadLine());
Console.Write("Enter total miles traveled: "); miles = double.Parse(Console.ReadLine());
Console.Write("Enter number of gallons of gas consumed: "); gallons = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the gas price per gallon: "); unitPrice = double.Parse(Console.ReadLine()); }
//print the calculated values static void printOutput(double mpg, double cpm)
{ Console.WriteLine();
Console.WriteLine("Miles per Gallon: {0:F2}", mpg); Console.WriteLine("Cost per mile: {0:C}", cpm); Console.Read(); } } }
Step by Step Solution
There are 3 Steps involved in it
To create a Trip class with the needed functionality its essential to utilize objectoriented principles such as encapsulation and object initializatio... View full answer
Get step-by-step solutions from verified subject matter experts
