-
Calculator
Hi my name is Moses and i'm learning Java just by reading off the internet. I came with the idea of programming a "temperature calculator" that would convert from Fahrenheit to Celsius and from Celsius to Fahrenheit. From what i have read you need a class and within the class you need methods and you can have different methods if you want, so i decided to make a method for each calculation. (i don't know if this is the best way to do this)
So my question is how can i call the methods so that the user can input a value and then give me the answer?
here is what i have so far. ( I'm using Netbeans. I decided to use this one after reading the IDE thread in this forum)
Code:
package temperature;
/* i imported the scanner so that the user could input a value
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner temp = new Scanner(System.in); /*Created the scanner
System.out.println("Enter a value in Fahrenheit");
}
public void fahr(){
double f = 0;
double c = 0;
f = c * 9/5 + 32;
}
public void celsius(){
double f = 0;
double c = 0;
c= (f - 32) * 5/9;
}
}
Thanks in advanced
-
Hi - you may want to read into the Scanner class in the Java API: Scanner (Java 2 Platform SE 5.0)
You have already created a scanner object. In order to get input from user, you can use temp.nextDouble() . This reads the next token of input from the scanner as a double. Store that value into a double variable, compute the conversion, and simply display the result to console by using a println statement. If you are using an IDE (like Netbeans for example), type the object name and press the dot operator '.' and see for yourself all the methods that can be invoked on that object. Good way to discover/learn new methods.
Most importantly, always have the Java API link open, and don't be afraid to go in there and look up Scanner for example. Its a valuable tool, especially when starting to learn.
Best,
-
thank you so much User0 :)
-
You also need to be able to pass a value to your fahr() and celsius() methods, and to have each method return a value. As it is now, all they do is calculate the equivalent of 0 degrees in the opposite scale and then forget about the result when the variables go out of scope.
-
What exactly do you mean by pass the value to the method? this where i get a little lost.
-
Quote:
Originally Posted by
Moshe22
Code:
public void fahr(){
double f = 0;
double c = 0;
f = c * 9/5 + 32;
}
public void celsius(){
double f = 0;
double c = 0;
c= (f - 32) * 5/9;
}
}
What he means is - you have written two methods that calculate Celsius and Fahrenheit but they do not take in any parameters to do so. Say for example, the user entered a value in Fahrenheit and wants it converted to Celsius. Then your Celsius method header would need to change as follows:
Code:
public static void celsius(double f){
double c = 0;
c = (f - 32) * 5/9;
System.out.println("Celsius value = " + c);
}
Here, you are passing the parameter f which is of type double, to the celsius method. The method uses this value of f in the calculation. Then, to invoke the method, you will need to do the following in your main method:
If you want the celcius method to return a value, you need to change the header to the following:
Code:
public static [B]double [/B]celsius(double f){
double c = 0;
c = (f - 32) * 5/9;
[B]return c;[/B] // This returns the celsius value to whoever called this method
}
I suggest you read more on parameter passing if you have not already done so.
Best,
-
He means as parameters. For example in this pseudocode:
Code:
int i = read int from user
print fahr(i)
...
public void fahr(int intFromUser){
//Perform conversion with intFromUser which was 'passed in' as a parameter
}
Does that help? Do you understand how parameters and return values work?
-
Thanks to both of you user0 and Quad64bit. I haven't read about parameters yet and value. I will need to read about it. Any website you guys would suggest?
-