Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java programming help?

I'm very new to this stuff and am having some trouble with the homework. This code is supposed to convert Celsius to Fahrenheit and vice versa. I'm supposed to write a method that:

1. Has the method modifiers public static

2. Has the name convertTemp

3. Receives two parameters: the first is of type double, which you should call temperature, and the second is of type String, which you should call temperatureScale

4. The return of the method should be void

5. The body of the method should have an if, else-if, else statement. Depending on the value of the argument temperatureScale, the method should convert the value saved in the variable temperature from Fahrenheit to Celsius if temperatureScale is f, or convert temperature from Celsius to Fahrenheit if temperatureScale is c. If temperatureScale is neither f nor c, the method should print an error message. The skeleton of the if statement is the following:

if ( /* check if temperature scale is equal to the letter f */ ){

// code that converts from Fahrenheit to Celsius

// and prints the result to the screen

} else if (/* check if temperature scale is equal to the letter c */){

// code that converts from Celsius to Fahrenheit

// and prints the result to the screen

} else {

// code that outputs a message indicating that an incorrect

// option was selected

}

Declare a variable to hold the calculation result, and use the System.outprintln method to print to the screen the output of the calculation.

3 Answers

Relevance
  • 3 years ago

    In Java, unlike in C/C++ you cannot have a method belonging to no class.

    "But what if I want my method to be independent of the Object it belongs to?"

    That's what static methods are for. Within a static method you have no access to the object's fields (except for those that are static as well).

    So your task is to have a C-like method that converts temperatures.

    class Converter{

    public static void convertTemp (double temp, String temperatureScale)

    {

    //your text specifically says to use if/if-else/else, but this would be better implemented using a switch-case (check it out)

    //also, you could do the computation within the print function, but the text says otherwise.

    double result;

    if(temperatureScale == "f")

    {

    result = temp * 1.4 + 32.0;

    System.out.println(result);

    }

    //do this for the other cases and you're done.

    }

    };

    "Ok, but how do I call a static function?"

    Since static functions belong to no object, it is pointless to instantiate one in the first place. This is how you use them:

    class Source{

    public static void main(String [] args)

    {

    Converter.convertTemp(32, "f");

    }

    }

  • 3 years ago

    lol it is hard but you try and try

  • 3 years ago

    You can look up conversion formulas for C-to-F and F-to-C conversion, but they're really very simple. A Celsius degree is 1.4 (=9/5) times bigger than a Fahrenheit degree, and 0 C = 32 F is the freezing temperature of water. So, the conversions in Java/C/C++/C# form are:

    celsius = (fahrenheit - 32.0) / 1.4;

    fahrenheit = celsius * 1.4 + 32.0;

Still have questions? Get your answers by asking now.