Why is this equation not working? it's a really simple equation too...

Started by
4 comments, last by fadilthrejk 20 years, 6 months ago
i'm workin on a simple temperature converter in java, using multiple classes. i seem to be havin some trouble gettin it to output the right value. here's the code for the method that does the conversion:

    public double fahrenheitToCelsius()
    {
        double answer = (number - 32);
        answer *= 5/9;
        return answer;
    }
  
here's the code for the other class. keyboard is another class that just simplifies getting input from the terminal:

System.out.print("Enter a number: "); // prompts the user for a number        
        double myNumber = Keyboard.readDouble();// reads the number from the keyboard
                                                // using the readDouble() method of
                                                // the Keyboard class

        System.out.println(myNumber + " degrees fahrenheit is " + thisMyClass.fahrenheitToCelsius() 
                            + " degrees celsius");     
EDIT: Whoops...forgot to include the problem. No matter what value is inputted, i get 0.0 or -0.0 as the result. [edited by - fadilthrejk on October 13, 2003 8:32:21 AM]
Advertisement

    public double fahrenheitToCelsius()    {        double answer = (number - 32.0);        answer *= 5.0/9.0;        return answer;    }  


you are doing int division... add .0 to numbers
damn i feel really stupid right now...
public double fahrenheitToCelsius()
{
double answer = (myNumber - 32);
answer *= 5/9;
return answer;
}

seems you switch your variables around some (myNumber and number)???

-------

quote:Original post by fadilthrejk
    public double fahrenheitToCelsius()    {        double answer = (number - 32);        answer *= 5/9;        return answer;    }   

System.out.print("Enter a number: "; // prompts the user for a number                double myNumber = Keyboard.readDouble();// reads the number from the keyboard                                                // using the readDouble() method of                                                // the Keyboard class        System.out.println(myNumber + " degrees fahrenheit is " + thisMyClass.fahrenheitToCelsius()                             + " degrees celsius";      


<SPAN CLASS=editedby>[edited by - fadilthrejk on October 13, 2003 8:32:21 AM]</SPAN>


for this to work, "number" must be a member of the same object that you''re using to call fahrenheitToCelsius(). Assigning a value to "myNumber" won''t ever work because (1) it''s not called "number", and (2) myNumber is local, so fahrenheitToCelsius() will never ever be able to see it.

Here are 2 ways you can make it work. You can make sure that there is a public double in your class called "number":

System.out.print("Enter a number: "; // prompts the user for a number                double myNumber = Keyboard.readDouble();// reads the number from the keyboard        thisMyClass.number = myNumber;                                                // using the readDouble() method of                                                // the Keyboard class        System.out.println(myNumber + " degrees fahrenheit is " + thisMyClass.fahrenheitToCelsius()                             + " degrees celsius";      


or you could to it the better way and make fahrenheitToCelsius() work using a parameter:

    public double fahrenheitToCelsius(double number)    {        double answer = (number - 32);        answer *= 5/9;        return answer;    }   


so that you can later say:
System.out.println(myNumber + " degrees fahrenheit is " + thisMyClass.fahrenheitToCelsius(myNumber) + " degrees celsius";
"number" is just the name of the paramater in the class constructor. i assign myNumber to number. it works...i had it working after the first post.

This topic is closed to new replies.

Advertisement