Results 1 to 9 of 9
Thread: Calculator
- 01-16-2011, 04:36 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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)
Thanks in advancedJava 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; } }
- 01-16-2011, 06:44 AM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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,--user0--
- 01-16-2011, 02:07 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
thank you so much User0 :)
- 01-16-2011, 03:06 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 01-17-2011, 04:47 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
What exactly do you mean by pass the value to the method? this where i get a little lost.
- 01-17-2011, 05:09 AM #6
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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:
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:Java Code:public static void celsius(double f){ double c = 0; c = (f - 32) * 5/9; System.out.println("Celsius value = " + c); }
If you want the celcius method to return a value, you need to change the header to the following:Java Code:celcius(23.6);
I suggest you read more on parameter passing if you have not already done so.Java 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 }
Best,--user0--
- 01-17-2011, 05:10 AM #7
He means as parameters. For example in this pseudocode:
Does that help? Do you understand how parameters and return values work?Java 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 }
- 01-17-2011, 05:18 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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?
- 01-17-2011, 05:29 AM #9
The official tutorials cover most of this, here is a link:
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Good luck!
Similar Threads
-
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
need help with my calculator
By semoche in forum AWT / SwingReplies: 6Last Post: 12-04-2009, 10:16 PM -
Calculator
By water in forum AWT / SwingReplies: 4Last Post: 09-23-2009, 06:00 AM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks