PDA

View Full Version : Small Java program help


munch
09-26-2003, 04:35 AM
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

Spookster
09-26-2003, 05:37 AM
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.

bcarl314
09-26-2003, 03:51 PM
remember Java basics

file temp.java


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

Spookster
09-26-2003, 04:37 PM
Originally posted by bcarl314
remember Java basics

file temp.java


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;

munch
09-26-2003, 07:02 PM
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

fuadsm
10-23-2003, 10:55 AM
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,