/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package frames;
import modelo.Complex;
import vista.SwingValidator;
import java.text.NumberFormat;
public class ComplexApp extends javax.swing.JFrame {
/**
* Creates new form OpGUI
*/
Complex co;
NumberFormat numero = NumberFormat.getNumberInstance();
public ComplexApp() {
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">//GEN-BEGIN:initComponents
private void initComponents() {
ml = new javax.swing.JLabel();
nl = new javax.swing.JLabel();
ny = new javax.swing.JTextField();
mx = new javax.swing.JTextField();
porl = new javax.swing.JLabel();
masl = new javax.swing.JLabel();
divl = new javax.swing.JLabel();
menosl2 = new javax.swing.JLabel();
menos = new javax.swing.JTextField();
mas = new javax.swing.JTextField();
por = new javax.swing.JTextField();
div = new javax.swing.JTextField();
calcular = new javax.swing.JButton();
limpiar = new javax.swing.JButton();
salir = new javax.swing.JButton();
nx = new javax.swing.JTextField();
my = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ComplexApp: 4 Operaciones");
ml.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
ml.setText("m");
nl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
nl.setText("n");
ny.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
nyKeyTyped(evt);
}
});
mx.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
mxKeyTyped(evt);
}
});
porl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
porl.setText("m * n:");
masl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
masl.setText("m + n:");
divl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
divl.setText("m / n:");
menosl2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
menosl2.setText("m - n:");
menos.setEditable(false);
menos.setBackground(new java.awt.Color(255, 255, 204));
menos.setFocusable(false);
mas.setEditable(false);
mas.setBackground(new java.awt.Color(255, 255, 204));
mas.setFocusable(false);
por.setEditable(false);
por.setBackground(new java.awt.Color(255, 255, 204));
por.setFocusable(false);
div.setEditable(false);
div.setBackground(new java.awt.Color(255, 255, 204));
div.setFocusable(false);
calcular.setMnemonic('C');
calcular.setText("Calcular");
calcular.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calcularActionPerformed(evt);
}
});
limpiar.setMnemonic('L');
limpiar.setText("Limpiar");
limpiar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
limpiarActionPerformed(evt);
}
});
salir.setMnemonic('S');
salir.setText("Salir");
salir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
salirActionPerformed(evt);
}
});
nx.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
nxKeyTyped(evt);
}
});
my.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
myKeyTyped(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(calcular)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(limpiar)
.addGap(18, 18, 18)
.addComponent(salir))
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(porl, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(masl, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(divl, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(menosl2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(div, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(menos)
.addComponent(por, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(mas, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(mx, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(my, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(nx, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ny, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(44, 44, 44))
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(ml, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(nl, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(92, 92, 92))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ml)
.addComponent(nl))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ny, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(mx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(my, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calcular)
.addComponent(limpiar)
.addComponent(salir))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(masl))
.addComponent(mas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(menosl2)
.addComponent(menos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(8, 8, 8)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(porl))
.addComponent(por, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(divl))
.addComponent(div, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(29, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void calcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcularActionPerformed
if(isValidData()){
float mxx = Float.parseFloat(mx.getText());
float myy = Float.parseFloat(my.getText());
float nxx = Float.parseFloat(nx.getText());
float nyy = Float.parseFloat(ny.getText());
Complex m1 = new Complex(mxx, myy);
Complex n1 = new Complex(nxx, nyy);
co = new Complex();
Complex mn = co.sumar(m1, n1);
mas.setText(numero.format(mn.getX()) + " + i" + numero.format(mn.getY()));
mn = co.restar(m1, n1);
menos.setText(numero.format(mn.getX()) + " + i" + numero.format(mn.getY()));
mn = co.multiplicar(m1, n1);
por.setText(numero.format(mn.getX()) + " + i" + numero.format(mn.getY()));
if(n1.getX()==0 || n1.getX()==0) div.setText("División entre 0");
else {
mn = co.dividir(m1, n1);
div.setText(numero.format(mn.getX()) + " + i" + numero.format(mn.getY()));
}
limpiar.requestFocusInWindow();
}
}//GEN-LAST:event_calcularActionPerformed
private void limpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limpiarActionPerformed
mx.setText("");
my.setText("");
nx.setText("");
ny.setText("");
mas.setText("");
menos.setText("");
por.setText("");
div.setText("");
mx.requestFocusInWindow();
}//GEN-LAST:event_limpiarActionPerformed
private void salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirActionPerformed
System.exit(0);
}//GEN-LAST:event_salirActionPerformed
private void mxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mxKeyTyped
char c = evt.getKeyChar();
if (c!='0' && c!='1' && c!='2' && c!='3' && c!='4' && c!='5' && c!='6' &&
c!='7' && c!='8' && c!='9' && c!='.' && c!='+' && c!='-') evt.consume();
if(c==10) my.requestFocusInWindow();
}//GEN-LAST:event_mxKeyTyped
private void nyKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nyKeyTyped
char c = evt.getKeyChar();
if (c!='0' && c!='1' && c!='2' && c!='3' && c!='4' && c!='5' && c!='6' &&
c!='7' && c!='8' && c!='9' && c!='.' && c!='+' && c!='-') evt.consume();
if(c==10) calcular.requestFocusInWindow();
}//GEN-LAST:event_nyKeyTyped
private void nxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nxKeyTyped
char c = evt.getKeyChar();
if (c!='0' && c!='1' && c!='2' && c!='3' && c!='4' && c!='5' && c!='6' &&
c!='7' && c!='8' && c!='9' && c!='.' && c!='+' && c!='-') evt.consume();
if(c==10) ny.requestFocusInWindow();
}//GEN-LAST:event_nxKeyTyped
private void myKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_myKeyTyped
char c = evt.getKeyChar();
if (c!='0' && c!='1' && c!='2' && c!='3' && c!='4' && c!='5' && c!='6' &&
c!='7' && c!='8' && c!='9' && c!='.' && c!='+' && c!='-') evt.consume();
if(c==10) nx.requestFocusInWindow();
}//GEN-LAST:event_myKeyTyped
private boolean isValidData(){
SwingValidator sv = new SwingValidator();
return sv.isPresent(mx, ml.getText()) &&
sv.isFloat(mx, ml.getText()) &&
sv.isPresent(my, ml.getText()) &&
sv.isFloat(my, ml.getText()) &&
sv.isPresent(nx, nl.getText()) &&
sv.isFloat(nx, nl.getText()) &&
sv.isPresent(ny, nl.getText()) &&
sv.isFloat(ny, nl.getText());
}
/**
* @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(ComplexApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ComplexApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ComplexApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ComplexApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ComplexApp().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton calcular;
private javax.swing.JTextField div;
private javax.swing.JLabel divl;
private javax.swing.JButton limpiar;
private javax.swing.JTextField mas;
private javax.swing.JLabel masl;
private javax.swing.JTextField menos;
private javax.swing.JLabel menosl2;
private javax.swing.JLabel ml;
private javax.swing.JTextField mx;
private javax.swing.JTextField my;
private javax.swing.JLabel nl;
private javax.swing.JTextField nx;
private javax.swing.JTextField ny;
private javax.swing.JTextField por;
private javax.swing.JLabel porl;
private javax.swing.JButton salir;
// End of variables declaration//GEN-END:variables
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package vista;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class SwingValidator {
public boolean isPresent(JTextComponent c, String fieldName){
if (c.getText().length() == 0) {
showMessage(c, fieldName + " es un campo requerido.");
c.requestFocusInWindow();
return false;
}
return true;
}
public boolean isInteger(JTextComponent c, String fieldName){
try{
int i = Integer.parseInt(c.getText());
return true;
} catch (NumberFormatException e){
showMessage(c, fieldName + " debe ser entero.");
c.requestFocusInWindow() ;
return false;
}
}
public boolean isgrather0(JTextComponent c, String fieldName){
if(Integer.parseInt(c.getText())>0) return true;
showMessage(c, fieldName + " debe ser mayor que 0.");
c.requestFocusInWindow() ;
return false;
}
public boolean isFloat(JTextComponent c, String fieldName) {
try{
double d = Float.parseFloat(c.getText());
return true;
} catch (NumberFormatException e){
showMessage(c, fieldName + " debe ser un número válido.");
c.requestFocusInWindow() ;
return false;
}
}
private void showMessage(JTextComponent c, String message){
JOptionPane.showMessageDialog(c, message, "Dato no válido", JOptionPane.ERROR_MESSAGE);
}
}
package modelo;
public class Complex {
private float x, y;
public Complex(){}
public Complex(float x, float y){
this.x = x;
this.y = y;
}
public float getX(){
return x;
}
public float getY(){
return y;
}
public Complex sumar(Complex m, Complex n){
return new Complex(m.x+n.x, m.y+n.y);
}
public Complex restar(Complex m, Complex n){
return new Complex(m.x-n.x, m.y-n.y);
}
public Complex multiplicar(Complex m, Complex n){
return new Complex(m.x*n.x - m.y*n.y, m.x*n.y + m.y*n.x);
}
public Complex dividir(Complex m, Complex n){
float div = n.x*n.x + n.y*n.y;
Complex d = new Complex(n.x,-n.y);
d = multiplicar(m, d);
d.x /= div;
d.y /= div;
return d;}
}