This forum is in archive mode. You will not be able to post new content.

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - j12stealth

Pages: [1]
1
hey thanks for the tip now i know why people are hard to find

2
Java / [JAVA] Metric conversion help?
« on: March 18, 2012, 12:05:49 PM »
Hey guys I need help about this code this is my case study for COPRO(computer programming 2).
Im using eclipse indigo to run my java codes and i think it runs properly but when i run this to jcreator it says it has 1 or 2 errors..if yah know which are the errors please kindly comment below



Code: [Select]
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<Object> (metric);
        impb = new JComboBox<Object> (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);
    }
}

Pages: [1]


Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.