Create a console-based application named
TaxCalculation. Include two overloaded methods—one
that accepts a price and a tax rate expressed as doubles
(for example, 79.95 and 0.06, where 0.06 represents 6%),
and one that accepts a price as a double and a tax rate as
an integer (for example, 79.95 and 6, where 6 also represents
6%). Include a Main() method that demonstrates
each method calculates the same tax amount appropriately.
So , if I input first number as double and second as int it works fine, but when i put two doubles then it gives me an error, I know it because of the code i highlighted in yellow, but if I use ref parameters in my methods then for some reason only 1st method does all the math, and second method never does anything even if my first input is double and second is int....
double price=0;
int tax=0;
calcTax(price, tax);
}
public static void calcTax( double price, double tax)
{
Console.WriteLine("Please enter price of the item");
double itemPrice = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please now enter tax");
double itemPriceTax = Convert.ToDouble(Console.ReadLine());
double totalPrice;
totalPrice = itemPrice * itemPriceTax/100;
Console.WriteLine("Your item cost {0:C}, the1 tax for this item is {1:C}", itemPrice, totalPrice);
}
public static void calcTax( double price, int tax)
{
Console.WriteLine("Please enter price of the item");
double itemPrice = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please now enter tax");
int itemPriceTax = Convert.ToInt32(Console.ReadLine());
double totalPrice;
totalPrice = itemPrice * itemPriceTax/100;
Console.WriteLine("Your item cost {0:C}, the2 tax for this item is {1:C}", itemPrice, totalPrice);
Loading
VulpesPosted Jan 16, 2012, 5:17 AM
http://www.c-sharpcorner.com/Forums/Thread/157460/method-overload.aspx
http://www.c-sharpcorner.com/Forums/Thread/157458/another-problem-with-methods.aspx
I'd suggest:
Datta KharadPosted Jan 16, 2012, 1:31 AM
I have tried your example and check it is proper or not.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
double price = 70;
double tax = 5;
//int tax = 6;
double totalPrice;
calcTax(ref price, tax, out totalPrice);
Console.WriteLine("Your item cost {0:C}, the1 tax for this item is {1:C}", price, totalPrice);
Console.ReadKey();
}
public static void calcTax(ref double price,double tax,out double totalPrice)
{
double ItemPrice;
double iTax;
Console.WriteLine("Please enter price of the item");
ItemPrice = Convert.ToDouble(Console.ReadLine());
price = (ItemPrice != 0.0 ? ItemPrice : price);
Console.WriteLine("Please now enter tax");
iTax = Convert.ToDouble(Console.ReadLine());
tax = (iTax != 0.0 ? iTax : tax);
totalPrice = price * tax / 100;
}
public static void calcTax(ref double price, int tax, out double totalPrice)
{
double ItemPrice;
int iTax;
Console.WriteLine("Please enter price of the item");
ItemPrice = Convert.ToDouble(Console.ReadLine());
price = (ItemPrice != 0.0 ? ItemPrice : price);
Console.WriteLine("Please now enter tax");
iTax = Convert.ToInt32(Console.ReadLine());
tax = (iTax != 0 ? iTax : tax);
totalPrice = price * tax / 100;
}
}
}
Prime bPosted Jan 16, 2012, 12:32 AM
Jignesh TrivediPosted Jan 15, 2012, 11:37 PM
try following code.
static void Main(string[] args)
{
double price = 0;
double tax = 0;
calcTax(price, tax);
}
public static void calcTax(double price, double tax)
{
}
public static void calcTax(double price, int tax)
{
}
hope this help.