¡¿Qué hacer con una calculadora que sólo suma?!
Imaginemos que alguien nos da un calculadora que sólo tiene la funcionalidad de sumar y nos dice que quiere que reste, multiplique y divida.
Lo primero que se nos viene a la mente es mandarla a volar y crear una calculadora más "inteligente" nosotros mismos, pero lo que hay que hacer en este caso es aplicar INGENIERÍA INVERSA.
Vamos a empezar.
Este es el código de nuestra calculadora "sumadora":
import javax.swing.*;
import java.awt.event.*;
public class Formulario extends JFrame implements ActionListener {
private JTextField textfield1;
private JButton boton1, boton2;
private String cad1 = null, cad2= null;
public Formulario (){
setLayout(null);
textfield1= new JTextField();
textfield1.setBounds(10, 10, 100, 50);
add(textfield1);
boton1= new JButton("+");
boton1.setBounds(10, 100, 50, 50);
add(boton1);
boton2=new JButton ("=");
boton2.setBounds(70, 100, 50, 50);
add(boton2);
boton1.addActionListener(this);
boton2.addActionListener(this);
}
public void actionPerformed (ActionEvent e){
if (e.getSource()==boton1){
cad1= textfield1.getText();
textfield1.setText(null);
}
if (e.getSource()==boton2){
cad2= textfield1.getText();
int x= Integer.parseInt(cad1);
int y= Integer.parseInt(cad2);
int suma= x+y;
String resultado= String.valueOf(suma);
textfield1.setText(null);
textfield1.setText(resultado);
}
}
public static void main (String[]ar){
Formulario calc= new Formulario();
calc.setBounds(100, 100, 200, 200);
calc.setResizable(false);
calc.setVisible(true);
}
}
Iniciemos con la resta.
Lo primero que se piensa es "¿Cómo voy a restar con una suma?", pero la respuesta es más fácil de lo que se piensa, sólo bastará con esto:
int res= x+(-y);
La suma de un positivo con un negativo.
Ahora la multiplicación.
for(int i=0;i<x;i++)
tot+=y;
No es más que ir sumando 'x' veces el número 'y'. Demasiado fácil, ¿no?
Y por último, la división.
int A = 0;
int B = 0;
int tot = 0;
if(x>B){
while(A<x){
tot+=y;
B++;
if(tot>=x)
A+=x;
}
} else
if(x==y)
tot=1;
Un 'for' en dónde vaya sumando de 'y' en 'y' hasta que sea mayor a 'x', entonces regresa el mayor número entero positivo (eso es una gran ventaja, ya que no nos tendremos que pelear con los decimales).
Y dejo la imagen de la calculadora COMPLETA, con su código.
import javax.swing.*;
import java.awt.event.*;
public class Formulario extends JFrame implements ActionListener {
private JTextField textfield1;
private JButton boton1, boton2, boton3, boton4, boton5;
private String cad1 = null, cad2= null, cad3=null;
public Formulario (){
setLayout(null);
textfield1= new JTextField();
textfield1.setBounds(10, 10, 100, 50);
add(textfield1);
boton1= new JButton("+");
boton1.setBounds(10, 100, 50, 50);
add(boton1);
boton2=new JButton ("=");
boton2.setBounds(70, 100, 50, 50);
add(boton2);
boton3=new JButton ("-");
boton3.setBounds(10, 170, 50, 50);
add(boton3);
boton4=new JButton ("*");
boton4.setBounds(70, 170, 50, 50);
add(boton4);
boton5=new JButton ("/");
boton5.setBounds(10, 240, 50, 50);
add(boton5);
boton1.addActionListener(this);
boton2.addActionListener(this);
boton3.addActionListener(this);
boton4.addActionListener(this);
boton5.addActionListener(this);
}
public void actionPerformed (ActionEvent e){
if (e.getSource()==boton1){
cad1= textfield1.getText();
cad3= "+";
textfield1.setText(null);
}
if (e.getSource()==boton2){
cad2= textfield1.getText();
int x= Integer.parseInt(cad1);
int y= Integer.parseInt(cad2);
String resultado = "";
if(cad3=="+")
{
int suma= x+y;
resultado= String.valueOf(suma);
} else
if(cad3=="-")
{
int res= x+(-y);
resultado= String.valueOf(res);
} else
if(cad3=="*")
{
int tot = 0;
for(int i=0;i<x;i++)
tot+=y;
resultado= String.valueOf(tot);
} else
{
int A = 0;
int B = 0;
int tot = 0;
if(B<x){
while(A<x){
tot+=y;
B++;
if(tot>=x)
B+=x;
}
} else
if(x==y)
tot=1;
resultado= String.valueOf(tot);
}
textfield1.setText(null);
textfield1.setText(resultado);
}
if (e.getSource()==boton3){
cad1= textfield1.getText();
cad3= "-";
textfield1.setText(null);
}
if (e.getSource()==boton4){
cad1= textfield1.getText();
cad3= "*";
textfield1.setText(null);
}
if (e.getSource()==boton5){
cad1= textfield1.getText();
cad3= "/";
textfield1.setText(null);
}
}
public static void main (String[]ar){
Formulario calc= new Formulario();
calc.setBounds(100, 100, 170, 350);
calc.setResizable(false);
calc.setVisible(true);
}
}


No hay comentarios:
Publicar un comentario