Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

fadilthrejk

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

This topic is 5395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
public double fahrenheitToCelsius()
{
double answer = (myNumber - 32);
answer *= 5/9;
return answer;
}

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

-------

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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";

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!