9 dic 2010

Estructura secuencial ejemplo 2

El siguiente programa lee una cantidad en gigabytes y lo convierte a megabytes, kilobytes, bytes y bits.

-> Pseudocódigo:

Inicio

//Declaración de variables
double gigas,megas,kilo,bytes,bits

//Entrada de datos
Leer gigas

//Proceso (conversiones)
megas = gigas*1024
kilo = megas*1024
bytes = kilo*1024
bits = bytes*8

//Salida de resultados
Visualizar megas,kilo,bytes,bits

Fin

-> Programación en JCreatorAbrir el JCreator, crear un nuevo proyecto (basic java applet) con el nombre "calculadorabits".

* No incluir las comillas ("").


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

public class calculadorabits extends JApplet implements ActionListener{

          //Declaración de controles
          JLabel lblGigabytes;
          JTextField txtGigabytes;
          JButton btnCalcular;
          JTextArea txtA;
          JScrollPane scpResultado;

          //Creación de la interfaz gráfica de usuario GUI
          public void init(){
                    getContentPane().setLayout(null);

                    lblGigabytes = new JLabel("Gigabytes");
                    lblGigabytes.setBounds(20,20,70,23);
                    getContentPane().add(lblGigabytes);

                    txtGigabytes = new JTextField();
                    txtGigabytes.setBounds(90,20,100,23);
                    getContentPane().add(txtGigabytes);

                    btnCalcular = new JButton("Calcular");
                    btnCalcular.setBounds(219,20,100,23);
                    btnCalcular.addActionListener(this);
                    getContentPane().add(btnCalcular);

                    txtA = new JTextArea();
                    scpResultado = new JScrollPane(txtA);
                    scpResultado.setBounds(20,53,300,100);
                    getContentPane().add(scpResultado);
          }

          //Proceso eventos ActionEvent
          public void actionPerformed( ActionEvent e ){

                    //Declaración de variables
                    double gigas,megas,kilo,bytes,bits;

                    //Entrada de datos
                    gigas = Double.parseDouble(txtGigabytes.getText());

                    //Proceso (conversiones)
                    megas = gigas*1024;
                    kilo = megas*1024;
                    bytes = kilo*1024;
                    bits = bytes*8;

                    //Visualizar salida de resultados
                    txtA.setText("Megabytes: "+megas+"\n");
                    txtA.append("Kilobytes: "+kilo+"\n");
                    txtA.append("Bytes: "+bytes+"\n");
                    txtA.append("Bits: "+bits);
          }
}



- Ir al primer ejemplo de estructura secuencial.

- Ir a estructura de selección simple if.

No hay comentarios:

Publicar un comentario