/*Program for conversion of a decimal no into binary,octal,hexadecimal*/
import java.string.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class dec extends Frame implements ActionListener
{
int n,d,a,no,i;
int ans[]=new int[20];
String s;
String d="";
Button b1,b2,b3,b4,b5,b6;
TextField t1,t2,t3,t4;
public dec()
{
b1=new Button("ENTER NUMBER:");
b2=new Button("BINARY NUMBER:");
b3=new Button("OCTAL NUMBER:");
b4=new Button("HEXADECIMAL NUMBER:");
b5=new Button("RESET:");
b6=new Button("EXIT:");
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
t4=new TextField(20);
setLayout(new FlowLayout());
add(b1); add(t1); add(b2); add(t2); add(b3); add(t3); add(b4); add(t4);
add(b5); add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b2)
{
s=t1.getText();
a=Integer.parseInt(s);
no=a;
for(i=1;no!=0;i++,no/=2)
ans[i]=no%2;
for(i--;i>0;i--)
d+=ans[i];
if(t2.getText().equals(""))
t2.setText(d);
}
if(ae.getSource()==b3)
{
s=t1.getText();
no=a;
for(d="",no=a,i=1;no!=0;i++,no/=8)
ans[i]=no%8;
for(i--;i>0;i--)
d+=ans[i];
if(t3.getText().equals(""))
t3.setText(d);
}
if(ae.getSource()==b3)
{
s=t1.getText();
no=a;
for(d="",no=a,i=1;no!=0;i++,no/=16)
ans[i]=no%16;
for(i--;i>0;i--)
{
switch(ans[i])
{
case 10:d+="A"; break;
case 11:d+="B"; break;
case 12:d+="C"; break;
case 13:d+="D"; break;
case 14:d+="E"; break;
case 15:d+="F"; break;
default:d+=ans[i];
}
}
if(t4.getText().equals(""))
t4.setText(d);
}
if(ae.getSource()==b5)
{
t1.setText("");
}
if(ae.getSource()==b6)
{
System.exit(0);
}
}
public static void main(String args[])throws Exception
{
dec de=new dec();
de.setSize(200,400);
de.setVisible(true);
}
}
import java.string.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class dec extends Frame implements ActionListener
{
int n,d,a,no,i;
int ans[]=new int[20];
String s;
String d="";
Button b1,b2,b3,b4,b5,b6;
TextField t1,t2,t3,t4;
public dec()
{
b1=new Button("ENTER NUMBER:");
b2=new Button("BINARY NUMBER:");
b3=new Button("OCTAL NUMBER:");
b4=new Button("HEXADECIMAL NUMBER:");
b5=new Button("RESET:");
b6=new Button("EXIT:");
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);
t4=new TextField(20);
setLayout(new FlowLayout());
add(b1); add(t1); add(b2); add(t2); add(b3); add(t3); add(b4); add(t4);
add(b5); add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b2)
{
s=t1.getText();
a=Integer.parseInt(s);
no=a;
for(i=1;no!=0;i++,no/=2)
ans[i]=no%2;
for(i--;i>0;i--)
d+=ans[i];
if(t2.getText().equals(""))
t2.setText(d);
}
if(ae.getSource()==b3)
{
s=t1.getText();
no=a;
for(d="",no=a,i=1;no!=0;i++,no/=8)
ans[i]=no%8;
for(i--;i>0;i--)
d+=ans[i];
if(t3.getText().equals(""))
t3.setText(d);
}
if(ae.getSource()==b3)
{
s=t1.getText();
no=a;
for(d="",no=a,i=1;no!=0;i++,no/=16)
ans[i]=no%16;
for(i--;i>0;i--)
{
switch(ans[i])
{
case 10:d+="A"; break;
case 11:d+="B"; break;
case 12:d+="C"; break;
case 13:d+="D"; break;
case 14:d+="E"; break;
case 15:d+="F"; break;
default:d+=ans[i];
}
}
if(t4.getText().equals(""))
t4.setText(d);
}
if(ae.getSource()==b5)
{
t1.setText("");
}
if(ae.getSource()==b6)
{
System.exit(0);
}
}
public static void main(String args[])throws Exception
{
dec de=new dec();
de.setSize(200,400);
de.setVisible(true);
}
}
No comments:
Post a Comment