Thread: help...calculator and C# displaying two different answers

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    help...calculator and C# displaying two different answers

    ok i have the program written it is homework. but when i plug in the numbers in a calculator and what i see on my computer screen are two different things.

    Code:
        public double SetTempWithF(double degrees)
            {
               tempk = (degrees + 459.67) * (5/9);
              // tempk = degrees;
               Console.WriteLine("settemwithf {0}", degrees);
                return tempk;
            }
    degrees is getting a fixed number of 32 being passed in.

    Code:
            const double TEST_VALUE1 = 32.0;
            const double TEST_VALUE2 = 100.0;
    
            static void Main()
            {
                // Create a Temperature object
                Temperature todaysTemp = new Temperature();
    
                // Set its value to 32⁰ Fahrenheit
                todaysTemp.SetTempWithF(TEST_VALUE1);
    the calculator displays 273.15 and the program displays -273.15

    i have another method in the class that has a similar problem calculator displays 212 code displays - 86.52

    since it isnt too long i am just going to post the whole thing. in two blocks one for each file

    the part with main is pre provided and cannot be edited. so the only part that can be editted is the class that i wrote. so ya no idea why the math is not displaying correctly any help would be appreciated in pin pointing what i may of done wrong.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace temperature
    {
        class Temperature
        {
            private double tempk;
    
            public Temperature()
            {
              tempk = 0.0;
            }
            public double SetTempWithF(double degrees)
            {
               tempk = (degrees + 459.67) * (5/9);
              // tempk = degrees;
               Console.WriteLine("settemwithf {0}", degrees);
                return tempk;
            }
            public double SetTempWithC(double degrees)
            {
                tempk = degrees + 273.15;
                Console.WriteLine("settempwithc {0}", tempk);
              //  tempk = degrees;
                return tempk;
            }
            public double GetTempF()
            {
                tempk = (tempk * (9/5)) - 459.67;
                Console.WriteLine("GetTempF {0}",tempk);
                return tempk;
            }
            public double GetTempC()
            {
                tempk = tempk - 273.15;
                Console.WriteLine("gettempC {0}", tempk);
                return tempk;
            }
        }
    }
    Code:
    using System;
    //using namespace temperature2;
    
    namespace temperature
    {
        class Program
        {
            const double TEST_VALUE1 = 32.0;
            const double TEST_VALUE2 = 100.0;
    
            static void Main()
            {
                // Create a Temperature object
                Temperature todaysTemp = new Temperature();
    
                // Set its value to 32⁰ Fahrenheit
                todaysTemp.SetTempWithF(TEST_VALUE1);
    
                // get and display the temperature in Celsius
                Console.WriteLine("Today's Temperature is {0}⁰ Celsius", todaysTemp.GetTempC());
    
                // Test it the other way around
                todaysTemp.SetTempWithC(TEST_VALUE2);
    
                // get and display the temperature in Fahrenheit
                Console.WriteLine("Today's Temperature is {0}⁰ Fahrenheit", todaysTemp.GetTempF());
    
                Console.ReadLine();
            }//End Main()
        } // end of Program class
    } // end of namespace
    hooch

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    never mind i figured it out thanks. appears i grabbed the wrong formulas off of google.
    hooch

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok scratch that lol why is 5/9 = 0????

    edit:

    oh i see. nevermind sorry im new to C# and didnt realize you had to put 5.0/9.0 which = .5555555
    Last edited by ssjnamek; 01-20-2011 at 12:57 AM.
    hooch

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You should not overwrite your private variable in a getter. Call a getter 2 times and the temperature will change. That's probably not what you want.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Elaborating on the above point, you have degrees in Kalvin and you switch them from Celsius to Fahrenheit and vice verta. The thing is that you don't know a given time if tempK is actually C or F.

    For Object Oriented Programming, this somehow is a flaw, because if you gave me the class Temperature and I only new the methods and not their exact implementation I would get confused.

    The "Get" functions should be more like "Transform". You could have a Transform function and then a Get which will just display and return the value.

    Most importantly you should have bool variables indicating what type tempK is. Then if you call "TransformC2F" and it is already C it will not do anything. So you know at any given time that "TransforC2F" will always give you C, no matter how many times you call it.

    So, if in 10 years you want a class to be able to calculated Celsius to Fahrenheit, you just copy past the above one not needing to read it again and see how it works. That is one of the main point. A more practical example is that if you were to program for hours, you would not remember even yourself exactly how you implemented a method. Or if you were working with another programmer he would use your code, again, without knowing exactly all the details.

    I know you are a beginner, so thought to elaborate in order to avoid bad programming tactics

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by C_ntua View Post
    Most importantly you should have bool variables indicating what type tempK is.
    Ick, I don't like that idea.

    I'm a firm believer that internally, you should store a variable in one and only one format unless absolutely necessary to do otherwise. So tempK should always and forever be temperature in Kelvin. Any necessary transforms should happen on get/set, which I think was the intent except for the fact the code overwrites the member variable instead of doing the calculations into a new variable.

    The only time I could really justify the other method would be if a round-trip conversion was too lossy.
    Last edited by Cat; 01-21-2011 at 11:29 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yes, Cat's method is much better
    Well, my method is actually stupid to be honest...

    You would lose in precision with my method in a round-trip conversion so you should not unnecessarily save the value of the conversion.

    If the computations to convert were really complex, the obvious optimization would be to save Celsius and Fahrenheit (for example) the first time you call Get or Set in some variables (like tempC and tempF). Then set a bool and return the variable if user calls again Get or Set so you avoid repeating caluclations. If tempK is altered, the bool variable will go back to initial value and calculations would be needed again.

Popular pages Recent additions subscribe to a feed