Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Small Java program help

    Hey everyone,

    Looking for a little help on writing a temperature conversion program that converts from Fahrenheit to Celcius. I should be able to enter the Fahrenheit temperature from the keyboard (assuming I use a Text Field). Then using a TextField it should display the converted temperature. Of course using the following formula for conversion:

    Celcius = 5 / 9 * (Fahrenheit - 32)


    Thanks for any insight on this.

    Munch

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    What kind of help are you looking for? There's not really much to it. Take the input and insert it into the forumla and then output the results. If you are going to create a GUI for this then do that last. First write a class for conversion containng say two methods called computeFahrenheit and another called computeCelsius and pass the appropriate parameters to them and return the results. Once you have that done then create the GUI and apply your conversion class to it.

    This sounds like a homework assignment so of course we are not going to write any code for you. You need to do that.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    remember Java basics

    file temp.java

    Code:
    class temp {
       public static void main(string[args]) {
          //read input and process and return
          //the rest is up to you...
       }
    }
    Code -> compile -> run

    Or, if you program like I do...

    code -> compile -> swear -> code -> compile -> swear -> repeat (100 times) -> run

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by bcarl314
    remember Java basics

    file temp.java

    Code:
    class temp {
       public static void main(string[args]) {
          //read input and process and return
          //the rest is up to you...
       }
    }
    Code -> compile -> run

    Or, if you program like I do...

    code -> compile -> swear -> code -> compile -> swear -> repeat (100 times) -> run
    while(compile != true){
    swear = new Swear();
    }
    this.run;
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New to the CF scene
    Join Date
    Aug 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys for the help, it's not really homework because I'm not a programming student, I'm a networking student and our professor didn't think anyone could come up with the code. I do know a little java because I took a java class a few semesters ago, but just needed a little shove in the right direction because this stuff is greek to me. Thanks everyone.

    Munch

  • #6
    New Coder
    Join Date
    Jun 2003
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    this is your program

    This is the program you asked for..hope you like it..I am just a bigener with java...

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class TextFieldEventTest extends JFrame{
    JTextField celcius = new JTextField(10);
    JTextField fahrenheit = new JTextField(10);
    Container c = getContentPane();
    TextFieldEventTest(){
    c.setLayout(new FlowLayout());
    c.add(new JLabel("Celcius"));
    c.add(celcius);
    celcius.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    String cString = celcius.getText();
    double cValue = Double.parseDouble(cString.trim());
    double fValue = cValue*9.0/5.0+32.0;
    fahrenheit.setText((int)fValue+"");
    }
    });

    c.add(new JLabel("Fahrenheit"));
    c.add(fahrenheit);
    fahrenheit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    String fString = fahrenheit.getText();
    double fValue = Double.parseDouble(fString.trim());
    double cValue = (fValue-32.0)*5.0/9.0;
    celcius.setText((int)cValue+"");
    }
    });
    } // end of constructor
    public static void main(String [] args){
    TextFieldEventTest t = new TextFieldEventTest();
    t.pack();
    t.show();
    }
    }

    compile this program and then run it...
    yours,


  •  

    Posting Permissions

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