Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-26-2003, 03:35 AM   PM User | #1
munch
New to the CF scene

 
Join Date: Aug 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
munch is an unknown quantity at this point
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
munch is offline   Reply With Quote
Old 09-26-2003, 04:37 AM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,234
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
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
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 09-26-2003, 02:51 PM   PM User | #3
bcarl314
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
bcarl314 will become famous soon enough
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
bcarl314 is offline   Reply With Quote
Old 09-26-2003, 03:37 PM   PM User | #4
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,234
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
Quote:
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
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 09-26-2003, 06:02 PM   PM User | #5
munch
New to the CF scene

 
Join Date: Aug 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
munch is an unknown quantity at this point
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
munch is offline   Reply With Quote
Old 10-23-2003, 09:55 AM   PM User | #6
fuadsm
New Coder

 
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
fuadsm is an unknown quantity at this point
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,
fuadsm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:48 PM.


Advertisement
Log in to turn off these ads.