Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

JAVA - GUI metric and temp conversion program - help with output!

$
0
0
My outputs are nor correct at all. The only outputs that are correct is Celsius to Fahrenheit. i put an text output in the round off method to see what it is doing before exporting it to the GUI. and on all except for Celsius to Fahrenheit it outputs text two times. The first is correct but the second is wrong and that is what the user sees.

I do not understand why it runs though twice?

Conversion.java
public class Conversion {

    public static double m2k( double m ) {
        return roundOff( m*1.6 );
    } //end m2k method

    public static double k2m( double k ) {
        return roundOff( k*0.62 );
    } //end k2m method

    protected static double roundOff( double d ) {
        d = d * 100;
    	d = Math.round( d );
        d = d / 100;
        System.out.println(d);
        return d;
    } // end of roundOff method
} //end of class



MetricConverter.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;

public class MetricConverter extends JFrame implements ActionListener {
	
    private JLabel title = new JLabel("Metric Converter Form" );
    private JPanel titlePanel = new JPanel( );
    private JLabel inputLabel = new JLabel( "Input:" );
    private JTextField inputField = new JTextField( 10 );
    private JPanel inputPanel = new JPanel( );
    private JPanel resultPanel = new JPanel( );
    private JTextField resultField = new JTextField( 10 );
    private JLabel resultLabel = new JLabel( "Results:" );
    private JPanel converterPanel = new JPanel( );
    private JButton convertBtn = new JButton("Convert");
    private JButton clearBtn = new JButton("Clear");
    private JPanel buttonPanel = new JPanel();
    private JPanel controlPanel = new JPanel();
    private ButtonGroup bg = new ButtonGroup();
    private JPanel typePanel = new JPanel();
    private JRadioButton m2kButton = new JRadioButton("Miles to KM");
    private JRadioButton k2mButton = new JRadioButton("KM to Miles");
    private JRadioButton f2cButton = new JRadioButton("Fahrenheit to Celsius");
    private JRadioButton c2fButton = new JRadioButton("Celsius to Fahrenheit");


    public MetricConverter( ) {
    	super("Metric Converter");
        createTitlePanel(title);
        getContentPane().add(titlePanel, BorderLayout.NORTH);
        createConverterPanel( );
        getContentPane().add( converterPanel, BorderLayout.CENTER);
        createControlPanel();
        getContentPane().add(controlPanel, BorderLayout.SOUTH);
    }//end of constructor
    
    private void createTitlePanel( JLabel t ) {
        titlePanel.add( t );
    }
    
    private void createInputPanel( ) {
    	inputPanel.setLayout(new GridLayout(1,2));
        inputPanel.add( inputLabel  );
        inputPanel.add( inputField );
    }

    private void createResultPanel( ) {
    	resultPanel.setLayout(new GridLayout(1,2));
        resultPanel.add( resultLabel );
        resultPanel.add( resultField );
    }

    private void createConverterPanel( ) {
    	converterPanel.setLayout(new GridLayout(2,1));
        createInputPanel( );
        converterPanel.add( inputPanel );
        createResultPanel( );
        converterPanel.add(resultPanel);
   }
    
    private void createControlPanel( ){
        controlPanel.setLayout(new GridLayout(1,2));
    	bg.add(m2kButton);
        bg.add(k2mButton);
        bg.add(f2cButton);
        bg.add(c2fButton);
        typePanel.setLayout(new GridLayout(4,1));
        createTypeBorder( );
        m2kButton.setSelected(true);
        typePanel.add(m2kButton);
        typePanel.add(k2mButton);
        typePanel.add(f2cButton);
        typePanel.add(c2fButton);
        controlPanel.add(typePanel);
        buttonPanel.setLayout(new GridLayout(1,2));
        buttonPanel.add(convertBtn);
        buttonPanel.add(clearBtn);
        controlPanel.add(buttonPanel);
        convertBtn.addActionListener(this);
        clearBtn.addActionListener(this);
    }
    
    public void createTypeBorder(){
    	Border etchedBorder = BorderFactory. createEtchedBorder(EtchedBorder.LOWERED); 
    	TitledBorder titleBorder = BorderFactory.createTitledBorder( etchedBorder, "convert:");
		titleBorder.setTitleJustification(TitledBorder.LEFT);
		titleBorder.setTitleColor(Color.GRAY);
    	typePanel.setBorder(titleBorder); 
    }
    
    public void actionPerformed(ActionEvent ae) {
		if ( ae.getSource( ) == convertBtn )  {
			double d = 0.0;
	    	d = Double.parseDouble( inputField.getText( ) );
	    	if( m2kButton.isSelected( ) )
	    	{
	    		d = Conversion.m2k( d );
	    	}
	    	else if( k2mButton.isSelected( ) )
	    	{
	    		d = Conversion.k2m( d );
	    	}
	    	else if( f2cButton.isSelected())
	    	{
	    		d = TempConversion.f2c( d );
	    	}
	    	else if( c2fButton.isSelected());
	    	{
	    		d = TempConversion.c2f( d );
	    	}
	 		resultField.setText( Double.toString(d ) );
	    }
	    else if ( ae.getSource( ) == clearBtn ){
	        inputField.setText("");
	        resultField.setText("");
	   }
    } //end actionPerformed method


    public static void main( String[ ] args ) {
        final int FRAMEWIDTH = 500;  
        final int FRAMEHEIGHT = 400;
        MetricConverter mc = new MetricConverter( ); 
        mc.setSize( FRAMEWIDTH, FRAMEHEIGHT );
        mc.pack();
        mc.setVisible( true );
    }//end of main method
}//end of class



TempConversion.java
public class TempConversion extends Conversion {
	
	public static double f2c( double f ) {
        return roundOff((f - 32) * 5/9);
    } //end f2c method

    public static double c2f( double c ) {
        return roundOff(c * 9/5 + 32);
    } //end c2f method
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>