I know how to convert binary to decimal...octal to decimal ... and vice versa as I have learned that prior taking this java programming class.
And I have no difficulty creating buttons n labels....that's the easiest part.
What I don't know is how to use the pow method for the conversion part as I am not gonna have any fix number....user will type lets say...01001 or 111000 .... so how do i capture the numbers from right to left n convert it to decimal?
I noe the conversion is like :
(0*16) + (1*8) + (0*4) + (0*2) +( 0*1)
and I tried using ifElse statements...but i think there's a much simpler n easier way to do....well...maybe i give u what i've done n can shed on some light on where i went wrong...
i m such a dummy in this i need to be knock on the head...
well here goes:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Numberpro extends
Applet implements ActionListener
{
Label first, one, two;
TextField satu, dua;
Button con;
int answer;//number that is entered in the textfield
double a1,a2,a4,a8,a16;//declare the 5 digit binary to decimal conversion power
//example:2^0=1 , 2^1=2 , 2^2=4 , 2^3=8 , 2^4=16
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,za,zb,zc,zd,ze;
//declare if 00000 , 00001 , 00010 then it becomes.....
String type="binary";//convert binary base to decimal
String type2="octal";//convert octal base to decimal
String type3="";
public void init()
{first=new Label ("Number System");
add(first);
one=new Label ("Enter the number : ");
add(one);
satu=new TextField(9);
add(satu);
satu.addActionListener(this);
two=new Label ("Enter the base system ");
add(two);
dua=new TextField(9);
add(dua);
dua.addActionListener(this);
con=new Button ("Convert");
add(con);
con.addActionListener(this);}
public void actionPerformed(ActionEvent event)
{answer=Integer.parseInt(event.getActionCommand());
repaint();
type3=dua.getText();
repaint();}
public static double pow(int a, int b)
{a1 = Math.pow(2.0, 0.0);
a2 = Math.pow(2.0, 1.0);
a4 = Math.pow(2.0, 2.0);
a8 = Math.pow(2.0, 3.0);
a16 = Math.pow(2.0, 4.0);}
public void paint(Graphics g)
{a=a1;
b=a2;
c=(a2+a1);
d=a4;
e=a4+a1;
f=a4+a2;
g=a4+a2+a1;
h=a8;
i=a8+a1;
j=a8+a2;
k=a8+a2+a1;
l=a8+a4;
m=a8+a4+a1;
n=a8+a4+a2;
o=a8+a4+a2+a1;
p=a16;
q=a16+a1;
r=a16+a2;
s=a16+a2+a1;
t=a16+a4;
u=a16+a4+a1;
v=a16+a4+a2;
w=a16+a4+a2+a1;
x=a16+a8;
y=a16+a8+a1;
z=a16+a8+a2;
za=a16+a8+a2+a1;
zb=a16+a8+a4;
zc=a16+a8+a4+a1;
zd=a16+a8+a4+a2;
ze=a16+a8+a4+a2+a1;}
if (type.equals(type3))
{g.drawString("answer " , 40, 55 );}
if (type=00000)
{g.drawString("Decimal Equivalent " + 0 , 60, 75 );}
else
if (type=00001)
{g.drawString("Decimal Equivalent " + a , 60, 75 );}
else
if (type=00010)
{g.drawString("Decimal Equivalent " + b , 60, 75 );}
else
if (type=00011)
{g.drawString("Decimal Equivalent " + c , 60, 75 );}
else
if (type=00100)
{g.drawString("Decimal Equivalent " + d , 60, 75 );}
else
if (type=00101)
{g.drawString("Decimal Equivalent " + e , 60, 75 );}
else
if (type=00110)
{g.drawString("Decimal Equivalent " + f , 60, 75 );}
else
if (type=00111)
{g.drawString("Decimal Equivalent " + g , 60, 75 );}
else
if (type=01000)
{g.drawString("Decimal Equivalent " + h , 60, 75 );}
else
if (type=01001)
{g.drawString("Decimal Equivalent " + i , 60, 75 );}
else
if (type=01010)
{g.drawString("Decimal Equivalent " + j , 60, 75 );}
else
if (type=01011)
{g.drawString("Decimal Equivalent " + k , 60, 75 );}
else
if (type=01100)
{g.drawString("Decimal Equivalent " + l , 60, 75 );}
else
if (type=01101)
{g.drawString("Decimal Equivalent " + m , 60, 75 );}
else
if (type=01110)
{g.drawString("Decimal Equivalent " + n , 60, 75 );}
else
if (type=01111)
{g.drawString("Decimal Equivalent " + o , 60, 75 );}
else
if (type=10000)
{g.drawString("Decimal Equivalent " + p , 60, 75 );}
else
if (type=10001)
{g.drawString("Decimal Equivalent " + q , 60, 75 );}
else
if (type=10010)
{g.drawString("Decimal Equivalent " + r , 60, 75 );}
else
if (type=10011)
{g.drawString("Decimal Equivalent " + s , 60, 75 );}
else
if (type=10100)
{g.drawString("Decimal Equivalent " + t , 60, 75 );}
else
if (type=10101)
{g.drawString("Decimal Equivalent " + u , 60, 75 );}
else
if (type=10110)
{g.drawString("Decimal Equivalent " + v , 60, 75 );}
else
if (type=10111)
{g.drawString("Decimal Equivalent " + w , 60, 75 );}
else
if (type=11000)
{g.drawString("Decimal Equivalent " + x , 60, 75 );}
else
if (type=11001)
{g.drawString("Decimal Equivalent " + y , 60, 75 );}
else
if (type=11010)
{g.drawString("Decimal Equivalent " + z , 60, 75 );}
else
if (type=11011)
{g.drawString("Decimal Equivalent " + za , 60, 75 );}
else
if (type=11100)
{g.drawString("Decimal Equivalent " + zb , 60, 75 );}
else
if (type=11101)
{g.drawString("Decimal Equivalent " + zc , 60, 75 );}
else
if (type=11110)
{g.drawString("Decimal Equivalent " + zd , 60, 75 );}
else
if (type=11111)
{g.drawString("Decimal Equivalent " + ze , 60, 75 );}
}
I have another question for you too:
1) For the TextField can i use ActionListener instead of TextListener....will it still work?