//BinaryCalculator
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BinaryCalculator extends JFrame
{
Container con;
int flag;
int num1 , num2;
int num3,num4;
char array1[];
int array[];
JTextArea txtop;
JButton butadd;
JButton butequ;
JButton butcomplement;
JButton butsub;
public BinaryCalculator()
{
con = this.getContentPane();
con.setLayout(null);
txtop = new JTextArea(1,15);
txtop.setBounds(50,50,300,30);
con.add(txtop);
butadd = new JButton("+");
butadd.setBounds(50,120,50,30);
con.add(butadd);
butsub = new JButton("-");
butsub.setBounds(125,120,50,30);
con.add(butsub);
butcomplement = new JButton("~");
butcomplement.setBounds(200,120,50,30);
con.add(butcomplement);
butequ = new JButton("=");
butequ.setBounds(275,120,50,30);
con.add(butequ);
butadd.addActionListener(new w11());
butsub.addActionListener(new w11());
butcomplement.addActionListener(new w11());
butequ.addActionListener(new w11());
}
class w11 implements ActionListener
{
String number;
int res;
public void actionPerformed(ActionEvent ae)
{
JButton temp = (JButton)ae.getSource();
if( temp.getLabel().equals("+") )
{
number = txtop.getText();
if( (res=chkbinary(number))==1 )
{
flag = 1;
num1 = getDecimal(number);
try
{
num3 = Integer.parseInt(number);
}
catch(Exception exc23)
{
JOptionPane.showMessageDialog(con,"number is not in range");
}
txtop.setText(null);
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
}
else
if( temp.getLabel().equals("~") )
{
number = txtop.getText();
if( number.length() < 1 )
JOptionPane.showMessageDialog(con,"enter number");
else
{
flag = 2;
}
}
else
if( temp.getLabel().equals("-") )
{
number = txtop.getText();
if( (res = chkbinary(number) )==1 )
{
flag = 3;
num1 = getDecimal(number);
try
{
num3 = Integer.parseInt(number);
}
catch(Exception exc23)
{
JOptionPane.showMessageDialog(con,"number is not in range");
}
txtop.setText(null);
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
}
else
if( temp.getLabel().equals("=") )
{
switch(flag)
{
case 1: number = txtop.getText();
if( (res=chkbinary(number) )==1 )
{
num2 = getDecimal(number);
int num5 = num1+num2;
txtop.setText(Integer.toBinaryString(num5));
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
case 3: number = txtop.getText();
if( (res=chkbinary(number) )==1 )
{
num2 = getDecimal(number);
int num5 = num1-num2;
txtop.setText(Integer.toBinaryString(num5));
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
case 2:
number = txtop.getText();
txtop.setText(null);
if( ( res = chkbinary(number) )==1 )
{
for( int i = 0 ; i < number.length() ; i++ )
{
if( number.charAt(i) == '1')
array1[i] = '0';
else
array1[i] = '1';
txtop.append(Character.toString((char)array1[i]));
}
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
default:System.out.println ("in default");break;
}
}
}
public int getDecimal(String t)
{
int r, j=0;
int ans = 0;
array1 = t.toCharArray();
for( int i = (t.length()- 1); i >= 0 ; i--)
{
r = Integer.parseInt(""+t.charAt(i));
ans += r*(double)Math.pow((double)2,(double)j);
j++;
}
return ans;
}
public int chkbinary(String t)
{
array1 = t.toCharArray();
int i;
for( i = 0 ;i < t.length();i++)
{
if( array1[i] == '1' || array1[i] == '0')
{
}
else
break;
}
if( i == t.length())
return 1;
else return 0;
}
}
public static void main(String arg[])
{
BinaryCalculator obj = new BinaryCalculator();
obj.setVisible(true);
obj.setTitle("Binary Calculator");
obj.setExtendedState(MAXIMIZED_BOTH);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BinaryCalculator extends JFrame
{
Container con;
int flag;
int num1 , num2;
int num3,num4;
char array1[];
int array[];
JTextArea txtop;
JButton butadd;
JButton butequ;
JButton butcomplement;
JButton butsub;
public BinaryCalculator()
{
con = this.getContentPane();
con.setLayout(null);
txtop = new JTextArea(1,15);
txtop.setBounds(50,50,300,30);
con.add(txtop);
butadd = new JButton("+");
butadd.setBounds(50,120,50,30);
con.add(butadd);
butsub = new JButton("-");
butsub.setBounds(125,120,50,30);
con.add(butsub);
butcomplement = new JButton("~");
butcomplement.setBounds(200,120,50,30);
con.add(butcomplement);
butequ = new JButton("=");
butequ.setBounds(275,120,50,30);
con.add(butequ);
butadd.addActionListener(new w11());
butsub.addActionListener(new w11());
butcomplement.addActionListener(new w11());
butequ.addActionListener(new w11());
}
class w11 implements ActionListener
{
String number;
int res;
public void actionPerformed(ActionEvent ae)
{
JButton temp = (JButton)ae.getSource();
if( temp.getLabel().equals("+") )
{
number = txtop.getText();
if( (res=chkbinary(number))==1 )
{
flag = 1;
num1 = getDecimal(number);
try
{
num3 = Integer.parseInt(number);
}
catch(Exception exc23)
{
JOptionPane.showMessageDialog(con,"number is not in range");
}
txtop.setText(null);
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
}
else
if( temp.getLabel().equals("~") )
{
number = txtop.getText();
if( number.length() < 1 )
JOptionPane.showMessageDialog(con,"enter number");
else
{
flag = 2;
}
}
else
if( temp.getLabel().equals("-") )
{
number = txtop.getText();
if( (res = chkbinary(number) )==1 )
{
flag = 3;
num1 = getDecimal(number);
try
{
num3 = Integer.parseInt(number);
}
catch(Exception exc23)
{
JOptionPane.showMessageDialog(con,"number is not in range");
}
txtop.setText(null);
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
}
else
if( temp.getLabel().equals("=") )
{
switch(flag)
{
case 1: number = txtop.getText();
if( (res=chkbinary(number) )==1 )
{
num2 = getDecimal(number);
int num5 = num1+num2;
txtop.setText(Integer.toBinaryString(num5));
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
case 3: number = txtop.getText();
if( (res=chkbinary(number) )==1 )
{
num2 = getDecimal(number);
int num5 = num1-num2;
txtop.setText(Integer.toBinaryString(num5));
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
case 2:
number = txtop.getText();
txtop.setText(null);
if( ( res = chkbinary(number) )==1 )
{
for( int i = 0 ; i < number.length() ; i++ )
{
if( number.charAt(i) == '1')
array1[i] = '0';
else
array1[i] = '1';
txtop.append(Character.toString((char)array1[i]));
}
}
else
{
JOptionPane.showMessageDialog(con,"not a valid number");
txtop.setText(null);
}
break;
default:System.out.println ("in default");break;
}
}
}
public int getDecimal(String t)
{
int r, j=0;
int ans = 0;
array1 = t.toCharArray();
for( int i = (t.length()- 1); i >= 0 ; i--)
{
r = Integer.parseInt(""+t.charAt(i));
ans += r*(double)Math.pow((double)2,(double)j);
j++;
}
return ans;
}
public int chkbinary(String t)
{
array1 = t.toCharArray();
int i;
for( i = 0 ;i < t.length();i++)
{
if( array1[i] == '1' || array1[i] == '0')
{
}
else
break;
}
if( i == t.length())
return 1;
else return 0;
}
}
public static void main(String arg[])
{
BinaryCalculator obj = new BinaryCalculator();
obj.setVisible(true);
obj.setTitle("Binary Calculator");
obj.setExtendedState(MAXIMIZED_BOTH);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}