Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-14-2007, 06:19 PM
javic's Avatar   
javic javic is offline
Newbie
 
Join Date: Feb 2007
Posts: 5
Rep Power: 0
javic is on a distinguished road
Default need help on class project

Hello there, I've been working on a assignment given to me two weeks ago.
as of now I'm still unable to arrange the code in its consecutive order. this assignment basically was to setup a currency converter from american dollar to YEN, EURO and PESO. Everything is already setup except the formula code that will do the convertion process( If...else statement, or so ) In order to avoid confusion or for better visibility I've pasted the whole code which I'm working on. I would really appreciate your help.


Code:
public class CurrencyConverter extends JFrame
{
   // JLabel and JTextField to input dollar amount
   private JLabel dollarJLabel;
   private JTextField dollarJTextField;
   
   // JLabel and JTextField to input currency type to convert to
   private JLabel typeJLabel;
   private JTextField typeJTextField;
   
   // JLabel and JTextField to display converted value
   private JLabel convertedJLabel;
   private JTextField convertedJTextField;
   
   // JButton to initiate conversion
   private JButton convertJButton;
   
   // no-argument constructor
   public CurrencyConverter()
   {
      createUserInterface();
   }

   // create and position GUI components; register event handlers
   private void createUserInterface()
   {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components
      contentPane.setLayout( null ); 
      
      // set up dollarJLabel
      dollarJLabel = new JLabel();
      dollarJLabel.setBounds( 16, 16, 128, 21 );
      dollarJLabel.setText( "Dollars to convert:" );
      contentPane.add( dollarJLabel );
            
      // set up dollarJTextField
      dollarJTextField = new JTextField();
      dollarJTextField.setBounds( 175, 16, 96, 21 );
      dollarJTextField.setHorizontalAlignment( JTextField.RIGHT );
      contentPane.add( dollarJTextField );
      
      // set up typeJLabel
      typeJLabel = new JLabel();
      typeJLabel.setBounds( 16, 56, 150, 21 );
      typeJLabel.setText( "Convert from dollars to:" );
      contentPane.add( typeJLabel );
      
      // set up typeJTextField
      typeJTextField = new JTextField();
      typeJTextField.setBounds( 175, 56, 96, 21 );
      typeJTextField.setHorizontalAlignment( JTextField.RIGHT );
      contentPane.add( typeJTextField );
      
      // set up convertedJLabel
      convertedJLabel = new JLabel();
      convertedJLabel.setBounds( 16, 96, 112, 24 );
      convertedJLabel.setText( "Converted amount:" );
      contentPane.add( convertedJLabel );

      // set up convertedJTextField
      convertedJTextField = new JTextField();
      convertedJTextField.setBounds( 175, 96, 96, 21 );
      convertedJTextField.setHorizontalAlignment( 
         JTextField.RIGHT );
      convertedJTextField.setEditable( false );
      contentPane.add( convertedJTextField );
      
      // set up convertJButton and register its event handler
      convertJButton = new JButton();
      convertJButton.setText( "Convert" );
      convertJButton.setBounds( 175, 136, 96, 24 );
      contentPane.add( convertJButton );
      convertJButton.addActionListener(
         
         new ActionListener() // anonymous inner class
         {
             // event handler called when convertJButton is pressed
             public void actionPerformed ( ActionEvent event )
             {
                convertJButtonActionPerformed( event );
             }

         } // end anonymous inner class

      ); // end call to addActionListener
            
      // set properties of application's window
      setTitle( "Currency Converter" ); // set title bar text
      setSize( 300, 200 );              // set window size
      setVisible( true );               // display window
      
   } // end method createUserInterface
   
   // this method is called when the user clicks convertJButton
   private void convertJButtonActionPerformed( ActionEvent event )
   {
	//FORMULA NEEDEDHERE

  
   } 
   
   // 
   public static void main( String[] args )
   {
      CurrencyConverter application = new CurrencyConverter();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } 

}
thank you

Last edited by John; 02-16-2007 at 05:57 PM. Reason: When posting code please use code tags. There are PHP, HTML and CODE tags that can be used while posting code segments.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 02-14-2007, 10:07 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

This is not a tutorial so I'm moving it to the Java Help forum, add the [ CODE ] [ /CODE ] tags to format your code better and ill have a look at it tomorrow.

And what exactly are you asking? For the conversion formula or how to implement it?

Last edited by John; 02-14-2007 at 10:24 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-15-2007, 10:57 PM
javic's Avatar   
javic javic is offline
Newbie
 
Join Date: Feb 2007
Posts: 5
Rep Power: 0
javic is on a distinguished road
Default

Sorry about that Sidewinder.
And for your question, actually I need both. The implementation as well as the conversion. Please get back at me, thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-16-2007, 07:22 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

I got rid of your anonymous inner class (because I don't like them) and implemented ActionListener (in the class header). I also got rid of (just commented it out) your typeJTextField and replaced it with typeJComboBox (to avoid any input confusion). You may want to implement a try/catch to make sure the value inputed by the user can be converted to a Double

Code:
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class CurrencyConverter extends JFrame implements ActionListener {
   // JLabel and JTextField to input dollar amount
   private JLabel dollarJLabel;
   private JTextField dollarJTextField;
   
   // JLabel and JTextField to input currency type to convert to
   private JLabel typeJLabel;
   private JComboBox typeJComboBox;
   private String[] currencyCodes = new String[] {"EUR", "CAN"};
   //private JTextField typeJTextField;
   
   // JLabel and JTextField to display converted value
   private JLabel convertedJLabel;
   private JTextField convertedJTextField;
   
   // JButton to initiate conversion
   private JButton convertJButton;
   
   // Double value of dollarJTextField
   double value = 0;
   
   // no-argument constructor
   public CurrencyConverter() {
      createUserInterface();
   }

   // create and position GUI components; register event handlers
   public void createUserInterface() {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components
      contentPane.setLayout( null ); 
      
      // set up dollarJLabel
      dollarJLabel = new JLabel();
      dollarJLabel.setBounds( 16, 16, 128, 21 );
      dollarJLabel.setText( "Dollars to convert:" );
      contentPane.add( dollarJLabel );
            
      // set up dollarJTextField
      dollarJTextField = new JTextField();
      dollarJTextField.setBounds( 175, 16, 96, 21 );
      dollarJTextField.setHorizontalAlignment( JTextField.RIGHT );
      contentPane.add( dollarJTextField );
      
      // set up typeJLabel
      typeJLabel = new JLabel();
      typeJLabel.setBounds( 16, 56, 150, 21 );
      typeJLabel.setText( "Convert from dollars to:" );
      contentPane.add( typeJLabel );
      
      // set up typeJComboBox
      typeJComboBox = new JComboBox(currencyCodes);
      typeJComboBox.setBounds( 175, 56, 96, 21 );
      contentPane.add( typeJComboBox );
      
      // set up typeJTextField
      //typeJTextField = new JTextField();
      //typeJTextField.setBounds( 175, 56, 96, 21 );
      //typeJTextField.setHorizontalAlignment( JTextField.RIGHT );
      //contentPane.add( typeJTextField );
      
      // set up convertedJLabel
      convertedJLabel = new JLabel();
      convertedJLabel.setBounds( 16, 96, 112, 24 );
      convertedJLabel.setText( "Converted amount:" );
      contentPane.add( convertedJLabel );

      // set up convertedJTextField
      convertedJTextField = new JTextField();
      convertedJTextField.setBounds( 175, 96, 96, 21 );
      convertedJTextField.setHorizontalAlignment( 
         JTextField.RIGHT );
      convertedJTextField.setEditable( false );
      contentPane.add( convertedJTextField );
      
      // set up convertJButton and register its event handler
      convertJButton = new JButton();
      convertJButton.setText( "Convert" );
      convertJButton.setBounds( 175, 136, 96, 24 );
      contentPane.add( convertJButton );
      convertJButton.addActionListener(this);
            
      // set properties of application's window
      setTitle( "Currency Converter" ); // set title bar text
      setSize( 300, 200 );              // set window size
      setVisible( true );               // display window
      
   } // end method createUserInterface
   
   // this method is called when the user clicks convertJButton
   public void actionPerformed(ActionEvent e) {
           value = Double.parseDouble(dollarJTextField.getText());

	   //Convert from USD to EUR
	   if(typeJComboBox.getSelectedItem().equals("EUR")){
		   convertedJTextField.setText(String.valueOf(value * 0.761499).substring(0, 4)); 
	   } 
	   
	   //Convert from USD to CAN
	   else if(typeJComboBox.getSelectedItem().equals("CAN")){
		   convertedJTextField.setText(String.valueOf(value * 1.16385).substring(0, 4));
	   }
	   
	   //Continue the same "else if" process for all the currenct you want to convert
	} 
   
   // 
   public static void main( String[] args )
   {
      CurrencyConverter application = new CurrencyConverter();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   }
}

Last edited by John; 02-16-2007 at 07:29 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-19-2007, 04:31 PM
javic's Avatar   
javic javic is offline
Newbie
 
Join Date: Feb 2007
Posts: 5
Rep Power: 0
javic is on a distinguished road
Default

thnx alot sidewinder that really helps.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A cool project idea. AlexanderRybak Community Projects 3 02-11-2008 03:37 AM
Issue writing to file: pointer to a class which contains pointers to other classes Sheemer C and C++ 0 08-21-2007 02:17 AM
Java Help Files xXHalfSliceXx Java Help 3 11-29-2006 12:30 AM
Contributing to a project Jordan Community Projects 7 09-15-2006 05:43 PM
Community Project! - Recent project gone sour Crane C# Programming 5 09-09-2006 03:13 PM


All times are GMT -5. The time now is 11:30 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads