The Artima Developer Community
Sponsored Link

Java Answers Forum
plz help me. i'm over-whelmed

1 reply on 1 page. Most recent reply: Dec 12, 2003 12:58 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
lena obaid

Posts: 1
Nickname: cheeks
Registered: Dec, 2003

plz help me. i'm over-whelmed Posted: Dec 12, 2003 8:02 AM
Reply to this message Reply
Advertisement
hi.. i was wondering if you could assist me in this conversion excercise. i have to make up for all the missed assignments while i was hospitalized for a month. and this one is giving me a headache for some reason. i'd really appreciate it.. thanx
Implement the folowing integer methods:
a) Method CELSIUS returns the Celsius equivalent of a Fahrenheit temperature, using the calculation
C=5.0 / 9.0 * 9 f - 32 );
b) Metod fahrenheit returns the fahrenheit equivalentof a celsius temperature, using the calculation
F = 9.0 / 5.0 * C + 32;
c) use the methods from parts (a) & (b) to write an applet that enables the user either to enter a Fahrenheit temperature and display he Celsius equivalent or to enter a Celsius temperature and display the Fahrenheit equivalent.
Note: the applet will require two JTextField objects that have registered action events. When actionPerformed is called, the ActionEvent parameter has method getSource() to determine the GUI component with which the user interacted. Your actionPerformed method should contain an if..else statemet of the form
if (actionEvent.getSource() == input1){
//process input1 interaction here

}
else { //e.getSource() == input2
// process input2 interaction here

}


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: plz help me. i'm over-whelmed Posted: Dec 12, 2003 12:58 PM
Reply to this message Reply
I trust you when you say you were hospitalized. Here is your applet. I have tested it as application.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
/*
An applet to convert celsius to Fahrenheit and vide-versa
*/
 
public class CFConversion extends JApplet implements ActionListener{
	private JTextField cText = new JTextField(15);
	private JTextField fText = new JTextField(15);
 
	public void init(){
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout (2,2)) ;
		panel.add ( new JLabel("Celsius:") ) ;
		panel.add ( cText ) ;
		panel.add ( new JLabel("Fahrenheit:") ) ;
		panel.add ( fText ) ;
		cText.addActionListener ( this ) ;
		fText.addActionListener ( this ) ;
		this.getContentPane().add (panel, "North");
	}
 
	public void actionPerformed(ActionEvent e ) {
		double c = 0.0;
		double f = 0.0;
		if ( e.getSource () == cText ) {
			 try {
				if ( cText.getText().equals("")){
					// Clear the F text
					fText.setText("");
				}
				else {
					c = Double.parseDouble(cText.getText());
					f = CtoF(c);
					fText.setText(Double.toString(f));
				}
			 }
			catch(NumberFormatException e1) {
			  // Just clear both fields
			 clearFields();
			}
		}
		else if ( e.getSource() == fText) {
			 try {
				if ( fText.getText().equals("")){
					// Clear the C text
					cText.setText("");
				}
				else {
					f = Double.parseDouble(fText.getText());
					c = FtoC(f);
					cText.setText(Double.toString(c));
				}
			 }
			catch(NumberFormatException e2) {
			  // Just clear both fields
			 clearFields();
			}
 
		}
	}
 
	public double CtoF(double c) {
		return (9.0/5.0) * c + 32.0;
	}
 
	public double FtoC(double f) {
		return  (5.0/9.0) * (f - 32.0);
	}
	public void clearFields(){
		cText.setText("");
		fText.setText("");
	}
 
 
	public static void main(String[] args) {
		JFrame f = new JFrame();
		CFConversion cf = new CFConversion();
		cf.init();
		f.getContentPane().add (cf);
		f.pack();
		f.setVisible ( true);
	}
}

Flat View: This topic has 1 reply on 1 page
Topic: Font and Applet Previous Topic   Next Topic Topic: little help please.  i am sure u could do it in secs.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use