0

I am studying methods and have been given an exercise to do. I am a bit unsure as to what to do with this particular question.

The Question we have been given is:

    Modify the above program so the conversion is done in a method.

This is the code I have so far and my problem is when I run the code I get as far as when I enter the letter and it stops.

   //Exercise 3 Brian Sheet 5

//Modify the above program so that the conversion is done in a method

import java.util.Scanner;

public class Exercise3 {

public static void main(String[] args) {
    double temp;
    String c = "c";
    String f = "f";
    String a;
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the temperature: ");
    temp = input.nextDouble();

    input = new Scanner(System.in);

    System.out
            .println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
    a = input.nextLine();
    if (a.equals(c)) {
        celsiusEq(temp);
    } else {
        Fahren(temp);
    }

}

private static double celsiusEq(double celsius) {
    double temp;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double fahrenheit) {
    double temp;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

} I don't know what I am doing wrong and it is probably something very simple. If anyone could help me, it would be graciously appreciated as I have been looking at this for the past 30 minutes!

2
  • I hope this helps you: java-made-easy.com/variable-scope.html Commented Oct 7, 2013 at 10:04
  • 3
    You're confusing returnwith System.out.println. The first one allows the caller to use what the called method returns. The second one prints to the screen. Your code calls a method, which returns a double, but does absolutely nothing with the doube value which is returned. Commented Oct 7, 2013 at 10:07

9 Answers 9

2

Here you need to interchange the temp and celsius varables to work properly

private static double celsiusEq(double celsius) {
        double temp; //here is the problem
        celsius = (temp - 32) * 5 / 9;
        return celsius;

    }

Here you need to interchange the temp and fahrenheit varables to work properly

 private static double Fahren(double fahrenheit) {
        double temp; //here is the problem
        fahrenheit = temp * 9 / 5 + 32;
        return fahrenheit;
    }

Correction here

private static double celsiusEq(double temp){
    double celsius;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double temp){
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

Update request

returntype functionname(argumenttype argument2 ,argumenttype argument2,....argumenttype argumentN  ){
// local variable declaration
variableype variable1;
variableype variable2;
----------------------
variableype variableN;
 Your calculation

   return your value based on the return type; 

}

see more details here

http://www.tutorialspoint.com/java/java_variable_types.htm

http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

Sign up to request clarification or add additional context in comments.

2 Comments

So to use a variable in a method it needs to be declared in the brackets?
That's an argument. Its a value which is passed in when the method is called.
1

This is your piece of code:

private static double celsiusEq(double temp){
    double celsius;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double temp){
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

There are several corrections that need to be done:

1.) Since you are passing temp as the parameter , why don't you simply use that in your function like this:

private static double Fahren(double temp){
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

2.) Try to make such methods public instead of private. Provide easier accessibility and manipulation.

3.)Since your function returns a double , you will need to capture the result in order to print it/modify it . Like this:

double answerInCelcius = celsiusEq(temp);  
System.out.println("Answer in Celsius is :" +answerInCelsius);

Hope it helps :)

Comments

1

You need to change your methods to

private static double celsiusEq(double temp) {
    double celsius;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double temp) {
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

Your variable names got interchanged.

Comments

1

Its pretty simple, actually. Here's the code :

Celsius To Fahrenheit

private static double celsiusToFahrenheit(double celsius)
{
   double fahrenheit;
   fahrenheit = ((celsius * 9) / 5) + 32;
   return fahrenheit;
}

Fahrenheit To Celsius

private static double fahrenheitToCelsius(double fahrenheit)
{
   double celsius;
   celsius = ((fahrenheit - 32) * 5) / 9;
   return celsius;
}

Always use brackets while performing number operations. Its a good programming habit.

Here's what was wrong with your code :

private static double celsiusEq(double celsius) 
{
    double temp; //TEMP HAS NO VALUE
    celsius = (temp - 32) * 5 / 9; //STILL YOU ARE USING IT
    return celsius;    
}

private static double Fahren(double fahrenheit) 
{
    double temp; //TEMP HAS NO VALUE
    fahrenheit = temp * 9 / 5 + 32; //STILL YOU ARE USING IT
    return fahrenheit;
}

Instead of using the celsius and fahrenheit variables, you were using the temp ones.

Comments

0

try with this following code

import java.util.Scanner;
    public class Exercise3 {

    public static void main(String[] args) {
      double temp;
      String c = "c";
      String f = "f";
      String a;
      Scanner input = new Scanner(System.in);

       System.out.println("Please enter the temperature: ");
       temp = input.nextDouble();

       input = new Scanner(System.in);

    System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
    a = input.nextLine();
    if(a.equals(c)){
        System.out.println(celsiusEq(temp));
    }else{
        Fahren(temp);
    }


    }


    private static double celsiusEq(double celsius){
    double temp = 0;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

    private static double Fahren(double fahrenheit){
    double temp = 0;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

}

ouput

Please enter the temperature: 
250
Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)
f
32.0

Comments

0

There are compile time error in your code: You can't use local variable without intinializing it. Here inside celsiusEq,Fahren methods intialize temp variable like:

double temp = 0;

Then it should work.

FYI Please read following article.

http://www.dummies.com/how-to/content/local-variables-in-java.html

Comments

0

I would do something like:

import java.util.Scanner;
public class Exercise3 {

    public static void main(String[] args) {
        double temp;
        String a;

        System.out.println("Please enter the temperature: ");
        Scanner input = new Scanner(System.in);
        temp = input.nextDouble();


        System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
        a = input.nextLine();
        switch(a)
        {
            case "c":
                temp = toCelsius(temp);
                System.out.println("Temp in Celsius: " + temp);
                break;
            case "f":
                temp = toFahren(temp);
                System.out.println("Temp in Fahren: " + temp);
                break;
            default:
                System.out.println("Invalid Entry");
        }

    }


    private static double toCelsius (double fahren){
        return (fahren - 32) * 5 / 9;
    }

    private static double toFahren(double celsius){
        return celsius * 9 / 5 + 32;
    }

}

But hey! Many other people beat me to the punch.

Comments

0
private static double celsiusEq(double temp) {
    return (temp - 32) * 5 / 9;

}

private static double Fahren(double temp) {
    return temp * 9 / 5 + 32;
}

and don't forget to print a result vlaue like System.out.println(celsiusEq(temp)); :-)

Comments

0

Your are just confusing with temp and Celsius variables in side methods. So i have changed the code and following is the updated version of your code.

public class Exercise3 {

public static void main(String[] args) {
    double temp;
    String c = "c";
    String f = "f";
    String a;
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the temperature: ");
    temp = input.nextDouble();

    input = new Scanner(System.in);

    System.out
            .println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
    a = input.nextLine();
    if (a.equals(c)) {
        celsiusEq(temp);
    } else {
        Fahren(temp);
    }

}

private static double celsiusEq(double temp ) {
    double celsius;
    celsius = (temp - 32) * 5 / 9;
            System.out.println("AFTER CONVERTION " + celsius);
    return celsius;

}

private static double Fahren(double temp ) {
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
            System.out.println("AFTER CONVERTION " + fahrenheit);
    return fahrenheit;
}
     }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.