Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Fahrenheit to Celcius in C# with methods

Tags:

methods

c#

Hello guys I am a newbie and I am currently a first year in Computer Science. We have been given an exercise in which we have to convert from Fahrenheit to Celcius with methods (professor advised no static). We have started with C#. Here is my code.

namespace Week3_Exe3._1{
public class Formula
{
    public double F;
    public double C;

    public void calculate(double F, double C)
    {
        C = (5 / 9) * (F - 32);

    }

    public static void Main(string[] args)
    {

    Console.WriteLine("Please enter the Fahrenheit you wish to convert");
        F = double.Parse(Console.ReadLine());

        Formula form = new Formula();

        Console.WriteLine(F + "Fahrenheit correspond to " + form.calculate() + "Degrees celcius");
    }
}
}

I am currently working with Visual Studio Community 2015 and in form.calculate, it reds out the calculate with the error

CS7036 C# There is no argument given that corresponds to the required formal parameter '' of 'Formula.calculate(double, double)'

I searched for it but I still do not understand what is missing. I created an instance to use, but it's not working. Can anyone give me answer?

like image 749
SoftDev30_15 Avatar asked Nov 16 '15 11:11

SoftDev30_15


3 Answers

Your calculate method expect 2 parameters but you try to call it without parameters. In my humble opinion, it shouldn't take two parameters at all. Just one fahrenheit parameter is enough so it can calculate celsius value.

Also 5 / 9 performs an integer division —it always discards the fractional part— so it will always return 0.

A static method should be enough for your case;

static double Celsius(double f)
{
    return 5.0/9.0 * (f - 32);  
}
like image 160
Soner Gönül Avatar answered Oct 05 '22 22:10

Soner Gönül


There a few errors in your code. First of all, your function only requires one input parameter, the temperature in Fahrenheit. After you have resolved this, you will find temperature of 100 Fahrenheit will return a temperature of 0 Celcius and this is obviously not correct. You need to modify your equation to use at least one fractional part otherwise C# will implicitly cast the values to integers.

using System;

namespace Week3_Exe3._1
{
    public class Formula
    {
        public double calculate(double F)
        {
            return (5.0 / 9.0) * (F - 32.0);
        }

        public static void Main(string[] args)
        {
            Console.WriteLine("Please enter the Fahrenheit you wish to convert");
            var F = double.Parse(Console.ReadLine());
            Formula form = new Formula();
            Console.WriteLine(F + " Fahrenheit correspond to " + form.calculate(F) + " Degrees celcius");
        }
    }
}
like image 42
Alex Avatar answered Oct 05 '22 22:10

Alex


Your code has a few holes, my suggested rewrite is this:

public class Formula
{
    public double F;
    public double C;

    public void calculate()
    {
        C = (5.0 / 9.0) * (F - 32);
    }

    public static void Main(string[] args)
    {
        Console.WriteLine("Please enter the Fahrenheit you wish to convert");
        Formula form = new Formula();
        form.F = double.Parse(Console.ReadLine());
        form.calculate();
        Console.WriteLine(F + "Fahrenheit correspond to " + form.C + "Degrees celcius");
    }
}

keep in mind that this code only works for F => C, not the other way around.

like image 27
Denis Kralj Avatar answered Oct 05 '22 23:10

Denis Kralj