Saturday, March 28, 2009

case1











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 {

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

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

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("Hexidecimal");



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)
{
decimalsolution();
}
}
);


p1.add(multiply);
p1.add(divide);

add(p1, "North");

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

}

public void solution()
{
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);
}

public void decimalsolution()
{
String numbers = num.getText();
int binaryconvert = 1;

for(int i = 0; i < numbers.length(); i++)
{
if(numbers.charAt(i) == '1')
binaryconvert = binaryconvert ;
else
binaryconvert = binaryconvert + binaryconvert;
}

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

}

No comments:

Post a Comment