Question

design a windows application (measurement converter) that has two text boxes and tow combo boxes, the...

design a windows application (measurement converter) that has two text boxes and tow combo boxes, the user will assign an integer value in the first text box and to be added to the selected unit from the first combo box then the user will choose another unit from the second combo box and the convertered value will be shown on the second text box

the units are :

Kilometer
Meter
Centimeter
Millimeter
Mile
Yard
Foot
Inch
Nautical Mile

(on c#)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Converter extends JFrame {

    static JFrame frame;
    static JComboBox metb, impb;
    static Container pane;
    static Insets insets;
    static JLabel to;
    static JButton go, clear1, clear2;
    static JTextField metf, impf;
    static Dimension screenSize;
    static Point middle, newLocation;  
    
    public static void compute(String meti, String impi, int x, int y, double ratio) {
        if (meti.length() >= 1 && impi.length() == 0) { //converting from metric to imperial        
            if (metb.getSelectedIndex() != 0 && impb.getSelectedIndex() != 0) { //if user has selected units     
                //length
                if (metb.getSelectedIndex() >= 1 && impb.getSelectedIndex() >= 1) {                   
                    if (metb.getSelectedIndex() == x && impb.getSelectedIndex() == y) { 
                    double parse = Double.parseDouble(meti);
                    double ans = parse * ratio;
                    String result = "" + ans;
                    impf.setText(result);                                      
                    }                                        
                }
            } else {
                JOptionPane.showMessageDialog(frame,"Please select you units");
            }       
        } else if (meti.length() == 0 && impi.length() >= 1) { //converting from imperial to metric  
            if (metb.getSelectedIndex() != 0 && impb.getSelectedIndex() != 0) { //if user has selected units
                if (metb.getSelectedIndex() >= 1 && impb.getSelectedIndex() >= 1) {                   
                    if (metb.getSelectedIndex() == x && impb.getSelectedIndex() == y) { 
                    double parse = Double.parseDouble(impi);
                    double ans = parse / ratio;
                    String result = "" + ans;
                    metf.setText(result);                                      
                    }                                        
                }
            } else {
                JOptionPane.showMessageDialog(frame,"Please select your units");
            }   
        }        
    }
    
    public static void temperature(String meti, String impi) {
        if (meti.length() >= 1 && impi.length() == 0) { //converting from metric to imperial        
            if (metb.getSelectedIndex() != 0 && impb.getSelectedIndex() != 0) { //if user has selected units     
                //length
                if (metb.getSelectedIndex() >= 7 && metb.getSelectedIndex() <= 8) {                   
                    if (metb.getSelectedIndex() == 7 && impb.getSelectedIndex() == 10) {//k f
                        double parse = Double.parseDouble(meti);
                        double ans = 9/5 * (parse - 273) + 32;
                        String result = "" + ans;
                        impf.setText(result);    
                    } else if (metb.getSelectedIndex() == 8 && impb.getSelectedIndex() == 9) {//c k
                        double parse = Double.parseDouble(meti);
                        double ans = parse + 273;
                        String result = "" + ans;
                        impf.setText(result);
                    } else if (metb.getSelectedIndex() == 8 && impb.getSelectedIndex() == 10) {//c f
                        double parse = Double.parseDouble(meti);
                        double ans = 9/5 * parse + 32;
                        String result = "" + ans;
                        impf.setText(result);
                    }                                                      
                }
            } else {
                JOptionPane.showMessageDialog(frame,"Please select your units");
            }       
        } else if (meti.length() == 0 && impi.length() >= 1) { //converting from imperial to metric  
            if (metb.getSelectedIndex() != 0 && impb.getSelectedIndex() != 0) { //if user has selected units
                    if (metb.getSelectedIndex() == 7 && impb.getSelectedIndex() == 10) {//f k
                        double parse = Double.parseDouble(impi);
                        double ans = 9/5 * (parse - 32) + 273;
                        String result = "" + ans;
                        metf.setText(result);    
                    } else if (metb.getSelectedIndex() == 8 && impb.getSelectedIndex() == 9) {//k c
                        double parse = Double.parseDouble(impi);
                        double ans = parse - 273;
                        String result = "" + ans;
                        metf.setText(result);
                    } else if (metb.getSelectedIndex() == 8 && impb.getSelectedIndex() == 10) {//f c
                        double parse = Double.parseDouble(impi);
                        double ans = 9/5 * parse - 32;
                        String result = "" + ans;
                        metf.setText(result);
                    }
            } else {
                JOptionPane.showMessageDialog(frame,"Please select you units");
            }   
        }       
    }        
    
    public static class goAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String meti = metf.getText();
            String impi = impf.getText();
            if (meti.length() == 0 && impi.length() >= 1 || meti.length() >= 1 && impi.length() == 0) {
                compute(meti, impi, 1, 1, 0.0393700787); //mm in
                compute(meti, impi, 1, 2, 0.0032808399); //mm ft
                compute(meti, impi, 1, 3, 0.0010936133);//mm yd
                compute(meti, impi, 1, 4, 0.000000621371192); //mm mi
                compute(meti, impi, 2, 1, 0.393700787);//cm in
                compute(meti, impi, 2, 2, 0.032808399);//cm ft
                compute(meti, impi, 2, 3, 0.010936133);//cm yd
                compute(meti, impi, 2, 4, 0.00000621371192);//cm mi
                compute(meti, impi, 3, 1, 39.3700787);//m in
                compute(meti, impi, 3, 2, 3.2808399);//m ft
                compute(meti, impi, 3, 3, 1.0936133);//m yd
                compute(meti, impi, 3, 4, 0.000621371192);//m mi
                compute(meti, impi, 4, 1, 39370.0787);//km in
                compute(meti, impi, 4, 2, 3280.8399);//km ft
                compute(meti, impi, 4, 3, 1093.6133);//km yd
                compute(meti, impi, 4, 4, 0.621371192);//km mi
                compute(meti, impi, 5, 5, 0.0338140227);//ml fl oz
                compute(meti, impi, 5, 6, 0.00211337642);//ml pt
                compute(meti, impi, 5, 7, 0.00105668821);//ml qt
                compute(meti, impi, 5, 8, 0.000264172052);//ml gal
                compute(meti, impi, 6, 5, 33.8140227);//l fl oz
                compute(meti, impi, 6, 6, 2.11337642);//l pt
                compute(meti, impi, 6, 7, 1.05668821);//l qt
                compute(meti, impi, 6, 8, 0.264172052);//l gal
                compute(meti, impi, 7, 9, 1); //k k   
                compute(meti, impi, 9, 11, 0.0352739619);//g oz
                compute(meti, impi, 9, 12, 0.00220462262);//g lb
                compute(meti, impi, 9, 13, 0.000157473044);//g st
                compute(meti, impi, 9, 14, 0.00000110231131);//g t
                compute(meti, impi, 10, 11, 35.2739619);//kg oz
                compute(meti, impi, 10, 12, 2.20462262);//kg lb
                compute(meti, impi, 10, 13, 0.157473044);//kg st
                compute(meti, impi, 10, 14, 0.00110231131);//kg t
                compute(meti, impi, 11, 11, 35273.9619);//mt oz
                compute(meti, impi, 11, 12, 2204.62262);//mt lb
                compute(meti, impi, 11, 13, 157.473044);//mt st
                compute(meti, impi, 11, 14, 1.10231131);//mt t
                temperature(meti, impi);//k f, c k, c f 
            } else if (meti.length() >= 1 && impi.length() >= 1) { //input in two fields        
                JOptionPane.showMessageDialog(frame,"Please only put a number on one side");
                impf.setText("");
                metf.setText("");             
            } else if (meti.length() == 0 && impi.length() == 0) { //input in no fields        
                JOptionPane.showMessageDialog(frame,"Please enter a number to be converted");            
            }                 
        }
    }       
    
    public static class clear1Action implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            metf.setText("");
        }
    }
    
    public static class clear2Action implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            impf.setText("");
        }
    }
    
    public static void main (String[] args) {
    
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch (ClassNotFoundException e) {}
        catch (InstantiationException e) {}
        catch (IllegalAccessException e) {}
        catch (UnsupportedLookAndFeelException e) {}
    
        String[] metric = { "metric:", "millimetres", "centimetres", "metres", "kilometres", "mililitres", "litres", "kelvin", "celcius", "grams", "kilograms", "tonnes" };                
        String[] imperial = { "imperial:", "inches", "feet", "yards", "miles", "fluid ounces", "US pints", "US quarts", "US gallons", "kelvin", "fahrenheit", "ounces", "pounds", "stones", "tons" };   
        
        frame = new JFrame ("Unit Converter");                        
        metb = new JComboBox (metric);
        impb = new JComboBox (imperial);  
        to = new JLabel("< -- to -- >");
        go = new JButton("Go");
        clear1 = new JButton("C");
        clear2 = new JButton("C");
        metf = new JTextField();
        impf = new JTextField();             
               
        pane = frame.getContentPane();
        insets = pane.getInsets();
        pane.setLayout(null);        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        metb.setSelectedIndex(0);
        impb.setSelectedIndex(0);
        
        pane.add(metf);
        metf.setBounds(insets.left + 5, insets.top + 5, 200, metf.getPreferredSize().height);       
        
        pane.add(metb);
        metb.setBounds(insets.left + 5, metf.getHeight() + 5, 282, metb.getPreferredSize().height);
        
        pane.add(clear1);
        clear1.setBounds(metf.getX() + metf.getWidth() + 5, insets.top + 5, clear1.getPreferredSize().width, metb.getPreferredSize().height);
        
        pane.add(to);
        to.setBounds(clear1.getX() + clear1.getWidth() + 5, insets.top + 5, to.getPreferredSize().width, to.getPreferredSize().height);
        
        pane.add(impf);
        impf.setBounds(to.getX() + to.getWidth() + 5, insets.top + 5, 200, impf.getPreferredSize().height);  
        
        pane.add(impb);
        impb.setBounds(metb.getX() + metb.getWidth() + 5, impf.getHeight() + 5, 283, impb.getPreferredSize().height);
        
        pane.add(clear2);
        clear2.setBounds(impf.getX() + impf.getWidth() + 5, insets.top + 5, clear2.getPreferredSize().width, metb.getPreferredSize().height);      
        
        pane.add(go);
        go.setBounds(clear2.getX() + clear2.getWidth() + 5, insets.top + 5, go.getPreferredSize().width, metb.getPreferredSize().height);
        
        go.addActionListener(new goAction());
        clear1.addActionListener(new clear1Action());
        clear2.addActionListener(new clear2Action());
        
        frame.setSize(580,110); 
        
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        middle = new Point(screenSize.width / 2, screenSize.height / 2);
        newLocation = new Point(middle.x - (frame.getWidth() / 2), middle.y - (frame.getHeight() / 2));
        frame.setLocation(newLocation);
        
        frame.setVisible(true);
    }
}
Add a comment
Know the answer?
Add Answer to:
design a windows application (measurement converter) that has two text boxes and tow combo boxes, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the valu...

    Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: From To Conversion Miles - Kilometers: 1 mile = 1.6093 kilometers Kilometers - Miles: 1 kilometer = 0.6214 miles Feet - Meters: 1 foot = 0.3048 meters Meters - Feet: 1 meter = 3.2808 feet Inches - Centimeters: 1 inch = 2.54 centimeters Centimeters - Inches: 1...

  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game si...

    Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game simulates a card game where the cards a placed face down and the player flips over pairs of cards in an attempt to find matching cards.   Program Procedure Display a 4x4 grid of “face down” cards. Assign the letters A through H randomly to the cards in pairs. Allow the user to click on a card to “flip” it over and...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but...

    If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but minor discrepancies may require you to adjust. If you are attempting this assignment using another version of Visual Studio, you can expect differences in the look, feel, and/or step-by-step instructions below and you’ll have to determine the equivalent actions or operations for your version on your own. INTRODUCTION: In this assignment, you will develop some of the logic for, and then work with, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT