/*
* 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 binary_add_subtract;
import java.lang.StringBuilder;
/**
*
* @author Lorilla
*/
public class binary_add_subtract_j extends javax.swing.JFrame {
/**
* Creates new form binary_add_subtract_j
*/
public binary_add_subtract_j() {
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() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jTextField3 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jTextField6 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Compute");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField4.setText("jTextField4");
jTextField5.setText("jTextField5");
jTextField5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField5ActionPerformed(evt);
}
});
jButton2.setText("Compute");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jTextField6.setText("jTextField6");
jLabel2.setText("Integer");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jLabel2))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField6)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField5)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jTextField3)
.addComponent(jTextField1)
.addComponent(jTextField2)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
.addContainerGap(123, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(9, 9, 9)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(40, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
int i;
int[] a = new int[8];
int[] b = new int[8];
int[] sum = new int[8];
String c, d, check_c, check_d;
Methods m = new Methods();
c = jTextField1.getText();
a = m.Converter(c);
d = jTextField2.getText();
b = m.Converter(d);
sum = m.BinaryAddition(a, b);
StringBuilder builder = new StringBuilder();
for (i = 0; i < 8; i++) {
builder.append(sum[i]);
}
String result = builder.toString();
String input0 = c;
String input1 = d;
// Use as radix 2 because it's binary
int number0 = Integer.parseInt(input0, 2);
int number1 = Integer.parseInt(input1, 2);
int sum1 = number0 + number1;
String result1 = Integer.toString(sum1, 2);
if (result.equals(result1)) {
jTextField3.setText(result);
} else {
jTextField3.setText("Overflow!! or Enter 8 bit only. Run Again: ");
}
int convert1 = Integer.parseInt(input0, 2);
jTextField4.setText(Integer.toString(convert1));
int convert2 = Integer.parseInt(input1, 2);
jTextField5.setText(Integer.toString(convert2));
int sum2 = convert1 + convert2;
String result2 = Integer.toString(sum2);
jTextField6.setText(result2);
}//GEN-LAST:event_jButton1ActionPerformed
private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField5ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField5ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
int i;
int num1;
String num2;
String num3;
Methods m = new Methods();
String num = jTextField5.getText();
num1 = Integer.parseInt((num));
num2 = convertBinary(num1);
jTextField2.setText(num2);
String num7;
String num4 = jTextField4.getText();
int num8 = Integer.parseInt(num4);
num7 = convertBinary(num8);
jTextField1.setText(num7);
int sum1 = num1 + num8;
jTextField6.setText(Integer.toString(sum1));
int[] sum = new int[8];
int[] a = new int[8];
int[] b = new int[8];
String c, d;
c = jTextField1.getText();
a = m.Converter(c);
d = jTextField2.getText();
b = m.Converter(d);
sum = m.BinaryAddition(a, b);
StringBuilder builder = new StringBuilder();
for (i = 0; i < 8; i++) {
builder.append(sum[i]);
}
String result = builder.toString();
}//GEN-LAST:event_jButton2ActionPerformed
public String convertBinary(int num){
int binary[] = new int[40];
int index = 0;
while(num > 0){
binary[index++] = num%2;
num = num/2;
}
String str;
StringBuilder builder = new StringBuilder();
for(int i = index-1;i >= 0;i--){
builder.append(String.format("%d", binary[i]));
}
String result = builder.toString();
return result;
}
class Methods {
int[] sum;
int[] BinaryAddition(int[] a, int[] b) {
sum = new int[8];
int cry = 0;
for (int i = 7; i >= 0; i--) {
if (cry == 0 && a[i] == 0 && b[i] == 0) {
sum[i] = 0;
cry = 0;
}
else if (cry == 0 && a[i] == 0 && b[i] == 1) {
sum[i] = 1;
cry = 0;
}
else if (cry == 0 && a[i] == 1 && b[i] == 0) {
sum[i] = 1;
cry = 0;
}
else if (cry == 0 && a[i] == 1 && b[i] == 1) {
sum[i] = 0;
cry = 1;
}
else if (cry == 1 && a[i] == 0 && b[i] == 0) {
sum[i] = 1;
cry = 0;
}
else if (cry == 1 && a[i] == 0 && b[i] == 1) {
sum[i] = 0;
cry = 1;
}
else if (cry == 1 && a[i] == 0 && b[i] == 0) {
sum[i] = 1;
cry = 0;
}
else if (cry == 1 && a[i] == 1 && b[i] == 1) {
sum[i] = 1;
cry = 1;
}
}
System.out.println("\n");
System.out.println("Sum : ");
jLabel1.setText(cry+"\t");
return (sum);
}
int[] Converter(String s) {
int[] a = new int[8];
if (s.length() != 8) {
jTextField3.setText("Enter 8 bit only. Run Again: ");
}
for (int i = 0; i < s.length(); i++) {
if ((s.charAt(i)) == '0') {
a[i] = 0;
} else if ((s.charAt(i)) == '1') {
a[i] = 1;
} else {
System.out.println("Error: Binary includes only 1 and 0. Run Again! ");
System.exit(1);
}
}
return a;
}
}
/**
* @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(binary_add_subtract_j.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(binary_add_subtract_j.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(binary_add_subtract_j.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(binary_add_subtract_j.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 binary_add_subtract_j().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
// End of variables declaration//GEN-END:variables
}
No comments:
Post a Comment