Senhores, pesquisei bastante aqui no fórum e não achei uma solução. Estou criando uma calculadora e preciso de algum método que faça divisão de valores em hexadecimal.
Segue o codigo.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculadora;
/**
*
* @author Thayane
*/
public class JFCalc extends javax.swing.JFrame {
char operacao;
String temp, numeroAntes;
double num1,num2,resultado;
/**
* Creates new form JFCalc
*/
public JFCalc() {
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() {
jButton21 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jTVisor = new javax.swing.JTextField();
jBBackspace = new javax.swing.JButton();
jB7 = new javax.swing.JButton();
jB4 = new javax.swing.JButton();
jB1 = new javax.swing.JButton();
jB0 = new javax.swing.JButton();
jBvirgula = new javax.swing.JButton();
jB2 = new javax.swing.JButton();
jB5 = new javax.swing.JButton();
jB8 = new javax.swing.JButton();
jBCE = new javax.swing.JButton();
jBC = new javax.swing.JButton();
jB9 = new javax.swing.JButton();
jB6 = new javax.swing.JButton();
jB3 = new javax.swing.JButton();
jBadicao = new javax.swing.JButton();
jBresultado = new javax.swing.JButton();
jBsubtracao = new javax.swing.JButton();
jBmultiplicacao = new javax.swing.JButton();
jBdivisao = new javax.swing.JButton();
jBTrocaSinal = new javax.swing.JButton();
jButton21.setText("jButton21");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora");
jBBackspace.setText("DEL");
jBBackspace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBBackspaceActionPerformed(evt);
}
});
jB7.setText("7");
jB7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB7ActionPerformed(evt);
}
});
jB4.setText("4");
jB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB4ActionPerformed(evt);
}
});
jB1.setText("1");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB1ActionPerformed(evt);
}
});
jB0.setText("0");
jB0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB0ActionPerformed(evt);
}
});
jBvirgula.setText(",");
jBvirgula.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBvirgulaActionPerformed(evt);
}
});
jB2.setText("2");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB2ActionPerformed(evt);
}
});
jB5.setText("5");
jB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB5ActionPerformed(evt);
}
});
jB8.setText("8");
jB8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB8ActionPerformed(evt);
}
});
jBCE.setText("CE");
jBCE.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCEActionPerformed(evt);
}
});
jBC.setText("C");
jBC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCActionPerformed(evt);
}
});
jB9.setText("9");
jB9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB9ActionPerformed(evt);
}
});
jB6.setText("6");
jB6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB6ActionPerformed(evt);
}
});
jB3.setText("3");
jB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB3ActionPerformed(evt);
}
});
jBadicao.setText("+");
jBadicao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBadicaoActionPerformed(evt);
}
});
jBresultado.setText("=");
jBresultado.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBresultadoActionPerformed(evt);
}
});
jBsubtracao.setText("-");
jBsubtracao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBsubtracaoActionPerformed(evt);
}
});
jBmultiplicacao.setText("*");
jBmultiplicacao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBmultiplicacaoActionPerformed(evt);
}
});
jBdivisao.setText("/");
jBdivisao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBdivisaoActionPerformed(evt);
}
});
jBTrocaSinal.setText("-/+");
jBTrocaSinal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBTrocaSinalActionPerformed(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.TRAILING, false)
.addComponent(jTVisor)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBmultiplicacao))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBsubtracao))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jBBackspace)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBCE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBC)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBTrocaSinal))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB0)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBvirgula)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBadicao)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBresultado))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBdivisao))))
.addContainerGap(18, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jB0, jB1, jB2, jB3, jB4, jB5, jB6, jB7, jB8, jB9, jBBackspace, jBC, jBCE, jBTrocaSinal, jBadicao, jBdivisao, jBmultiplicacao, jBresultado, jBsubtracao, jBvirgula});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jTVisor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBBackspace)
.addComponent(jBCE)
.addComponent(jBC)
.addComponent(jBTrocaSinal))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB7)
.addComponent(jB8)
.addComponent(jB9)
.addComponent(jBdivisao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB4)
.addComponent(jB5)
.addComponent(jB6)
.addComponent(jBmultiplicacao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB1)
.addComponent(jB2)
.addComponent(jB3)
.addComponent(jBsubtracao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB0)
.addComponent(jBvirgula)
.addComponent(jBadicao)
.addComponent(jBresultado))
.addContainerGap(28, 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.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jB0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB0ActionPerformed
numeroDigitado("0");
}//GEN-LAST:event_jB0ActionPerformed
private void jBvirgulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBvirgulaActionPerformed
numeroDigitado(".");
}//GEN-LAST:event_jBvirgulaActionPerformed
private void jB1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB1ActionPerformed
numeroDigitado("1");
}//GEN-LAST:event_jB1ActionPerformed
private void jB2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB2ActionPerformed
numeroDigitado("2");
}//GEN-LAST:event_jB2ActionPerformed
private void jB3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB3ActionPerformed
numeroDigitado("3");
}//GEN-LAST:event_jB3ActionPerformed
private void jB4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB4ActionPerformed
numeroDigitado("4");
}//GEN-LAST:event_jB4ActionPerformed
private void jB5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB5ActionPerformed
numeroDigitado("5");
}//GEN-LAST:event_jB5ActionPerformed
private void jB6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB6ActionPerformed
numeroDigitado("6");
}//GEN-LAST:event_jB6ActionPerformed
private void jB7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB7ActionPerformed
numeroDigitado("7");
}//GEN-LAST:event_jB7ActionPerformed
private void jB8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB8ActionPerformed
numeroDigitado("8");
}//GEN-LAST:event_jB8ActionPerformed
private void jB9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB9ActionPerformed
numeroDigitado("9");
}//GEN-LAST:event_jB9ActionPerformed
private void jBadicaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBadicaoActionPerformed
escolherOperacao('+');
}//GEN-LAST:event_jBadicaoActionPerformed
private void escolherOperacao(char op) throws NumberFormatException {
operacao = op;
num1 = Double.parseDouble(jTVisor.getText());
jTVisor.setText("");
}
private void jBresultadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBresultadoActionPerformed
num2 = Double.parseDouble(jTVisor.getText());
switch (operacao){
case '+':
resultado = num1 + num2;
break;
case '-':
resultado = num1 - num2;
break;
case '*':
resultado = num1 * num2;
break;
case '/':
resultado = num1 / num2;
break;
}
jTVisor.setText(String.valueOf(resultado));
}//GEN-LAST:event_jBresultadoActionPerformed
private void jBsubtracaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBsubtracaoActionPerformed
escolherOperacao('-');
}//GEN-LAST:event_jBsubtracaoActionPerformed
private void jBmultiplicacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBmultiplicacaoActionPerformed
escolherOperacao('*');
}//GEN-LAST:event_jBmultiplicacaoActionPerformed
private void jBdivisaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBdivisaoActionPerformed
escolherOperacao('/');
}//GEN-LAST:event_jBdivisaoActionPerformed
private void jBCEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCEActionPerformed
num2 = 0;
jTVisor.setText(String.valueOf(num1));
}//GEN-LAST:event_jBCEActionPerformed
private void jBCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCActionPerformed
num1 = 0;
num2 = 0;
jTVisor.setText("");
}//GEN-LAST:event_jBCActionPerformed
private void jBBackspaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBackspaceActionPerformed
temp = numeroAntes;
jTVisor.setText(temp);
}//GEN-LAST:event_jBBackspaceActionPerformed
private void jBTrocaSinalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTrocaSinalActionPerformed
if (temp.startsWith("-")){
temp = temp.replace('-', '+' );
} else if (temp.startsWith("+")){
temp = temp.replace('+', '-' );
} else {
temp = '-' + temp;
}
jTVisor.setText(temp);
}//GEN-LAST:event_jBTrocaSinalActionPerformed
private void numeroDigitado(String numero) {
temp = jTVisor.getText();
numeroAntes = temp;
temp = temp + numero;
jTVisor.setText(temp);
}
/**
* @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(JFCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFCalc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFCalc.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 JFCalc().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jB0;
private javax.swing.JButton jB1;
private javax.swing.JButton jB2;
private javax.swing.JButton jB3;
private javax.swing.JButton jB4;
private javax.swing.JButton jB5;
private javax.swing.JButton jB6;
private javax.swing.JButton jB7;
private javax.swing.JButton jB8;
private javax.swing.JButton jB9;
private javax.swing.JButton jBBackspace;
private javax.swing.JButton jBC;
private javax.swing.JButton jBCE;
private javax.swing.JButton jBTrocaSinal;
private javax.swing.JButton jBadicao;
private javax.swing.JButton jBdivisao;
private javax.swing.JButton jBmultiplicacao;
private javax.swing.JButton jBresultado;
private javax.swing.JButton jBsubtracao;
private javax.swing.JButton jButton21;
private javax.swing.JButton jBvirgula;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTVisor;
// End of variables declaration//GEN-END:variables
}