Sunday, November 27

ANDROID GRAPHICS APPLICATION ~ BASIC CODES


 This post include:
  • Complete code for a simple graphics application in Android.
  • Concept of process in Android.
  • Use of Thread in Android programming.


Welcome friend,

 Here I'm posting the basic code for a simple Graphics application in Android ~ a "WELCOME-SCREEN"..!!

For any application, welcome screen is a must .(remember any game starts with an attractive welcome note,isnt it?).So chose to start graphics coding with craeting a welcome screen.

 summary of application::
  • When we open this application, a welcome screen will splash out and stay for a while.
  • We can also close this screen by pressing DOWN key.
  • Followed by this welcome screen we can place starting activity of our apllication.
Get..!! Set..!! GO..!!

Well,first create a new Android project in your Eclipse platform.
Let us start with MySpalsh.java

  1. public class MySplash extends Activity {
  2. private Thread myThread;
  3. protected int  SplashTime = 8000;

At first, Within the class we defined a thread <2> to display the welcome screen for a default time predefined by <3>

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.splash);
  4. final MySplash SplashProcess = this;

Within the onCreate function, (which will be active on starting) we set the screen as splash.xml<3>. we should link the welcome image to this xml file. we also created one process-instance <4> .

  1. myThread = new Thread() {
  2.          @Override
  3.           public void run() {
  4.               try {            
  5.                synchronized(this){
  6.                 wait(SplashTime);
  7.                }
  8.                 } catch(InterruptedException e) {}

Followed by this we define a thread to display the welcome image.<1> @override provision should also given<2>. we know that, in a Thread the code-fragment within run() will be executed<3>.Here<5> the keyword synchronised is used to get synchronisation among threads(i.e., at a time only one thread will be active).Each Thread will "wait" for the default wait-time<6>

  1.  finally {
  2.   finish();
  3.   Intent i = new Intent();
  4.                  i.setClass(SplashProcess, page2.class);
  5.            startActivity(i);
  6.               stop();
  7.              }
  8.          }
  9.         };
After this we define finally, specifying , after the working of Threads(i.e, when welcome image vanishes) what should do. finish() indicate<2> scope of Threads ends.Next activity/page (here page2.class)is started<5>. stop() is used to kill all the Threads created <6>

 myThread.start();

After Thraed definition we should call start() function to start Thread excecution.

  1. public boolean onTouchEvent(MotionEvent event) {
  2.     if (event.getAction() == MotionEvent.ACTION_DOWN) {
  3.      synchronized(myThread){
  4.       myThread.notifyAll();
  5.      }
  6.    }
  7.     return true;}
If we have to close welcome screen (without waiting till it ends) add this code. It detects whteher DOWN key is pressed<2>, if so, do notifyAll() function <4>.

The complete code is as follows:


MySplash.java


package com.VipinNarayan.splash;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.MotionEvent;
public class MySplash extends Activity {
protected int  SplashTime = 8000;
private Thread myThread;
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        final MySplash SplashProcess = this;
        myThread = new Thread() {
         @Override
          public void run() {
              try {            
               synchronized(this){
                wait(SplashTime);
               }
               } catch(InterruptedException e) {}
             finally {
                 finish();
                Intent i = new Intent();
                 i.setClass(SplashProcess, page2.class);
           startActivity(i);
                 stop();
            }
         }
        };
      myThread.start();
 }
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
     synchronized(myThread){
      myThread.notifyAll();
     }
   }
 return true;
}
}


page2.java

package com.VipinNarayan.splash;
import android.app.Activity;
import android.os.Bundle;
public class page2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


splash.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<ImageView android:src="@drawable/android" android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/ImageView01"></ImageView>
</LinearLayout>

Thursday, November 24

Disable Java Update Notification





Fed up with getting prompted to update Java, or if you have some web application like Banner that requires a particular version??? You can use a simple registry hack to disable notification of available updates...!!!


(1). Open the Registry Editor by going to the Start button and typing in regedt32.

(2).Navigate through to the following key: HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Update\Policy

(3).Change the value of EnableAutoUpdateCheck to 0 and the value of EnableJavaUpdate to 0.
Java should no longer prompt you for the annoying updates.



Enjoy..!!!

Is this trick useful??  Comment here..
Thank you.!!  :)

Tuesday, November 22

5 PROCESSES TO CHECK ARMSTRONG AND REVERSE OF NUMBER~ Unix Program

Hi reader,
  Here our task is :

  • Create 5 processes.
  • Find whether a number is Armstong and Reverse the same using these processes.



A :Accept number.
C:Find whether it is armstrong.
D:Print Result.
B:Calculate Reverse of the Number.
E:Display Reverse.



Here is the complete Program.


#include<stdio.h>
#include<unistd.h>
main()
{             //initially in A

int num,f1,f2,f3,f4,p1[2],p2[2],p3[2],p4[2],arm,rev,flag=0;
pipe(p1);
pipe(p2);


f1=fork();  //A created B
if(f1>0)    //A is working ..
{
f2=fork();  //A created C

if(f2>0)     //A is working...
{
printf("Enter a number   :");
scanf("%d",&num);


close(p1[0]);
close(p2[0]);
write(p1[1],&num,sizeof(num));
write(p2[1],&num,sizeof(num));
}
if(f2==0)   //C is working..
{


pipe(p3);
f3=fork(); //C created D

if(f3>0)   //C is working..
{
close(p1[1]);
read(p1[0],&num,sizeof(num));
flag=0;

for(arm=0;num>0;num/=10)
arm+=(num%10)*(num%10)*(num%10);

if(num==arm)flag=1;
close(p3[0]);
write(p3[1],&flag,sizeof(flag));
}
else if (f3==0)  //D is working..
{

close(p3[1]);
read(p3[0], &flag, sizeof(flag));
(flag)?printf("It is Armstrong Number"):("It is Not Armstrong Number");

}
}
}
if(f1==0)   //B is working..

{
sleep(2);
pipe(p4);
f4=fork(); //B created E

if(f4>0)   //B is working..{
close(p2[1]);
read(p2[0],&num,sizeof(num));
for(rev=0;num>0;num/=10)
rev=10*rev+num%10;
close(p4[0]);
write(p4[1],&rev,sizeof(rev));
}
if(f4==0)   //E is working..

{
close(p4[1]);
read(p4[0],&rev,sizeof(rev));
printf("Reverse of Number is : %d\n",rev);
}
}
}





Output
Enter a number: 153
It is Armstrong Number
Reverse of Number is :351

Monday, November 21

Lex program ~ Read and copy file content


This lex program will read an input file and copy the content with line number to another file.






%{
#include<stdio.h>
#include<string.h>
char line[20];
int count=0,i=0;
FILE *out;
%}
%%
['\n'] { fprintf(out,"%d %s\n",++count,line);}
(.*) {
strcpy(line,yytext);
}
%%
main()
{
yyin=fopen("in.c","r");
out=fopen("out.c","w");
yylex();
getch();
}

int yywrap()
{
return 1;
}








input
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}


output file


1 main()
2 {
3 int i=9, j=900,k=115;
4 printf("%d,i+j+k);
5 }
6 }

Read a file and copy content to other file~ C code




This C program will read an input file and copy the content to another file with Line numbers..!!


#include<stdio.h>
main()
{
      FILE *f1,*f2;
      int t=0;
      char line[20];
      f1=fopen("in.c","r");
      f2=fopen("out.c","w");
   
      while(!feof(f1)){
      fgets(line,20,f1);
      t++;
      fprintf(f2,"%d  %s",t,line);
      }
     getch();
      }

Lex- Collect Tokens and sort

Hello friend,
  Here I am with an interesting Lex program which sorts all the nubers from input file and sort them. If you are well-familiarised with lex paradigm, it is very easy task.

Things to remember:
  • The definition [0-9]+ can detect all the numbers from input
  • The number detected at present will be pointed by yytext and have length yyleng
  • Whenever a new token (here a number) is detected, yytext will be replaced with new value. 
Logic
  1. Read the input
  2. If a number is detected, store it in an array (we can use yytext and yyleng for this). Read each digit --> multiply with 10 --> add next digit -->continue upto last digit.
  for eg, to read 123
initialise num=0;
  • read 1--> num=num*10+1-->num=0+1-->num=1
  • read 2--> num=num*10+2-->num=10+2-->num=12
  • read 3--> num=num*10+3-->num=120+3-->num=123
store num in an array.
3. After reading all the numbers, sort the number within the array




%{
#include<stdio.h>
int t, a[20],i,j,n,num;
%}
%%
[0-9]+ {num=0;
 for(i=0;i<yyleng;i++)
num=num*10+(yytext[i]-'0');

a[n++]=num;
}
. ;
%%
main()
{
yyin=fopen("file.c","r");
yylex();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);

}

int yywrap()
{
return 1;
}





sample input (in file.c)
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}




output
9
115
900

Sunday, November 20

Quadruple ~ C code

#include<stdio.h>
#include<string.h>


main()
{

 char line[20];
int s[20];
int t=1;
     
int i=0;     
printf("Enter string..  :");
gets(line);
for(i=0;i<20;i++)s[i]=0;

printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}


for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);




getch();
}





Output




Enter string..  :a=b*c-b*c


op      a1      a2      res
:=      c                t1
*       b       t1       t2
:=      c                t3
*       b       t3       t4
-       t2      t4       t5
:=      t5              a

Friday, November 18

Lex and Yacc in Windows 7 - Compile and Run


Hi Friend,
I was in search for a long time to get a platform in windows-7 to run lex and Yacc ,for academic purposes. Many of my friends also had the same problem and often forced to choose Linux only for merely executing Lex & Yacc programs..!!!

At last, I found one solution and its my pleasure to share it with all of you.!!  :)

Its very simple.You need to download two very small files.
You have to download two executable( i.e.,   .exe files). In total,its size may have about 370KB.

To download them click the links below.

Link 1:   Flex

Link 2:  Bison

Save both files in same place. ( I did in D: --> Flex)
Keep the lex program to be compiled also in same place.


Now open command prompt--> reach your directory through command prompt.

To compile, type command 
                flex <filename> (eg:- flex myflex.l)

If your program is error-free, lex.yy.c will be generated.


We can compile this C file with any C compiler available (eg: Turbo C)

and generate yy.lex.exe

Now just run yy.lex.exe. Its your lexical analyser..!!!




I hope this post was useful for you.!!
Please comment your feedback here.!!
Thank You.!!  :)








Wednesday, November 16

1. Copy this line. 2. Paste it as a comment. 3.Delete "+" sign 4.press enter See the magic..!!! :) @+[178952746....:0]


Hi friend ..
 This time I would like to share a trick to make a nice 'magic' comment in FB.
For example..the post will be like this:


1. Copy this line.
2. Paste it as a comment.
3.Delete "+" sign
4.press enter
See the magic..!!! :)

@+[258129740903222:0]



On doing  as this post, the number will automatically vanished and replaced by
"LoVe U my dear friend - by vipin" :):):)



wow.. Cool na??

You too can make your own magic comment (with your name, to ur dear ones)!!
Only 5 steps are needed.

Make your own Facebook page.
 You can find the option for page creation at the bottom side of the page as this:

#Start to make a FB page: (You can select any category. I chose the last one.)

# Then you have to enter the title for your page. Listen, the content of your magic comment should be placed here (See, I put  LoVe U my dear friend - by vipin).
It can be any comment like

I love you..!!!
~U r my best friend..!!~
Let U b ma Best frnd~ FOREVER!!!
Its wonderful na..??
I love my Nation :)




# Then several steps will come to add picture, web address,etc,etc. You can do or skip these steps. It doesn't matter.

# Finally you will get yor page like this.

#Look at the URL of the page. You can find the ID for your page.

#Uff.. The task is over.!! Now you can make your amgic comment as:

@+[page id:0]

eg:
@+[258129740903222:0]


After deleting the "+" sign put it in your comment area. FB will recognize that it is Page ID and replace the number with page title.!! :)

1. Copy this line.
2. Paste it as a comment.
3.Delete "+" sign
4.press enter
See the magic..!!! :)

@+[258129740903222:0]

ENJOY!!!!!

How is the trick?? Is it cool?? How did u feel on trying this??
 Please comment here... !!  








Tuesday, November 15

LEX program to check a Date (dd/mm/yyyy)


Hello dude..   

            I would like to discuss a problem asked by my teacher in our internal lab exam- LEX program to check the validity of a Date. Ahem..In normal programming (i.e, that with C or Java) this is not a big deal!! But in Lex paradigm,it require some more effort..!!

                                                
                                                   

*Check the syntax of Date::


* If we have to check the syntax only ,i.e., dd/mm/yyyy apply the simple logic

[0-9][0-9] \ / [0-9][0-9] \ / [0-9] [0-9] [0-9] [0-9]


* But date should be within range [01-31] ,month be within [01-12] and assume year be within the range [1000-2999]. To apply these constraints, we redefine rules as:

"01-31"----> ([0-2][0-9] | 3 [0-1])
"01-12"----> (0[1-9] | 1[0-2])
"1000-2999"---->  ([1-2][0-9][0-9][0-9])


* Thus we can simply check the syntax of given Date as:

([0-2][0-9] | 3 [0-1])  \ /  (0[1-9] | 1[0-2]) \ / ([1-2][0-9][0-9][0-9])






*To Check the validity of date::

-->  In months 01,03,05,07,08,10 &12 , there will be at most 31 days.
([0-2][0-9]|[3][0-1])\/((0(1|3|5|7|8))|(10|12))\/([1-2][0-9][0-9][-0-9]) 

--> In months 04,06,09 & 11 may have at most 30 days
([0-2][0-9]|30)\/((0(4|6|9))|11)\/([1-2][0-9][0-9][0-9])

-->February has 28 days (in linear and non-linear years)
([0-1][0-9]|2[0-8])\/02\/([1-2][0-9][0-9][0-9])

-->If February has a day 29, check whether it is leap year or not..!!
29\/02\/([1-2][0-9][0-9][0-9])
 {
extract year value;
check whether it is a leap year;
}

-->Extract year value

   1.Iterate upto two "/" in date are over.(i.e.,in dd/mm/yyyy pass over two "/"s to reach at year value.
   2.read all susequent characters (they all are part of year value - yyyy)

while(yytext[i]!='/')i++;   //reach at first "/"
i++;                        // increment pointer
while(yytext[i]!='/')i++;   //reach at next "/"
i++;                        // increment pointer
while(i<yyleng)             // read all characters upto end of the string
yr=(10*yr)+(yytext[i++]-'0');// extract integer value of year

--> Check whether it is Leap year or not:

if(yr%4==0||(yr%100==0&&yr%400!=0))

Well... the complete lex program is as follows..!!



Date.l

%{
#include<stdio.h>
int i=0,yr=0,valid=0;
%}
%%

([0-2][0-9]|[3][0-1])\/((0(1|3|5|7|8))|(10|12))\/([1-2][0-9][0-9][-0-9]) {valid=1;}

([0-2][0-9]|30)\/((0(4|6|9))|11)\/([1-2][0-9][0-9][0-9]) {valid=1;}

([0-1][0-9]|2[0-8])\/02\/([1-2][0-9][0-9][0-9]) {valid=1;}

29\/02\/([1-2][0-9][0-9][0-9]) { while(yytext[i]!='/')i++; i++;while(yytext[i]!='/')i++;i++;while(i<yyleng)yr=(10*yr)+(yytext[i++]-'0'); if(yr%4==0||(yr%100==0&&yr%400!=0))valid=1;}

%%
main()
{
yyin=fopen("vpn.txt","r");
yylex();
if(valid==1) printf("It is a valid date\n");
else printf("It is not a valid date\n");
}

int yywrap()
{
return 1;
}




OUTPUT
Content in input file
(here, vpn.txt)
 Output
12/12/2011It is a valid date
32/10/2009It is not a valid date
29/02/2008It is a valid date
31/04/1990It is not a valid date
29/02/2007It is not a valid date



Hope this post was useful for you. If you have any suggestions or doubts,please do comment here...!!
Thank you




Tuesday, November 1

FOLLOW OF GIVEN GRAMMAR USING C PROGRAM

Hello reader,
In this post I am posting implementation of FOLLOW in C.
Note:
If your production statements contain multiple terms connected by '|'  then give these terms as separate productions
 


#include<stdio.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main()
{
 int i;
 int choice;
 char c,ch;
 printf("Enter the no.of productions: ");

scanf("%d", &n);

 printf(" Enter %d productions\nProduction with multiple terms should be give as separate productions \n", n);
 for(i=0;i<n;i++)
  scanf("%s%c",a[i],&ch);
    // gets(a[i]);

 do
 {
  m=0;
  printf("Find FOLLOW of -->");

  scanf(" %c",&c);
  follow(c);
  printf("FOLLOW(%c) = { ",c);
  for(i=0;i<m;i++)
   printf("%c ",followResult[i]);
  printf(" }\n");
  printf("Do you want to continue(Press 1 to continue....)?");
 scanf("%d%c",&choice,&ch);
 }
 while(choice==1);
}
void follow(char c)
{

    if(a[0][0]==c)addToResult('$');
 for(i=0;i<n;i++)
 {
  for(j=2;j<strlen(a[i]);j++)
  {
   if(a[i][j]==c)
   {
    if(a[i][j+1]!='\0')first(a[i][j+1]);

    if(a[i][j+1]=='\0'&&c!=a[i][0])
     follow(a[i][0]);

   }
  }
 }
}
void first(char c)
{
      int k;
                 if(!(isupper(c)))
                     //f[m++]=c;
                     addToResult(c);
                 for(k=0;k<n;k++)
                 {
                 if(a[k][0]==c)
                 {
                 if(a[k][2]=='$') follow(a[i][0]);
                 else if(islower(a[k][2]))
                     //f[m++]=a[k][2];
                     addToResult(a[k][2]);
                 else first(a[k][2]);
                 }
                 }

}

void  addToResult(char c)
{
    int i;
    for( i=0;i<=m;i++)
        if(followResult[i]==c)
            return;
   followResult[m++]=c;
}




Sample output: