Results 1 to 9 of 9

Thread: Calculator

  1. #1
    Moshe22 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default 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)

    Java 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

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    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--

  3. #3
    Moshe22 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    thank you so much User0 :)

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    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.

  5. #5
    Moshe22 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    What exactly do you mean by pass the value to the method? this where i get a little lost.

  6. #6
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Moshe22 View Post

    Java 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:
    Java 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:
    Java Code:
    celcius(23.6);
    If you want the celcius method to return a value, you need to change the header to the following:

    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
       }
    I suggest you read more on parameter passing if you have not already done so.
    Best,
    --user0--

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    He means as parameters. For example in this pseudocode:
    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
    }
    Does that help? Do you understand how parameters and return values work?

  8. #8
    Moshe22 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    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?

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

Similar Threads

  1. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 08:21 PM
  2. need help with my calculator
    By semoche in forum AWT / Swing
    Replies: 6
    Last Post: 12-04-2009, 10:16 PM
  3. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 06:00 AM
  4. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •