Friday, March 27, 2009

case 2











import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
import javax.swing.*;
import java.lang.*;
import java.util.*;


public class Conversion extends JFrame {

String text = "";
boolean enter = true;
JFrame h = new JFrame();
JPanel p6 = new JPanel();
JPanel p1 = new JPanel();

JLabel fnumber = new JLabel(" Number:");
JTextField num = new JTextField("", 20);

JLabel binary = new JLabel(" Conversion:");
JLabel bin = new JLabel("");

JButton add1 = new JButton("Binary");
JButton minus = new JButton("Decimal");
JButton multiply = new JButton("Octal");
JButton divide = new JButton("Hexadecimal");



public static void main(String args[]) {
Conversion Layout = new Conversion();
}

public Conversion( )
{

p1.setLayout(new GridLayout(2, 1));

p1.add(fnumber);
p1.add(num);
p1.add(binary);
p1.add(bin);

p1.add(add1);

add1.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
solution();
}
}
);

p1.add(minus);

minus.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text = num.getText();
String p = num.getText();
int number;
for(int i = 0; i < let =" p.charAt(i);" enter =" false;" enter ="=" number =" hexdecsolution();" enter =" true;" numbers =" Integer.parseInt(num.getText());" binaryconvert = "" i =" 0;">= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}

else
binaryconvert = binaryconvert + "0";
}

bin.setText(binaryconvert);
}

public void decimalsolution()
{
String numbers = num.getText();
int arrays[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16383 };
int binaryconvert = 0;
int len = numbers.length()-1;

num.setText(text);

for(int i = len, j = 0; i >= 0; i--)
{
if(numbers.charAt(j) == '1')
binaryconvert = arrays[i] + binaryconvert ;
j++;
}

bin.setText(String.valueOf(binaryconvert));
}

public void octalsolution()
{
int numbers = Integer.parseInt(num.getText());
int octal = 0, t = numbers, a, b;
String concat = "";

while(numbers!=0)
{
a = numbers / 8;
b = a * 8;
t = numbers - b;
numbers = a;
concat = String.valueOf(t) + concat;
}

bin.setText(concat);
}

public void hexasolution()
{
int numbers = Integer.parseInt(num.getText());
int octal = 0, t = numbers, a, b;
String concat = "";

while(numbers!=0)
{
a = numbers / 16;
b = a * 16;
t = numbers - b;
numbers = a;

if(t == 10)
concat = "A" + concat;
else if(t == 11)
concat = "B" + concat;
else if(t == 12)
concat = "C" + concat;
else if(t == 13)
concat = "D" + concat;
else if(t == 14)
concat = "E" + concat;
else if(t == 15)
concat = "F" + concat;
else
concat = String.valueOf(t) + concat;
}

bin.setText(concat);
}

public int hexdecsolution()
{
String concat = "", get = num.getText();
int num;

for(int i = 0; i < get.length(); i++)
{
if(get.charAt(i) == 'A')
concat = concat + "10";
else if(get.charAt(i) == 'B')
concat = concat + "11";
else if(get.charAt(i) == 'C')
concat = concat + "12";
else if(get.charAt(i) == 'D')
concat = concat + "13";
else if(get.charAt(i) == 'E')
concat = concat + "14";
else if(get.charAt(i) == 'F')
concat = concat + "15";
else
concat = concat + String.valueOf(get.charAt(i));
}
return Integer.valueOf(concat);
}

}

case 1













import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
import javax.swing.*;
import java.lang.*;
import java.util.*;


public class BinaryOperations extends JFrame {

JFrame h = new JFrame();
JPanel p6 = new JPanel();
JPanel p1 = new JPanel();


JLabel fnumber = new JLabel(" First Number:");
JTextField num = new JTextField("");

JLabel binary = new JLabel(" Binary:");
JLabel bin = new JLabel("");

JLabel snumber = new JLabel(" Second Number:");
JTextField num1 = new JTextField("");

JLabel binary1 = new JLabel(" Binary:");
JLabel bin1 = new JLabel("");

JLabel total = new JLabel(" Total:");
JTextField num2 = new JTextField("");

JLabel binary2 = new JLabel(" Binary:");
JLabel bin2 = new JLabel("");

JButton add1 = new JButton("+");
JButton minus = new JButton("-");
JButton multiply = new JButton("*");
JButton divide = new JButton("/");



public static void main(String args[]) {
BinaryOperations Layout = new BinaryOperations();
}

public BinaryOperations( )
{
p1.setLayout(new GridLayout(2, 1));
p6.setLayout(new GridLayout(2, 1));

p1.add(fnumber);
p1.add(num);

num.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int numbers = Integer.parseInt(num.getText());
int array[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
String binaryconvert = "";

for(int i = 0; i <>= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}

else
binaryconvert = binaryconvert + "0";
}

bin.setText(binaryconvert);
}
}
);


p1.add(binary);
p1.add(bin);


p1.add(snumber);
p1.add(num1);

num1.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int numbers = Integer.parseInt(num1.getText());
int array[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
String binaryconvert = "";

for(int i = 0; i <>= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}

else
binaryconvert = binaryconvert + "0";
}

bin1.setText(binaryconvert);
}
}
);


p1.add(binary1);
p1.add(bin1);

p6.add(total);
p6.add(num2);
p6.add(binary2);
p6.add(bin2);


p6.add(add1);

add1.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int number1 = Integer.parseInt(num.getText());
int number2 = Integer.parseInt(num1.getText());
int numbers = number1 + number2;

num2.setText(String.valueOf(numbers));

int array[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
String binaryconvert = "";

for(int i = 0; i <>= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}

else
binaryconvert = binaryconvert + "0";
}

bin2.setText(binaryconvert);

}
}
);

p6.add(minus);

minus.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int number1 = Integer.parseInt(num.getText());
int number2 = Integer.parseInt(num1.getText());
int numbers = number1 - number2;

num2.setText(String.valueOf(numbers));

int array[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
String binaryconvert = "";

for(int i = 0; i <>= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}

else
binaryconvert = binaryconvert + "0";
}

bin2.setText(binaryconvert);

}
}
);

p6.add(multiply);

multiply.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int number1 = Integer.parseInt(num.getText());
int number2 = Integer.parseInt(num1.getText());
int numbers = number1 * number2;

num2.setText(String.valueOf(numbers));

int array[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
String binaryconvert = "";

for(int i = 0; i <>= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}

else
binaryconvert = binaryconvert + "0";
}

bin2.setText(binaryconvert);

}
}
);

p6.add(divide);

divide.addActionListener
(

new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int number1 = Integer.parseInt(num.getText());
int number2 = Integer.parseInt(num1.getText());
int numbers = number1 / number2;

num2.setText(String.valueOf(numbers));

int array[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
String binaryconvert = "";

for(int i = 0; i <>= array[i])
{
numbers = numbers - array[i];
binaryconvert = binaryconvert + "1";
}

else
binaryconvert = binaryconvert + "0";
}

bin2.setText(binaryconvert);

}
}
);


add(p1, "North");

setSize(580, 150);
setTitle("Binary Conversion");
setVisible(true);
add(p6, "South");
}
}