Search This Blog

Decimal to BCD Converter in Java

In this post,we will see java program to convert decimal number to BCD representation. Binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, usually four. Thus the decimal digit 1 corresponds to  0001, 2 corresponds to 0010,....  8 corresponds to 1000 and 9 corresponds to 1001. Therefore, decimal number 194 can be represented in BCD as 0001 1001 0100. It should be noted that 1010, 1011, 1100, 1101,1110 and 1111 are illegal in BCD. The following java program converts any decimal number to BCD. But in this program, for conversion each digit in the decimal, we use Integer.toBinaryString() method. So, i have added another program which does not use this method.

Program 1:


    public static String toBCD(int num)
    {
        String BCD="";
        while(num!=0)
        {
        int t=num%10;
        String tBCD=Integer.toBinaryString(t);
        while(tBCD.length()<4)
            {
            tBCD="0"+tBCD;
            }
        BCD=tBCD+BCD;
        num/=10;
        }
        return BCD;
    }


Program 2:


import java.util.Scanner;
public class DecimalToBCD {

static String digitToBcd(int digit)
{
    String str="";
    for(int i=3;i>=0;i--)
        {
        int d=(int) Math.pow(2,i);
        if(digit/d!=0)
            {
                str+="1";
                digit-=d;
            }
        else
            str+="0";
        }
    return str;
}
    
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int decimal=sc.nextInt();
        int digit;
        String BCD="";
        while(decimal!=0)
            {
            digit=decimal%10;
            BCD=digitToBcd(digit)+" "+BCD;
            decimal/=10;
            }
        System.out.println("BCD is:"+BCD);
        
    }
}


The second program does not use any built in methods for conversion of decimal to BCD.

Java Program to Simulate Motion of Simple Pendulum

This is a java program to simulate motion of simple pendulum using java graphics. This was a problem given in computer graphics lab, which i was supposed to do using graphics.h header file in C. This is the java version of the same. If you are looking for a C Program, here it is: C Program to Simulate Simple Pendulum Motion - Computer Graphics Lab.

Output is as follows:



import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;


public class JavaApplication1 extends JFrame{

    int pivotx,pivoty;
    double thetamax,theta;;
    double len=260;
    int x,y,ymax,xmax;
    int bobradius=30;
    int xsign=-1,ysign=1;
    double omega;
    
    public JavaApplication1()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600,400);
        thetamax=60*Math.PI/180;
        pivotx=getWidth()/2;
        pivoty=40;
        ymax=(int) (pivoty+len*Math.cos(thetamax));
        xmax=(int) (pivotx+len*Math.sin(thetamax));
        x=xmax;
        y=ymax;
        theta=thetamax;
        System.out.println("pivotx:"+pivotx+" pivoty:"+pivoty);
        System.out.println("starting x:"+x+ "y:"+y);
        System.out.println("starting theta="+theta);
        t.start();
    }

    @Override
    public void paint(Graphics g) {
        System.out.println("called");        
        g.setColor(Color.WHITE);
        g.fillRect(0, 0,getWidth(),getHeight());
        g.setColor(Color.red);
        g.fillOval(pivotx-4,pivoty-4,8,8);
        g.drawLine(pivotx,pivoty, x,y);
        g.fillOval(x-bobradius/2,y-bobradius/2,bobradius,bobradius);
        
    }
    
    Thread t=new Thread()
    {
      public void run()
      {
    try {
      while(true)
      {
      if(x>=pivotx+Math.abs(len*Math.sin(thetamax)))
        {
        System.out.println("right extreme");
        xsign=-1;
        ysign*=-1;
        x=xmax-1;
        Thread.sleep(40);
        }
      else if(x<=pivotx-Math.abs(len*Math.sin(thetamax)))
      {
          System.out.println("left extreme");
          ysign*=-1;
          xsign=1;
          x=(int) (pivotx-Math.abs(len*Math.sin(thetamax))+2);
          Thread.sleep(40);
      }
      else if(y>=pivoty+len)
        {
              ysign*=-1;
              System.out.println("mean position");
        }

      omega=y/60*Math.PI/180;
      double decr=xsign*omega;
          System.out.println("decrement:"+decr);
      theta=theta+decr;
      x=(int) (pivotx+len*Math.sin(theta));
      y=(int) (pivoty+len*Math.cos(theta));
          System.out.println("new theta:"+theta);
      repaint();
      Thread.sleep(40);
    
      }
            } catch (InterruptedException ex) {}
      }
    };
    
    
    public static void main(String[] args) {
        new JavaApplication1().setVisible(true);
    }
    
}