Implementation of Java Native Interface
A) Develop a JNI application for giving a string equivalent of a number (integer) entered by the client.
B) Develop a JNI application for displaying factorial of an integer number.
C) Develop a JNI application for checking string palindrome.
(A) Develop a JNI application for giving a string equivalent of a number (integer) entered by the client.
Code:
NumString.java
NumImpl.c
Output:
(B) Develop a JNI application for displaying factorial of an integer number
Code:
Factorial.c
Factorial.h
Factorial.java
Output:
(C) Develop a JNI application for checking string palindrome.
Code:
Palindrome.c
Palindrome.h
Palindrome.java
Output:
A) Develop a JNI application for giving a string equivalent of a number (integer) entered by the client.
B) Develop a JNI application for displaying factorial of an integer number.
C) Develop a JNI application for checking string palindrome.
(A) Develop a JNI application for giving a string equivalent of a number (integer) entered by the client.
Code:
NumString.java
import java.io.*;
class NumString
{
public native void str(int num);
static
{
System.loadLibrary("numstring");
}
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int num=0;
System.out.println("Enter number between 1 to 10 to find string
equivalent: ");
num=Integer.parseInt(br.readLine());
new NumString().str(num);
}
catch(Exception e)
{
System.out.println("in main "+e);
}
}
}//class
class NumString
{
public native void str(int num);
static
{
System.loadLibrary("numstring");
}
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int num=0;
System.out.println("Enter number between 1 to 10 to find string
equivalent: ");
num=Integer.parseInt(br.readLine());
new NumString().str(num);
}
catch(Exception e)
{
System.out.println("in main "+e);
}
}
}//class
NumImpl.c
#include <stdio.h>
#include "jni.h"
#include "NumString.h"
JNIEXPORT void JNICALL Java_NumString_str
(JNIEnv *env, jobject obj, jint num)
{
switch(num)
{
case 1:printf("One");break;
case 2:printf("Two");break;
case 3:printf("Three");break;
case 4:printf("Four");break;
case 5:printf("Five");break;
case 6:printf("Six");break;
case 7:printf("Seven");break;
case 8:printf("Eight");break;
case 9:printf("Nine");break;
case 10:printf("Ten");break;
}
}
#include "jni.h"
#include "NumString.h"
JNIEXPORT void JNICALL Java_NumString_str
(JNIEnv *env, jobject obj, jint num)
{
switch(num)
{
case 1:printf("One");break;
case 2:printf("Two");break;
case 3:printf("Three");break;
case 4:printf("Four");break;
case 5:printf("Five");break;
case 6:printf("Six");break;
case 7:printf("Seven");break;
case 8:printf("Eight");break;
case 9:printf("Nine");break;
case 10:printf("Ten");break;
}
}
Output:
(B) Develop a JNI application for displaying factorial of an integer number
Code:
Factorial.c
#include <jni.h>
#include <stdio.h>
#include "Factorial.h"
JNIEXPORT void JNICALL Java_Factorial_fact
(JNIEnv *env, jobject obj, jint num)
{
int i,sum=1;
if(num==0)
{
printf("Factorial is 1");
}
else{
while(num>0)
{
sum=sum*num;
num--;
}
printf("Factorial is %d",sum);
}
}
#include <stdio.h>
#include "Factorial.h"
JNIEXPORT void JNICALL Java_Factorial_fact
(JNIEnv *env, jobject obj, jint num)
{
int i,sum=1;
if(num==0)
{
printf("Factorial is 1");
}
else{
while(num>0)
{
sum=sum*num;
num--;
}
printf("Factorial is %d",sum);
}
}
Factorial.h
#include <jni.h>
JNIEXPORT void JNICALL Java_Factorial_fact (JNIEnv *, jobject, jint);
JNIEXPORT void JNICALL Java_Factorial_fact (JNIEnv *, jobject, jint);
Factorial.java
import java.io.*;
public class Factorial
{
public native void fact(int a);
static
{
System.loadLibrary("Factorial");
}
public static void main(String[] args)
{
try
{
System.out.println("Enter number: ");
int a=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
Factorial j = new Factorial();
j.fact(a);
}
catch (Exception ee)
{
System.out.println(ee.getMessage());
}
}
}
public class Factorial
{
public native void fact(int a);
static
{
System.loadLibrary("Factorial");
}
public static void main(String[] args)
{
try
{
System.out.println("Enter number: ");
int a=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
Factorial j = new Factorial();
j.fact(a);
}
catch (Exception ee)
{
System.out.println(ee.getMessage());
}
}
}
Output:
(C) Develop a JNI application for checking string palindrome.
Code:
Palindrome.c
#include <jni.h>
#include <stdio.h>
#include<string.h>
#include "Palindrome.h"
JNIEXPORT jboolean JNICALL Java_Palindrome_palin
(JNIEnv *env, jobject obj, jchar ch1, jchar ch2)
{
if(ch1==ch2)
{
return 1;
}
else
{
return 0;
}
}
#include <stdio.h>
#include<string.h>
#include "Palindrome.h"
JNIEXPORT jboolean JNICALL Java_Palindrome_palin
(JNIEnv *env, jobject obj, jchar ch1, jchar ch2)
{
if(ch1==ch2)
{
return 1;
}
else
{
return 0;
}
}
Palindrome.h
#include <jni.h>
JNIEXPORT jboolean JNICALL Java_Palindrome_palin
(JNIEnv *, jobject, jchar, jchar);
JNIEXPORT jboolean JNICALL Java_Palindrome_palin
(JNIEnv *, jobject, jchar, jchar);
Palindrome.java
import java.io.*;
public class Palindrome
{
public native boolean palin(char ch,char ch1);
static
{
System.loadLibrary("Palindrome");
}
public static void main(String[] args)
{
try
{
System.out.println("Enter text: ");
String str=new BufferedReader(new InputStreamReader(System.in)).readLine();
Palindrome p = new Palindrome();
char c[]=new char[str.length()];
char z[]=new char[str.length()];
int i=0;
for(int x=str.length()-1;x>=0;x--)
{
z[i]=str.charAt(x);
i++;
}
int siz=str.length();
System.out.println();
for(i=0;i<siz;i++)
{
c[i]=str.charAt(i);
System.out.println(c[i]+"hi");
}
System.out.println();
for(i=0;i<siz;i++)
{
System.out.println(z[i]+"hello");
}
boolean blnFlags=false;
for(i=0;i<siz;i++)
{
blnFlags=p.palin(c[i],z[i]);
if(blnFlags==false)
{
System.out.println("String is not palindrome");
break;
}
}
if(blnFlags==true)
{
System.out.println("String is palindrome");
}
}
catch (Exception ee)
{
System.out.println(ee.getMessage());
}
}
}
public class Palindrome
{
public native boolean palin(char ch,char ch1);
static
{
System.loadLibrary("Palindrome");
}
public static void main(String[] args)
{
try
{
System.out.println("Enter text: ");
String str=new BufferedReader(new InputStreamReader(System.in)).readLine();
Palindrome p = new Palindrome();
char c[]=new char[str.length()];
char z[]=new char[str.length()];
int i=0;
for(int x=str.length()-1;x>=0;x--)
{
z[i]=str.charAt(x);
i++;
}
int siz=str.length();
System.out.println();
for(i=0;i<siz;i++)
{
c[i]=str.charAt(i);
System.out.println(c[i]+"hi");
}
System.out.println();
for(i=0;i<siz;i++)
{
System.out.println(z[i]+"hello");
}
boolean blnFlags=false;
for(i=0;i<siz;i++)
{
blnFlags=p.palin(c[i],z[i]);
if(blnFlags==false)
{
System.out.println("String is not palindrome");
break;
}
}
if(blnFlags==true)
{
System.out.println("String is palindrome");
}
}
catch (Exception ee)
{
System.out.println(ee.getMessage());
}
}
}
Output:
0 comments:
Confused? Feel free to ask
Post a Comment