Archive for August 20th, 2007
Number Conversion
Enter decimal number in the text field. When Calculate button clicked, It will display binary, octal and hexadecimal equivalents.
// By Santosh Wadghule.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberConversion extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4,n1,n2;
JTextField tf1,tf2,tf3,tf4;
JButton b1,b2;
JPanel p1,p2;
JFrame frame;
NumberConversion () //constuctor
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n1 = new JLabel("Created BY :",JLabel.CENTER);
n2 = new JLabel("Santosh Wadghule",JLabel.CENTER);
l1 = new JLabel("Decimal Number",JLabel.CENTER);
l2 = new JLabel("Binary Number",JLabel.CENTER);
l3 = new JLabel("Octal Number",JLabel.CENTER);
l4 = new JLabel("Hexadecimal Number",JLabel.CENTER);
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
b1 = new JButton("Calculate");
b2 = new JButton("Clear");
frame.setTitle("Number Conversion");
frame.setLayout(new GridLayout(6,2));
frame.setSize(300,300);
frame.add(n1);
frame.add(n2);
frame.add(l1);
frame.add(tf1);
frame.add(l2);
frame.add(tf2);
frame.add(l3);
frame.add(tf3);
frame.add(l4);
frame.add(tf4);
frame.add(b1);
frame.add(b2);
frame.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
} // end designing part
public static String toBinary(int n)
{
StringBuffer s = new StringBuffer(16);
while(n!=0)
{
s.append(n%2);
n=n/2;
}
return s.reverse().toString();
}
public static String toOctal(int n)
{
StringBuffer s = new StringBuffer(16);
while(n!=0)
{
s.append(n%8);
n=n/8;
}
return s.reverse().toString();
}
public static String toHex(int n)
{
StringBuffer s = new StringBuffer(16);
while(n!=0)
{
if(n%16>=10)
s.append((char)(55+n%16));//conversion for character
else
s.append(n%16);
n=n/16;
}
return s.reverse().toString();
}
public void actionPerformed(ActionEvent ae)
{ JButton b = (JButton)ae.getSource();
if(b==b1)
{
int n = Integer.parseInt(tf1.getText());
tf2.setText(toBinary(n));
tf3.setText(toOctal(n));
tf4.setText(toHex(n));
}
if(b==b2)
{
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf1.requestFocus();
}
}
public static void main(String args[])
{
new NumberConversion();
}
}
To-do list Source code on PSC
Today, I have uploded source code of ToDoList on Planet-Source-Code sites for beginners. This is very simple code for understand so everyone can imlpement this code. You can download it from by this link
Download



