-
Lenguaje
Java usando Applet
-
Descripción
Algoritmo para capturar un dato numérico entero, de acuerdo a las reglas de negocios que se describen a continuación:
- Como salida mostrar en pantalla si el dato es positivo o negativo.
- Como salida mostrar en pantalla si el dato es par o impar.
- Como salida mostrar el valor absoluto del dato.
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
66
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
66
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class SignoParidadYValorAbsoluto extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
private TextField field_dato_numerico_entero;
private Label label_absoluto;
private Button button;
@Override
public void init() {
field_dato_numerico_entero = new TextField(4);
label_absoluto = 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 dato numerico entero:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(field_dato_numerico_entero);
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 absoluto:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(label_absoluto);
panel.add(subpanel);
add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
int absoluto, dato_numerico_entero;
try {
dato_numerico_entero = Integer.parseInt(field_dato_numerico_entero.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
if(dato_numerico_entero<0)
{
javax.swing.JOptionPane.showMessageDialog(this, "Negativo");
absoluto=-dato_numerico_entero;
}
else
{
javax.swing.JOptionPane.showMessageDialog(this, "Positivo");
absoluto=dato_numerico_entero;
}
if(dato_numerico_entero%2==0)
javax.swing.JOptionPane.showMessageDialog(this, "Par");
else
javax.swing.JOptionPane.showMessageDialog(this, "Impar");
label_absoluto.setText(String.valueOf(absoluto));
}
}
import java.awt.*;
import java.awt.event.*;
public class SignoParidadYValorAbsoluto extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
private TextField field_dato_numerico_entero;
private Label label_absoluto;
private Button button;
@Override
public void init() {
field_dato_numerico_entero = new TextField(4);
label_absoluto = 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 dato numerico entero:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(field_dato_numerico_entero);
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 absoluto:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(label_absoluto);
panel.add(subpanel);
add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
int absoluto, dato_numerico_entero;
try {
dato_numerico_entero = Integer.parseInt(field_dato_numerico_entero.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
if(dato_numerico_entero<0)
{
javax.swing.JOptionPane.showMessageDialog(this, "Negativo");
absoluto=-dato_numerico_entero;
}
else
{
javax.swing.JOptionPane.showMessageDialog(this, "Positivo");
absoluto=dato_numerico_entero;
}
if(dato_numerico_entero%2==0)
javax.swing.JOptionPane.showMessageDialog(this, "Par");
else
javax.swing.JOptionPane.showMessageDialog(this, "Impar");
label_absoluto.setText(String.valueOf(absoluto));
}
}