java calculadora científica
TRANSCRIPT
![Page 1: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/1.jpg)
Calculadora Científica
Introducción: Este proyecto consiste en la creación de una calculadora científica utilizando funciones de la clase MATH de java que contiene funciones avanzadas de matemáticas que pueden ser utilizadas. Por otro lado, el presente proyecto intenta demostrar la forma de generar componentes en tiempo de ejecución, esta forma de crear componentes presenta ciertas ventajas como determinar con precisión la posición a colocar el componente, sin embargo, presenta también ciertas dificultades. Llamamos tiempo de diseño cuando en el formulario arrastramos los diferentes componentes y los colocamos en la posición deseada y con las propiedades deseadas (texto, tamaño, color, etc.), por otro lado, se llama tiempo de ejecución cuando se crea el componente e indican sus propiedades mediante líneas de código.
Clase MATH: La clase MATH encapsula a todas las operaciones matemáticas comunes a todos los lenguajes de programación. A diferencia de otras clases, esta es de tipo public para que pueda ser llamada desde cualquier parte de un programa, sin necesidad de importarla y sus métodos son del tipo static para evitar el instanciamiento de los mismos. Algunos de los métodos de esta clase son:
Métodos Acción
Math.pow(a, b) a elevada a la b
Math.sqrt(a) Raíz cuadrada
Math.cbrt(a) Raíz cúbica
Math.sin(a) Seno de a
Math.cos(a) Coseno de a
Math.tan(a) Tangente de a
Math.rdn Número aleatorio
Math.toRadians Convierte a radianes
Math.toDegrees Convierte a grados
Métodos Acción
Math.asin(a) Arco seno de a
Math.acos(a) Arco coseno de a
Math.atan(a) Arco tangente de a
Math.abs(a) Valor absoluto de a
Math.log(a) Logaritmo natural
Math.log10(a) Logaritmo base 10
Math.E Base del log natural
Math.PI Valor de pi
Math.signum(a) Multiplica a por -1
Creación de componentes en tiempo de ejecución: Para crear un componente en tiempo de ejecución deben seguirse ciertos pasos:
a) Reservar espacio en memoria. Esto se logra indicando el tipo de componente y
asignándole un nombre adecuado. Ejem: JButton BotonOK;
b) Crear el elemento mediante el instanciamiento. Esto es indicarle a Java que genere el
componente deseado. Ejem: BotonOK = new JButton(“OK”);
c) Darle las propiedades necesarias tales como texto a mostrar, tipo de letra, etc. Ejem:
BotonOK.setFont(new java.awt.Font("Arial", 0, 11));
BotonOK.setBounds(5, 200, 50, 35);
d) Finalmente hay que mostrar el componente en el formulario, para ello es más sencillo
cuando se ha creado un panel en el formulario. Ejem: jPanel.add(BotonOK);
A modo de ejemplo: JButton BotonEliminar;
BotonEliminar = new Button(“Eliminar”);
![Page 2: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/2.jpg)
BotonEliminar.setFont(new java.awt.Font("Arial", 0, 11));
jPanel1.add(BotonEliminar);
BotonEliminar.setBounds(12, 125, 59, 35);
BotonEliminar.addActionListener(new ButtonListener());
Donde se puede observer que se esta creando un boton cuyo nombre es BotonEliminar, el texto a mostrar es Eliminar, usa letra Arial de 11 puntos en formato simple (sin negritas o cursivas), ha sido adicionado al panel jPanel1, está ubicado en las coordenas x=12, y=125; tiene un ancho de 59 pixeles y una altura de 35 pixeles y que se le ha implementado un método de escucha ButtonListener() para determinar si ha sido pulsado o no.
Código fuente del proyecto Calculadora Científica: //Importando las clases para utilizar en los componentes del formulario
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
/*
* Creación de una calculadora cientifica para ejemplificar el uso de los
* eventos de la clase MATH de java.
*/
public class Cientifica extends javax.swing.JFrame
{
// Declaración de variables
String TextoBoton[];
Button Teclas[];
JTextField Vis1, Vis2, Vis3;
JLabel Etiqueta;
ButtonGroup radioGroup1;
JRadioButton RBSexag, RBCents, RBRadia;
int x=5, y=70, conta=0, Angulo;
double A, B, Rpta, M=0;
//Creación del formulario
public Cientifica()
{
initComponents();
//Redimencionando el formulario
setSize(325,465);
//Cargando los elementos en el formulario
IniciarComponentes();
}
private void IniciarComponentes()
{
Teclas = new Button[44];
//Colocando los cuadros de texto en el formulario
Vis1 = new JTextField();
jPanel1.add(Vis1);
Vis1.setBounds(5, 5, 310, 20);
Vis1.setFont(new java.awt.Font("Arial", 0, 12));
Vis1.setHorizontalAlignment(JTextField.RIGHT);
Vis1.setMargin(new Insets(0,5,0,5));
Vis1.setEditable(false);
Vis2 = new JTextField();
jPanel1.add(Vis2);
![Page 3: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/3.jpg)
Vis2.setBounds(5, 24, 310, 20);
Vis2.setFont(new java.awt.Font("Arial", 0, 12));
Vis2.setHorizontalAlignment(JTextField.RIGHT);
Vis2.setMargin(new Insets(0,5,0,5));
Vis2.setEditable(false);
Vis3 = new JTextField();
jPanel1.add(Vis3);
Vis3.setBounds(5, 43, 310, 20);
Vis3.setFont(new java.awt.Font("Arial", 0, 12));
Vis3.setHorizontalAlignment(JTextField.RIGHT);
Vis3.setMargin(new Insets(0,5,0,5));
Vis3.setEditable(false);
//Llamamos a la función que asigna el texto de cada una de los botones
TextoBotones();
//Creación y colocación de los botones
for (int i=0; i<4; i++)
{
for (int j = 0; j < 6; j++)
{
Teclas[conta] = new Button(TextoBoton[conta]);
Teclas[conta].setFont(new java.awt.Font("Arial", 0, 11));
jPanel1.add(Teclas[conta]);
Teclas[conta].setBounds(x,y,50,35);
Teclas[conta].addActionListener(new ButtonListener());
x += 52;
conta++;
}
x = 5;
y += 38;
}
y += 5;
x = 5;
for (int i=0; i<4; i++)
{
for (int j = 0; j<5; j++)
{
Teclas[conta] = new Button(TextoBoton[conta]);
Teclas[conta].setFont(new java.awt.Font("Arial", 0, 11));
jPanel1.add(Teclas[conta]);
Teclas[conta].setBounds(x,y, 59,35);
Teclas[conta].addActionListener(new ButtonListener());
x += 63;
conta++;
}
x = 5;
y += 38;
}
//Creación de los botones de radio
Etiqueta = new JLabel();
Etiqueta.setText("Eliga una opción para la medida angular");
jPanel1.add(Etiqueta);
Etiqueta.setFont(new java.awt.Font("Arial", 0, 12));
Etiqueta.setBounds(5, y, 250,25);
radioGroup1 = new ButtonGroup();
y +=25;
RBSexag = new JRadioButton("Sexagesimal", true);
radioGroup1.add(RBSexag);
RBSexag.setBounds (5, y, 100, 25);
RBSexag.setFont(new java.awt.Font("Arial", 0, 11));
jPanel1.add(RBSexag);
![Page 4: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/4.jpg)
RBCents = new JRadioButton("Centesimal", false);
radioGroup1.add(RBCents);
RBCents.setBounds (110, y, 100, 25);
RBCents.setFont(new java.awt.Font("Arial", 0, 11));
jPanel1.add(RBCents);
RBRadia = new JRadioButton("Radianes", false);
radioGroup1.add(RBRadia);
RBRadia.setBounds (215, y, 100, 25);
RBRadia.setFont(new java.awt.Font("Arial", 0, 11));
jPanel1.add(RBRadia);
}
private void TextoBotones()
{
TextoBoton = new String[44];
//Array que almacena las etiquetas de los botones
TextoBoton[0] = "nCr";
TextoBoton[1] = "nPr";
TextoBoton[2] = "!";
TextoBoton[3] = "^2";
TextoBoton[4] = "^3";
TextoBoton[5] = "^x";
TextoBoton[6] = "²√¯";
TextoBoton[7] = "³√¯";
TextoBoton[8] = "x√¯";
TextoBoton[9] = "1/x";
TextoBoton[10] = "Abs";
TextoBoton[11] = "π";
TextoBoton[12] = "log";
TextoBoton[13] = "10^x";
TextoBoton[14] = "ln";
TextoBoton[15] = "e^x";
TextoBoton[16] = "%";
TextoBoton[17] = "(-)";
TextoBoton[18] = "Sen";
TextoBoton[19] = "Cos";
TextoBoton[20] = "Tan";
TextoBoton[21] = "ASen";
TextoBoton[22] = "ACos";
TextoBoton[23] = "ATan";
TextoBoton[24] = "7";
TextoBoton[25] = "8";
TextoBoton[26] = "9";
TextoBoton[27] = "/";
TextoBoton[28] = "AC";
TextoBoton[29] = "6";
TextoBoton[30] = "5";
TextoBoton[31] = "4";
TextoBoton[32] = "X";
TextoBoton[33] = "Mem";
TextoBoton[34] = "3";
TextoBoton[35] = "2";
TextoBoton[36] = "1";
TextoBoton[37] = "-";
TextoBoton[38] = "RCM";
TextoBoton[39] = "0";
TextoBoton[40] = ".";
TextoBoton[41] = "Rnd";
TextoBoton[42] = "+";
TextoBoton[43] = "ENTER";
}
//Sección que permite determinar el comportamiento de cada boton al ser pulsado
class ButtonListener implements ActionListener
{ ButtonListener() { }
![Page 5: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/5.jpg)
public void actionPerformed(ActionEvent e)
{
//Botones para ingresar datos
if (e.getActionCommand().equals("0"))
Vis3.setText(Vis3.getText() + "0");
if (e.getActionCommand().equals("1"))
Vis3.setText(Vis3.getText() + "1");
if (e.getActionCommand().equals("2"))
Vis3.setText(Vis3.getText() + "2");
if (e.getActionCommand().equals("3"))
Vis3.setText(Vis3.getText() + "3");
if (e.getActionCommand().equals("4"))
Vis3.setText(Vis3.getText() + "4");
if (e.getActionCommand().equals("5"))
Vis3.setText(Vis3.getText() + "5");
if (e.getActionCommand().equals("6"))
Vis3.setText(Vis3.getText() + "6");
if (e.getActionCommand().equals("7"))
Vis3.setText(Vis3.getText() + "7");
if (e.getActionCommand().equals("8"))
Vis3.setText(Vis3.getText() + "8");
if (e.getActionCommand().equals("9"))
Vis3.setText(Vis3.getText() + "9");
if (e.getActionCommand().equals("."))
{ if (Vis3.getText()!=null)
Vis3.setText(Vis3.getText() + ".");
else
Vis3.setText(Vis3.getText() + "0.");
}
//Botones para realizar operaciones
if (e.getActionCommand().equals("AC"))
if (Vis3.getText().equals(""))
{
Vis1.setText(null);
Vis2.setText(null);
}
else
{
Vis3.setText("");
}
if (e.getActionCommand().equals("nCr"))
{
if (Vis3.getText().equals(""))
{
A = Integer.parseInt(Vis1.getText());
B = Integer.parseInt(Vis2.getText());
Vis1.setText(null);
Vis2.setText(null);
}
else
{
A = Integer.parseInt(Vis2.getText());
B = Integer.parseInt(Vis3.getText());
Vis2.setText(null);
Vis3.setText(null);
![Page 6: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/6.jpg)
}
Rpta = Fact(A)/(Fact(A - B) * Fact(B));
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("nPr"))
{
if (Vis3.getText().equals(""))
{
A = Integer.parseInt(Vis1.getText());
B = Integer.parseInt(Vis2.getText());
Vis1.setText(null);
}
else
{
A = Integer.parseInt(Vis2.getText());
B = Integer.parseInt(Vis3.getText());
Vis2.setText(null);
Vis3.setText(null);
}
Rpta = Fact(A)/(Fact(A - B));
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("!"))
{
if (Vis3.getText().equals(""))
A = Integer.parseInt(Vis2.getText());
else
A = Integer.parseInt(Vis3.getText());
Rpta = Fact(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("^2"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.pow(A, 2);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("^3"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.pow(A, 3);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("^x"))
{
![Page 7: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/7.jpg)
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis1.getText());
B = Double.parseDouble(Vis2.getText());
Vis1.setText(null);
}
else
{
A = Double.parseDouble(Vis2.getText());
B = Double.parseDouble(Vis3.getText());
}
Rpta = Math.pow(A, B);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("²√¯"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.sqrt(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("³√¯"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.cbrt(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("x√¯"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis1.getText());
B = Double.parseDouble(Vis2.getText());
Vis1.setText(null);
}
else
{
A = Double.parseDouble(Vis2.getText());
B = Double.parseDouble(Vis3.getText());
}
Rpta = Math.pow(A,(1/B));
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("1/x"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
![Page 8: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/8.jpg)
if (A!=0)
{
Rpta = 1/A;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
else
Vis3.setText("ERROR. El valor no puede ser cero");
}
if (e.getActionCommand().equals("Abs"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.abs(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("π"))
{
Vis3.setText("" + Math.PI);
}
if (e.getActionCommand().equals("log"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.log10(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("10^x"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.pow(10,A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("ln"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.log(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("e^x"))
{
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
![Page 9: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/9.jpg)
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.pow(Math.E,A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("%"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis1.getText());
B = Double.parseDouble(Vis2.getText());
Vis1.setText(null);
}
else
{
A = Double.parseDouble(Vis2.getText());
B = Double.parseDouble(Vis3.getText());
}
Rpta = A*B/100;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("(-)"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis2.getText());
Rpta = -1*A;
Vis2.setText("" + Rpta);
}
else
{
A = Double.parseDouble(Vis3.getText());
Rpta = -1*A;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
}
if (e.getActionCommand().equals("Sen"))
{
if (RBSexag.isSelected() == true)
Angulo = 1;
if (RBCents.isSelected() == true)
Angulo = 2;
if (RBRadia.isSelected() == true)
Angulo = 3;
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
if (Angulo == 1)
A = Math.toRadians(A);
else if (Angulo == 2)
A = Math.PI*A/200;
Rpta = Math.sin(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
![Page 10: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/10.jpg)
if (e.getActionCommand().equals("Cos"))
{
if (RBSexag.isSelected() == true)
Angulo = 1;
if (RBCents.isSelected() == true)
Angulo = 2;
if (RBRadia.isSelected() == true)
Angulo = 3;
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
if (Angulo == 1)
A = Math.toRadians(A);
else if (Angulo == 2)
A = Math.PI*A/200;
Rpta = Math.cos(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("Tan"))
{
if (RBSexag.isSelected() == true)
Angulo = 1;
if (RBCents.isSelected() == true)
Angulo = 2;
if (RBRadia.isSelected() == true)
Angulo = 3;
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
if (Angulo == 1)
A = Math.toRadians(A);
else if (Angulo == 2)
A = Math.PI*A/200;
Rpta = Math.tan(A);
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("ASen"))
{
if (RBSexag.isSelected() == true)
Angulo = 1;
if (RBCents.isSelected() == true)
Angulo = 2;
if (RBRadia.isSelected() == true)
Angulo = 3;
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.asin(A);
if (Angulo == 1)
Rpta = Math.toDegrees(Rpta);
![Page 11: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/11.jpg)
else if (Angulo == 2)
Rpta = Rpta*200/Math.PI;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("ACos"))
{
if (RBSexag.isSelected() == true)
Angulo = 1;
if (RBCents.isSelected() == true)
Angulo = 2;
if (RBRadia.isSelected() == true)
Angulo = 3;
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.acos(A);
if (Angulo == 1)
Rpta = Math.toDegrees(Rpta);
else if (Angulo == 2)
Rpta = Rpta*200/Math.PI;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("ATan"))
{
if (RBSexag.isSelected() == true)
Angulo = 1;
if (RBCents.isSelected() == true)
Angulo = 2;
if (RBRadia.isSelected() == true)
Angulo = 3;
if (Vis3.getText().equals(""))
A = Double.parseDouble(Vis2.getText());
else
A = Double.parseDouble(Vis3.getText());
Rpta = Math.atan(A);
if (Angulo == 1)
Rpta = Math.toDegrees(Rpta);
else if (Angulo == 2)
Rpta = Rpta*200/Math.PI;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("Rnd"))
{
if (Vis3.getText().equals(""))
Vis3.setText("" + Math.random());
else
{
Vis1.setText(Vis2.getText());
Vis2.setText(Vis3.getText());
Vis3.setText("" + Math.random());
}
![Page 12: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/12.jpg)
}
if (e.getActionCommand().equals("/"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis1.getText());
B = Double.parseDouble(Vis2.getText());
Vis1.setText(null);
}
else
{
A = Double.parseDouble(Vis2.getText());
B = Double.parseDouble(Vis3.getText());
}
Rpta = A/B;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("X"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis1.getText());
B = Double.parseDouble(Vis2.getText());
Vis1.setText(null);
}
else
{
A = Double.parseDouble(Vis2.getText());
B = Double.parseDouble(Vis3.getText());
}
Rpta = A*B;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("-"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis1.getText());
B = Double.parseDouble(Vis2.getText());
Vis1.setText(null);
}
else
{
A = Double.parseDouble(Vis2.getText());
B = Double.parseDouble(Vis3.getText());
}
Rpta = A-B;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("+"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis1.getText());
B = Double.parseDouble(Vis2.getText());
Vis1.setText(null);
}
![Page 13: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/13.jpg)
else
{
A = Double.parseDouble(Vis2.getText());
B = Double.parseDouble(Vis3.getText());
}
Rpta = A+B;
Vis2.setText("" + Rpta);
Vis3.setText(null);
}
if (e.getActionCommand().equals("Mem"))
{
if (Vis3.getText().equals(""))
{
A = Double.parseDouble(Vis2.getText());
M = A;
Vis2.setText(null);
}
else
{
A = Double.parseDouble(Vis3.getText());
M = A;
Vis3.setText(null);
}
}
if (e.getActionCommand().equals("RCM"))
{
if (!Vis3.getText().equals(""))
{
if(!Vis2.getText().equals(""))
{
Vis1.setText(Vis2.getText());
Vis2.setText(Vis3.getText());
}
else
{
Vis2.setText(Vis3.getText());
}
Vis3.setText("" + M);
}
else
Vis3.setText("" + M);
}
if (e.getActionCommand().equals("ENTER"))
{
if (!Vis3.getText().equals(""))
{
if(!Vis2.getText().equals(""))
{
Vis1.setText(Vis2.getText());
Vis2.setText(Vis3.getText());
}
else
{
Vis2.setText(Vis3.getText());
}
Vis3.setText(null);
}
}
}
}
//función que permite calcular el factorial de un número
public double Fact (double n)
![Page 14: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/14.jpg)
{
int resol = 1;
if (n>0)
{
for (int i=1; i<= n; i++)
resol *= i;
}
else if (n==0)
resol = 1;
else
Vis3.setText("ERROR");
return resol;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora Científica");
setResizable(false);
jPanel1.setBackground(new java.awt.Color(133, 184, 249));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Cientifica().setVisible(true);
}
});
}
![Page 15: JAVA Calculadora Científica](https://reader036.vdocuments.pub/reader036/viewer/2022081717/553634494a7959191d8b48bf/html5/thumbnails/15.jpg)
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}