Está en la página 1de 19

Corporación Unificada Nacional

(CUN)

PROGRAMACIÓN ORIENTADA A OBJETOS 1 (51212)

CARLOS ARTURO MARIN DELGADILLO

ESTUDIANTE: JHON JAIRO CARVAJAL HERNANDEZ

PROYECTO CALCULADORA
NETBEANS 8.2
Proyecto calculadora básica con botón elevado al cuadrado en NetBeans

1. Creación de botones y sus respectivos números

Para la creación de cada numero se le da doble clic y este automáticamente nos


lleva al espacio para generar el código y darle funciones

Ejemplo creación del número 1

Para el resto de números se realiza la misma función y solo se cambia en la parte


final el número que deseamos agregar
Ejemplo 2, creación de funciones básicas suma, resta, multiplicación y división

Realizamos los mismo con las otras funciones únicamente cambiando el signo.

Para que nuestros botones funciones debemos realizar un switch en el cual


daremos las funciones cada vez que utilicemos esos botones, se programa de la
siguiente manera:

De esta manera ya nuestra calculadora queda funcional.

Para el botón elevado al cuadrado sabemos que la fórmula es número*número y el


código es el siguiente:
Este es el código realizado para ejecutar el programa:
package calculadora.prog_objetos;

/**

* @author 57321

*/

public class Calculadora extends javax.swing.JFrame {

public float primernumero;

public float segundonumero;

public String operador;

public Calculadora() {

initComponents();

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jButton19 = new javax.swing.JButton();

jButton17 = new javax.swing.JButton();

jButton20 = new javax.swing.JButton();

jButton18 = new javax.swing.JButton();

jPanel1 = new javax.swing.JPanel();

Pantalla = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();


jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jButton4 = new javax.swing.JButton();

jButton5 = new javax.swing.JButton();

jButton6 = new javax.swing.JButton();

jButton7 = new javax.swing.JButton();

jButton9 = new javax.swing.JButton();

jButton10 = new javax.swing.JButton();

jButton11 = new javax.swing.JButton();

jButton12 = new javax.swing.JButton();

jButton13 = new javax.swing.JButton();

jButton14 = new javax.swing.JButton();

jButton15 = new javax.swing.JButton();

jButton21 = new javax.swing.JButton();

jButton23 = new javax.swing.JButton();

jButton24 = new javax.swing.JButton();

jButton16 = new javax.swing.JButton();

jButton19.setText("jButton1");

jButton17.setText("jButton1");

jButton20.setText("jButton1");

jButton18.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(204, 255, 255));

Pantalla.setBackground(new java.awt.Color(255, 255, 255));

Pantalla.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

Pantalla.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(51, 51, 255)));

Pantalla.setOpaque(true);
jButton1.setText("C");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});

jButton2.setText("*");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

});

jButton3.setText("-");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

});

jButton4.setText("/");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

});

jButton5.setText("7");

jButton5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton5ActionPerformed(evt);

}
});

jButton6.setText("8");

jButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton6ActionPerformed(evt);

});

jButton7.setText("9");

jButton7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton7ActionPerformed(evt);

});

jButton9.setText("4");

jButton9.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton9ActionPerformed(evt);

});

jButton10.setText("5");

jButton10.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton10ActionPerformed(evt);

});

jButton11.setText("6");

jButton11.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton11ActionPerformed(evt);
}

});

jButton12.setText("+");

jButton12.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton12ActionPerformed(evt);

});

jButton13.setText("1");

jButton13.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton13ActionPerformed(evt);

});

jButton14.setText("2");

jButton14.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton14ActionPerformed(evt);

});

jButton15.setText("3");

jButton15.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton15ActionPerformed(evt);

});

jButton21.setText("0");

jButton21.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


jButton21ActionPerformed(evt);

});

jButton23.setText(".");

jButton23.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton23ActionPerformed(evt);

});

jButton24.setText("=");

jButton24.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton24ActionPerformed(evt);

});

jButton16.setText("^2");

jButton16.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton16ActionPerformed(evt);

});

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)

.addComponent(Pantalla, javax.swing.GroupLayout.PREFERRED_SIZE, 262,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jButton21, javax.swing.GroupLayout.PREFERRED_SIZE, 126,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton23, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton24, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(10, 10, 10)

.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 60,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(10, 10, 10)

.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 60,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(Pantalla, javax.swing.GroupLayout.PREFERRED_SIZE, 44,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton13, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton21, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton23, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton24, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGap(99, 99, 99))

);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 0, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, Short.MAX_VALUE)

);

pack();

}// </editor-fold>

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

this.Pantalla.setText("");

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

this.primernumero=Float.parseFloat(this.Pantalla.getText());

this.operador="/";

this.Pantalla.setText("");

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

this.primernumero=Float.parseFloat(this.Pantalla.getText());

this.operador="*";

this.Pantalla.setText("");

}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {

this.primernumero=Float.parseFloat(this.Pantalla.getText());

this.operador="+";

this.Pantalla.setText("");

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

this.Pantalla.setText(this.Pantalla.getText()+"0";

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

this.Pantalla.setText(this.Pantalla.getText()+"1";

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

this.Pantalla.setText(this.Pantalla.getText()+"2";

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

this.Pantalla.setText(this.Pantalla.getText()+"3";

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

this.Pantalla.setText(this.Pantalla.getText()+"4";

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

this.Pantalla.setText(this.Pantalla.getText()+"5";

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

this.Pantalla.setText(this.Pantalla.getText()+"6";

}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

this.Pantalla.setText(this.Pantalla.getText()+"7";

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

this.Pantalla.setText(this.Pantalla.getText()+"8";

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

this.Pantalla.setText(this.Pantalla.getText()+"9";

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

this.segundonumero=Float.parseFloat(this.Pantalla.getText());

switch(this.operador){

case "+":this.Pantalla.setText(sincero(this.primernumero+this.segundonumero));break;

case "-":this.Pantalla.setText(sincero(this.primernumero-this.segundonumero));break;

case "*":this.Pantalla.setText(sincero(this.primernumero*this.segundonumero));break;

case "/":this.Pantalla.setText(sincero(this.primernumero/this.segundonumero));break;

case "^2":this.Pantalla.setText(sincero(this.primernumero*primernumero));break;

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

this.primernumero=Float.parseFloat(this.Pantalla.getText());

this.operador="-";

this.Pantalla.setText("");

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

if(!this.Pantalla.getText().contains(".")){

this.Pantalla.setText(this.Pantalla.getText()+".";
}

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

this.primernumero=Float.parseFloat(this.Pantalla.getText());

this.operador="^2";

this.Pantalla.setText("");

public String sincero(float resultado){

String retorno="";

retorno=Float.toString(resultado);

if(resultado%1==0){

retorno=retorno.substring(0, retorno.length()-2);

return retorno;

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Calculadora().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JLabel Pantalla;

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton10;

private javax.swing.JButton jButton11;

private javax.swing.JButton jButton12;


private javax.swing.JButton jButton13;

private javax.swing.JButton jButton14;

private javax.swing.JButton jButton15;

private javax.swing.JButton jButton16;

private javax.swing.JButton jButton17;

private javax.swing.JButton jButton18;

private javax.swing.JButton jButton19;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton20;

private javax.swing.JButton jButton21;

private javax.swing.JButton jButton23;

private javax.swing.JButton jButton24;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JButton jButton5;

private javax.swing.JButton jButton6;

private javax.swing.JButton jButton7;

private javax.swing.JButton jButton9;

private javax.swing.JPanel jPanel1;

Foto del proyecto:

También podría gustarte