Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.

Archivo del blog

domingo, 5 de diciembre de 2021

Interés compuesto

En Netbeans se crea un JFrame y en modo diseño se añaden los siguientes componentes:

jButton_Info
jButton_X
jButton_Calcular
jButton_Limpiar

jTextField_PeriodoAhorro
jTextField_Rentabilidad
jTextField_SaldoFinal
jTextField_SaldoInicial

jLabels varios para identificar tipos de datos que hay que añadir en los jTextFields.
jPanels varios para agrupar y facilitar la inserción de los componentes cuando operamos en modo diseño.


Código Java (InteresCompuesto.java)

package interes_c;

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class InteresCompuesto extends javax.swing.JFrame {

    DecimalFormat f = new DecimalFormat("###, ###. ##");

    public InteresCompuesto() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    private void initComponents() { ... *Código generado automaticamente por NetBeans ...}

    private void jButton_XActionPerformed(java.awt.event.ActionEvent evt) {                                          
        this.dispose();
    }                                         

    private void jButton_InfoActionPerformed(java.awt.event.ActionEvent evt) {                                             

        String msg
                = "\n  Autor: Censor Cosmico\n"
                + "  Versión: 2021 DIC\n"
                + "  Fecha de creación: 04-12-2021\n"
                + "  http://censorcosmico.blogspot.com.es/\n ";

        JOptionPane.showMessageDialog(null, msg, "Info - Interés Compuesto -", JOptionPane.INFORMATION_MESSAGE);

    }

    private void jButton_LimpiarActionPerformed(java.awt.event.ActionEvent evt) {                                                
        jTextField_PeriodoAhorro.setText("");
        jTextField_Rentabilidad.setText("");
        jTextField_SaldoInicial.setText("");
        jTextField_SaldoFinal.setText("?");
    }                                               

    private void jButton_CalcularActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        calcular();
    }                                                

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InteresCompuesto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton_Calcular;
    private javax.swing.JButton jButton_Info;
    private javax.swing.JButton jButton_Limpiar;
    private javax.swing.JButton jButton_X;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JTextField jTextField_PeriodoAhorro;
    private javax.swing.JTextField jTextField_Rentabilidad;
    private javax.swing.JTextField jTextField_SaldoFinal;
    private javax.swing.JTextField jTextField_SaldoInicial;
    // End of variables declaration                  


    private void calcular() {
        // Formula interes compuesto: Cn=C0*(1+i)^n
        float n = Float.parseFloat(jTextField_PeriodoAhorro.getText());
        float c0 = Float.parseFloat(jTextField_SaldoInicial.getText());
        float i = Float.parseFloat(jTextField_Rentabilidad.getText()) / 100f;
        double cn = c0 * Math.pow((1 + i), n);
        jTextField_SaldoFinal.setText("" + f.format(cn));
    }
}


Resultado: 




















*Código generado por Netbeans en modo diseño:

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jButton_Info = new javax.swing.JButton();
        jButton_X = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jTextField_PeriodoAhorro = new javax.swing.JTextField();
        jTextField_Rentabilidad = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jTextField_SaldoInicial = new javax.swing.JTextField();
        jTextField_SaldoFinal = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel10 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jButton_Limpiar = new javax.swing.JButton();
        jButton_Calcular = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        jLabel1.setText("Interés Compuesto");

        jPanel7.setBackground(new java.awt.Color(0, 0, 0));
        jPanel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel7.setLayout(new java.awt.GridLayout(1, 0));

        jButton_Info.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 18)); // NOI18N
        jButton_Info.setText("Info");
        jButton_Info.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_InfoActionPerformed(evt);
            }
        });
        jPanel7.add(jButton_Info);

        jButton_X.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 18)); // NOI18N
        jButton_X.setText("X");
        jButton_X.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_XActionPerformed(evt);
            }
        });
        jPanel7.add(jButton_X);

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jTextField_PeriodoAhorro.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
        jTextField_PeriodoAhorro.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField_PeriodoAhorro.setText("1");
        jTextField_PeriodoAhorro.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.add(jTextField_PeriodoAhorro);

        jTextField_Rentabilidad.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
        jTextField_Rentabilidad.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField_Rentabilidad.setText("10");
        jTextField_Rentabilidad.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.add(jTextField_Rentabilidad);

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        jTextField_SaldoInicial.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
        jTextField_SaldoInicial.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField_SaldoInicial.setText("100");
        jTextField_SaldoInicial.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.add(jTextField_SaldoInicial);

        jTextField_SaldoFinal.setEditable(false);
        jTextField_SaldoFinal.setBackground(new java.awt.Color(255, 255, 204));
        jTextField_SaldoFinal.setFont(new java.awt.Font("Barlow Condensed Medium", 1, 48)); // NOI18N
        jTextField_SaldoFinal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField_SaldoFinal.setText("?");
        jTextField_SaldoFinal.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.add(jTextField_SaldoFinal);

        jPanel8.setLayout(new java.awt.GridLayout(1, 0));

        jLabel7.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
        jLabel7.setText("Periodo de ahorro (años):");
        jPanel8.add(jLabel7);

        jLabel8.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
        jLabel8.setText("Rentabilidad (%):");
        jPanel8.add(jLabel8);

        jPanel10.setLayout(new java.awt.GridLayout(1, 0));

        jLabel3.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
        jLabel3.setText("Saldo inicial:");
        jPanel10.add(jLabel3);

        jLabel5.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
        jLabel5.setText("Saldo final:");
        jPanel10.add(jLabel5);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 498, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8))))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPanel2, jPanel3});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jPanel2, jPanel3});

        jButton_Limpiar.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
        jButton_Limpiar.setText("Limpiar");
        jButton_Limpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_LimpiarActionPerformed(evt);
            }
        });

        jButton_Calcular.setFont(new java.awt.Font("Barlow Condensed Medium", 0, 18)); // NOI18N
        jButton_Calcular.setText("Calcular");
        jButton_Calcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CalcularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton_Calcular)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_Limpiar)
                .addContainerGap())
        );

        jPanel6Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton_Calcular, jButton_Limpiar});

        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Calcular)
                    .addComponent(jButton_Limpiar))
                .addContainerGap())
        );

        jPanel6Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton_Calcular, jButton_Limpiar});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(98, 98, 98)
                        .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

Con la tecnología de Blogger.