116k views
2 votes
Create a program named MealPriceCalculation that prompts users enter a meal price and the tip and then calculate and display the total meal price. The program should have a Main () and then have two overloaded methods of the same name CalDisplayPrice to calculate the total meal price, which will be displayed from the Main().

In the Main(), first accept a meal price (double) and a tip amount in dollars (double), and call the method to calculate total meal price. Then accept a meal price (double) and a tip in percentage (int) (e.g., 10 for 10%), and call the overloaded method to calculate the meal price.

1 Answer

3 votes

Final answer:

The MealPriceCalculation program prompts the user for a meal price and tip amount, then uses overloaded methods to calculate and display the total meal cost.

Step-by-step explanation:

The student is working on creating a program called MealPriceCalculation to calculate and display the total meal price. The Main() method should collect meal price and tip information from the user, then use two overloaded CalDisplayPrice methods to perform the calculations based on whether the tip is a dollar amount or a percentage.

In the first scenario, the method will accept a meal price (double) and a tip amount in dollars (double). After calculating the total, the result will be displayed from within Main(). In the second scenario, it will accept a meal price (double) and a tip in percentage (int), and then perform the appropriate calculations and display the result.

The key parts of the program include input collection, performing correct mathematical operations, and making use of method overloading to handle different types of tip inputs. Here is an example of what these methods might look like in C#:

public static void Main(string[] args)
{
Console.WriteLine("Enter meal price:");
double mealPrice = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter tip amount in dollars:");
double tipDollars = Convert.ToDouble(Console.ReadLine());
CalDisplayPrice(mealPrice, tipDollars);

Console.WriteLine("Enter tip percentage:");
int tipPercentage = Convert.ToInt32(Console.ReadLine());
CalDisplayPrice(mealPrice, tipPercentage);
}

public static void CalDisplayPrice(double mealPrice, double tipAmount)
{
double total = mealPrice + tipAmount;
Console.WriteLine("Total meal price with dollar tip: " + total);
}

public static void CalDisplayPrice(double mealPrice, int tipPercentage)
{
double tipAmount = mealPrice * tipPercentage / 100;
double total = mealPrice + tipAmount;
Console.WriteLine("Total meal price with percentage tip: " + total);
}

User ShreeshaDas
by
8.5k points