Documente Academic
Documente Profesional
Documente Cultură
1. Desarrolla una calculadora (Modo Gráfico) con las operaciones básicas incluyendo la
potencia de un número y la raíz cuadrada de un número, utilizando el lenguaje Java.
Código:
package calculadora;
/**
*/
Double resultado;
public frmCalculadora() {
initComponents();
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora");
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
btnmasmenos.setText("+ -");
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
btnmasmenos.addActionListener(new java.awt.event.ActionListener() {
btnmasmenosActionPerformed(evt);
});
btnraiz.setText("Raíz");
btnraiz.addActionListener(new java.awt.event.ActionListener() {
btnraizActionPerformed(evt);
});
btn7.setText("7");
btn7.addActionListener(new java.awt.event.ActionListener() {
btn7ActionPerformed(evt);
});
btn4.setText("4");
btn4.addActionListener(new java.awt.event.ActionListener() {
btn4ActionPerformed(evt);
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
});
btn1.setText("1");
btn1.addActionListener(new java.awt.event.ActionListener() {
btn1ActionPerformed(evt);
});
btn0.setText("0");
btn0.addActionListener(new java.awt.event.ActionListener() {
btn0ActionPerformed(evt);
});
btn1x.setText("1/x");
btn1x.addActionListener(new java.awt.event.ActionListener() {
btn1xActionPerformed(evt);
});
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
btn8.setText("8");
btn8.addActionListener(new java.awt.event.ActionListener() {
btn8ActionPerformed(evt);
});
btn5.setText("5");
btn5.addActionListener(new java.awt.event.ActionListener() {
btn5ActionPerformed(evt);
});
btn2.setText("2");
btn2.addActionListener(new java.awt.event.ActionListener() {
btn2ActionPerformed(evt);
});
btnx2.setText("x^2");
btnx2.addActionListener(new java.awt.event.ActionListener() {
btnx2ActionPerformed(evt);
});
btn9.setText("9");
btn9.addActionListener(new java.awt.event.ActionListener() {
btn9ActionPerformed(evt);
});
btn6.setText("6");
btn6.addActionListener(new java.awt.event.ActionListener() {
btn6ActionPerformed(evt);
});
btn3.setText("3");
btn3.addActionListener(new java.awt.event.ActionListener() {
btn3ActionPerformed(evt);
});
btnpunto.setText(".");
btnpunto.addActionListener(new java.awt.event.ActionListener() {
btnpuntoActionPerformed(evt);
});
btnxy.setText("x^y");
btnxy.addActionListener(new java.awt.event.ActionListener() {
btnxyActionPerformed(evt);
});
btnsuma.setText("+");
btnsuma.addActionListener(new java.awt.event.ActionListener() {
btnsumaActionPerformed(evt);
});
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
btnresta.setText("-");
btnresta.addActionListener(new java.awt.event.ActionListener() {
btnrestaActionPerformed(evt);
});
btnigual.setText("=");
btnigual.addActionListener(new java.awt.event.ActionListener() {
btnigualActionPerformed(evt);
});
btnex.setText("e^x");
btnex.addActionListener(new java.awt.event.ActionListener() {
btnexActionPerformed(evt);
});
btnborrar.setText("<--");
btnborrar.addActionListener(new java.awt.event.ActionListener() {
btnborrarActionPerformed(evt);
});
btnc.setText("C");
btnc.addActionListener(new java.awt.event.ActionListener() {
btncActionPerformed(evt);
});
btndividir.setText("/");
btndividir.addActionListener(new java.awt.event.ActionListener() {
btndividirActionPerformed(evt);
});
btnmultiplicar.setText("x");
btnmultiplicar.addActionListener(new java.awt.event.ActionListener() {
btnmultiplicarActionPerformed(evt);
});
getContentPane().add(btnmultiplicar, new
org.netbeans.lib.awtextra.AbsoluteConstraints(260, 150, -1, 40));
pack();
}// </editor-fold>
txtmostrar.setText(txtmostrar.getText()+"2");
txtmostrar.setText(txtmostrar.getText()+"4");
txtmostrar.setText(txtmostrar.getText()+"6");
txtmostrar.setText(txtmostrar.getText()+"5");
}
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
if(!txtmostrar.getText().equals("")){
valor1=txtmostrar.getText();
signo="/";
txtmostrar.setText("");
if(!txtmostrar.getText().equals("")){
valor1=txtmostrar.getText();
signo="*";
txtmostrar.setText("");
txtmostrar.setText(txtmostrar.getText()+"0");
txtmostrar.setText(txtmostrar.getText()+"1");
}
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
txtmostrar.setText(txtmostrar.getText()+"3");
txtmostrar.setText(txtmostrar.getText()+"7");
txtmostrar.setText(txtmostrar.getText()+"8");
txtmostrar.setText(txtmostrar.getText()+"9");
txtmostrar.setText("");
contenido=txtmostrar.getText();
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
if(contenido.length()<=0){
txtmostrar.setText("0.");
}else
if(txtmostrar.getText().contains(".")){
}else{
txtmostrar.setText(txtmostrar.getText()+".");
punto=false;
Double resultadocalc=0.0;
String respuesta;
if(signo.equals("+")){
resultadocalc=Double.parseDouble(valor1)+Double.parseDouble(valor2);
}else if(signo.equals("-")){
resultadocalc=Double.parseDouble(valor1)-Double.parseDouble(valor2);
}else if(signo.equals("*")){
resultadocalc=Double.parseDouble(valor1)*Double.parseDouble(valor2);
}else if (signo.equals("/")){
resultadocalc=Double.parseDouble(valor1)/Double.parseDouble(valor2);
}else if(signo.equals("x^y")){
resultadocalc=Math.pow(Double.parseDouble(valor1), Double.parseDouble(valor2));
respuesta=resultadocalc.toString();
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
return respuesta;
if(!txtmostrar.getText().equals("")){
valor1=txtmostrar.getText();
signo="+";
txtmostrar.setText("");
if(!txtmostrar.getText().equals("")){
valor1=txtmostrar.getText();
signo="-";
txtmostrar.setText("");
if(!txtmostrar.getText().equals("")){
valor1=txtmostrar.getText();
signo="x^y";
txtmostrar.setText("");
String resultadototal;
valor2=txtmostrar.getText();
if(!valor2.equals("")){
txtmostrar.setText(resultadototal);
contenido=txtmostrar.getText();
if(contenido.length()>0){
contenido=contenido.substring(0,contenido.length()-1);;
txtmostrar.setText(contenido);
contenido=txtmostrar.getText();
if(contenido.length()>0){
resultado=Math.sqrt(Double.parseDouble(contenido));
txtmostrar.setText((resultado.toString()));
contenido=txtmostrar.getText();
if(contenido.length()>0){
resultado=1/(Double.parseDouble(contenido));
txtmostrar.setText(resultado.toString());
contenido=txtmostrar.getText();
if(contenido.length()>0){
resultado=Math.pow(Double.parseDouble(contenido), 2);
txtmostrar.setText(resultado.toString());
contenido=txtmostrar.getText();
if(contenido.length()>0){
Nombre: Víctor Miguel Alonso Silva Matrícula: 1729690 Brigada: 409
resultado=Math.exp(Double.parseDouble(contenido));
txtmostrar.setText(resultado.toString());
/**
*/
//<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.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(frmCalculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
java.util.logging.Logger.getLogger(frmCalculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
java.util.logging.Logger.getLogger(frmCalculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
java.util.logging.Logger.getLogger(frmCalculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new frmCalculadora().setVisible(true);
});