• Lenguaje

    Java usando AWT

  • Descripción

    A partir de proporcionar la velocidad de un automóvil expresada en kilómetros por hora proporcione la velocidad en metros por segundo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.awt.*;
import java.awt.event.*;

public class VelocidadEnKmhAMs extends Frame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private TextField field_velocidad_en_km_por_h;
    private Label label_velocidad_en_m_por_s;
    private Button button;

    public Algoritmo() {
        field_velocidad_en_km_por_h = new TextField(4);
        label_velocidad_en_m_por_s = new Label();
        button = new Button("Procesar");
        setLayout(new BorderLayout());
        Panel panel, subpanel;
        panel = new Panel(new BorderLayout());
        subpanel = new Panel(new GridLayout(1, 1));
        subpanel.add(new Label("Ingresa el valor de velocidad en km por h:"));
        panel.add(subpanel, BorderLayout.WEST);
        subpanel = new Panel(new GridLayout(1, 1));
        subpanel.add(field_velocidad_en_km_por_h);
        panel.add(subpanel);
        add(panel, BorderLayout.NORTH);
        panel = new Panel(new FlowLayout());
        panel.add(button);
        add(panel);
        panel = new Panel(new BorderLayout());
        subpanel = new Panel(new GridLayout(1, 1));
        subpanel.add(new Label("Valor de velocidad en m por s:"));
        panel.add(subpanel, BorderLayout.WEST);
        subpanel = new Panel(new GridLayout(1, 1));
        subpanel.add(label_velocidad_en_m_por_s);
        panel.add(subpanel);
        add(panel, BorderLayout.SOUTH);
        button.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        double velocidad_en_km_por_h, velocidad_en_m_por_s;
        try {
            velocidad_en_km_por_h = Double.parseDouble(field_velocidad_en_km_por_h.getText());
        } catch (NumberFormatException numberFormatException) {
            return;
        }
        velocidad_en_m_por_s=velocidad_en_km_por_h*10/36;
        label_velocidad_en_m_por_s.setText(String.valueOf(velocidad_en_m_por_s));
        pack();
    }

    public static void main(String[] args) {
        Algoritmo algoritmo = new Algoritmo();
        algoritmo.pack();
        algoritmo.setLocationRelativeTo(null);
        algoritmo.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        algoritmo.setVisible(true);
    }

}