Huge Integer In Java :-
package CoreClasses;
import java.util.*;
public class HugeInteger
{
private byte num[];
private byte sign;
private int len;
public HugeInteger()
{
num=new byte[40];
sign=0;
len=40;
}
public void setObject(String str)
{
int i,j;
for(i=39,j=str.length();j>1;i--,j--)
num[i]=(byte)(str.charAt(j-1)-'0');
if(str.charAt(0)=='-')
sign=1;
else if(str.charAt(0)=='+')
sign=0;
else
{
sign=0;
num[i]=(byte)(str.charAt(0)-'0');
}
setLength();
}
public void read()
{
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the no");
String str=new String();
str=sc.next();
num=new byte[40];
int i,j;
for(i=39,j=str.length();j>1;i--,j--)
num[i]=(byte)(str.charAt(j-1)-'0');
if(str.charAt(0)=='-')
sign=1;
else if(str.charAt(0)=='+')
sign=0;
else
{
sign=0;
num[i]=(byte)(str.charAt(0)-'0');
}
//setLength();
}
public void setLength()
{
for(int i=0;i<40;i++)
{
if(num[i]==0)
{
len--;
}
else
break;
}
}
public void print()
{
//int printing=40;
//i/f(len>40)
// printing=80;
System.out.println("Result: ");
if(sign==1)
System.out.print("-");
else
System.out.print("+");
int f=0;
for(int i=0;i<40;i++)
{
if(f==0 && num[i]==0)
{
//len--;
continue;
}
f=1;
System.out.print(num[i]);
}
if(f==0)
System.out.print("0");
}
public byte grt_no(byte x[],byte y[])
{
int i=0;
while(i<40)
{
if(x[i]>y[i])
return(1);
else if(x[i]<y[i])
return(2);
i++;
}
return(0);
}
public HugeInteger multiply(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
HugeInteger temp1=new HugeInteger();
if(sign!=n2.sign)
temp.sign=1;
else
temp.sign=0;
temp.num=new byte[80];
for(int i=0;i<80;i++)
temp.num[i]=0;
temp.len=80;
this.setLength();
n2.setLength();
temp.mul(num,n2.num);
temp.setLength();
if(temp.len<41)
{
for(int i=40;i<80;i++)
temp1.num[i-40]=temp.num[i];
}
else
{
System.out.println("\nOut of range");
return temp1;
}
return temp1;
}
private void mul(byte n1[],byte n2[])
{
int i,j,k,l;
byte temp,carry=0,carry1=0;
for(i=39,k=0;i>0;i--,k++)
{
carry=carry1=0;
for(j=39,l=79;j>0;j--,l--)
{
temp=(byte)(n1[j]*n2[i]+carry);
if(temp>=10)
{
carry=(byte)(temp/10);
num[l-k]=(byte)(num[l-k]+temp%10);
}
else
{
carry=0;
num[l-k]=(byte)(num[l-k]+temp);
}
if(num[l-k]>=10)
carry1=(byte)(num[l-k]/10);
else
carry1=0;
num[l-k]=(byte)(num[l-k]%10);
num[l-k-1]=(byte)(num[l-k-1]+carry1);
}
}
}
public HugeInteger add(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
if(sign!=n2.sign)
{
int g=grt_no(num,n2.num);
if(g==2)
temp.sign=n2.sign;
else
temp.sign=sign;
temp.sub(num,n2.num);
}
else
{
temp.sum(num,n2.num);
temp.sign=n2.sign;
}
return(temp);
}
private void sum(byte n1[],byte n2[])
{
byte carry=0,temp;
for(int i=39;i>=0;i--)
{
temp=(byte)(n1[i]+n2[i]+carry);
if(temp>=10)
{
carry=(byte)(temp/10);
num[i]=(byte)(temp%10);
}
else
{
num[i]=temp;
carry=0;
}
}
}
public HugeInteger subtract(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
if(n2.sign==0)
n2.sign=1;
else
n2.sign=0;
if(sign!=n2.sign)
{
int g=grt_no(num,n2.num);
if(g==2)
temp.sign=n2.sign;
else
temp.sign=sign;
temp.sub(num,n2.num);
}
else
{
int g=grt_no(num,n2.num);
if(g==2)
temp.sign=sign;
else
temp.sign=n2.sign;
temp.sum(num,n2.num);
}
return(temp);
}
private void sub(byte n1[],byte n2[])
{
byte l1,l2,borrow=0,diff,i,grt;
byte x1,x2;
byte temp[]=new byte[40];
grt=grt_no(n1,n2);
if(grt==2)
{
for(i=0;i<40;i++)
temp[i]=n1[i];
for(i=0;i<40;i++)
n1[i]=n2[i];
for(i=0;i<40;i++)
n2[i]=temp[i];
}
for(i=39;i>=0;i--)
{
x1=n1[i];x2=n2[i];
diff=(byte)(x1-x2-borrow);
if(diff<0)
{
diff=(byte)(diff+10);
borrow=1;
}
else
borrow=0;
num[i]=(byte)(diff);
}
}
public HugeInteger division(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
HugeInteger res=new HugeInteger();
if(sign!=n2.sign)
res.sign=1;
HugeInteger t=new HugeInteger();
t=this;
t.sign=0;
if(t.isLessThan(n2))
return res;
if(n2.isEqualTo(res))
{
System.out.println("\nDivide by zero error");
res.num[39]=-1;
return res;
}
int i,pos,j=39,k=0;
for(i=40-n2.len,k=0;i<40;i++,k++)
temp.num[i]=num[40-len+k];
if(n2.len==len)
{
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
res.num[39]++;
}
}
else
{
for(i=0;i<len-n2.len;)
{
if(n2.isGreaterThan(temp))
{
res.leftShift();
res.num[39]=0;
temp.leftShift();
temp.num[39]=num[40+n2.len-len+i];
temp.setLength();
i++;
}
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
res.num[39]++;
}
}
}
return res;
}
private void leftShift()
{
int i;
for( i=0;i<39;i++)
{
num[i]=num[i+1];
}
num[i]=0;
}
public HugeInteger modulo(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
//this.setLength();
//n2.setLength();
if(this.isLessThan(n2))
return temp;
if(n2.isEqualTo(temp))
{
System.out.println("\nModulo by zero error");
temp.num[39]=-1;
return temp;
}
int i,pos,j=39,k=0;
for(i=40-n2.len,k=0;i<40;i++,k++)
temp.num[i]=num[40-len+k];
if(n2.len==len)
{
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
}
}
else
{
for(i=0;i<len-n2.len;)
{
if(n2.isGreaterThan(temp))
{
//temp.print();
temp.leftShift();
temp.num[39]=num[40+n2.len-len+i];
temp.setLength();
i++;
}
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
}
}
}
temp.setLength();
return temp;
}
public boolean isLessThan(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return (true);
if(sign!=1 && x.sign==1)
return (false);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return true;
else if(i==2&&sign==1)
return false;
else if(i==1&&sign==0)
return false;
else if(i==0)
return false;
else
return true;
}
public boolean isLessThanOrEqualTo(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(true);
if(sign!=1 && x.sign==1)
return(false);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return true;
else if(i==2&&sign==1)
return false;
else if(i==0)
return true;
else if(i==1&&sign==0)
return false;
else
return true;
}
public boolean isGreaterThan(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(false);
if(sign!=1 && x.sign==1)
return(true);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return false;
else if(i==2&&sign==1)
return true;
else if(i==1&&sign==0)
return true;
else
return false;
}
public boolean isGreaterThanOrEqualTo(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(false);
if(sign!=1 && x.sign==1)
return(true);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return false;
else if(i==2&&sign==1)
return true;
else if(i==0)
return true;
else if(i==1&&sign==0)
return true;
else
return false;
}
public boolean isEqualTo(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(false);
if(sign!=1 && x.sign==1)
return(false);
for(int i=0;i<40;i++)
if((num[i]-x.num[i])!=0)
return(false);
return(true);
}
public boolean notEqualTo(HugeInteger x)
{
if(sign!=x.sign)
return(true);
int i=grt_no(num,x.num);
if(i==0)
return false;
return true;
}
}
Huge Prime extending Huge Integer :-
import java.util.*;
import CoreClasses.HugeInteger;
class HugePrime extends HugeInteger
{
public boolean isPrime()
{
HugeInteger two=new HugeInteger();
HugeInteger zero=new HugeInteger();
HugeInteger three=new HugeInteger();
HugeInteger t=new HugeInteger();
HugeInteger temp=new HugeInteger();
HugeInteger t1=new HugeInteger();
two.setObject("2");
two.setLength();
zero.setObject("0");
three.setObject("3");
three.setLength();
if(this.isLessThan(two))
return false;
if(this.isEqualTo(two))
return true;
if((this.modulo(two)).isEqualTo(zero))
return false;
temp=this.division(two);
for(t=three;t.isLessThan(temp);)
{
t1=this;
if((t1.modulo(t)).isEqualTo(zero))
return false;
t=t.add(two);
t.setLength();
}
return true;
}
public boolean isCoPrime(HugeInteger n2)
{
HugeInteger one=new HugeInteger();
HugeInteger t1=new HugeInteger();
HugeInteger t=new HugeInteger();
HugeInteger temp=new HugeInteger();
HugeInteger zero=new HugeInteger();
HugeInteger two=new HugeInteger();
two.setObject("2");
one.setObject("1");
zero.setObject("0");
if(this.isEqualTo(one)||n2.isEqualTo(one))
return false;
if(this.isLessThan(n2))
temp=this;
else
temp=n2;
for(t=two;t.isLessThanOrEqualTo(temp);)
{
if((this.modulo(t)).isEqualTo(zero) && (n2.modulo(t)).isEqualTo(zero))
return false;
t=t.add(one) ;
t.setLength();
}
return true;
}
public HugeInteger inverse(HugeInteger n2)
{
HugeInteger res=new HugeInteger();
HugeInteger y=new HugeInteger();
HugeInteger two=new HugeInteger();
HugeInteger zero=new HugeInteger();
HugeInteger one=new HugeInteger();
HugeInteger temp=new HugeInteger();
HugeInteger mod=new HugeInteger();
zero.setObject("0");
zero.setLength();
one.setObject("1");
one.setLength();
two.setObject("2");
two.setLength();
if(this.modulo(two).isEqualTo(zero)&&n2.modulo(two).isEqualTo(zero))
return zero;
y.setObject("1");
res=this;
res.setLength();
do
{
y.setLength();
temp=res.multiply(y);
temp.setLength();
mod=temp.modulo(n2);
mod.setLength();
y=y.add(one);
}while(mod.notEqualTo(one));
y=y.subtract(one);
return y;
}
}
class HugePrimeTest
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
do
{
HugePrime ob1=new HugePrime();
HugePrime ob2=new HugePrime();
System.out.println("\n1.Prime\n2.Coprime\n3.Inverse\n4.exit\nEnter your choice\n ");
int ch=sc.nextInt();
switch(ch)
{
case 1:ob1.read();
if(ob1.isPrime())
System.out.println("Yes.Prime no");
else
System.out.println("NO,Not a prime no");
break;
case 2:ob1.read();
ob2.read();
if(ob1.isCoPrime(ob2))
System.out.println("Yes,CoPrimes");
else
System.out.println("NO,CoPrimes");
break;
case 3:
System.out.println("\nEnter 1st no to find inverse of it");
ob1.read();
ob1.setLength();
System.out.println("\nEnter the modulo no w.r.t. you want to find inverse");
ob2.read();
ob2.setLength();
HugeInteger ob3=new HugeInteger();
ob3=ob1.inverse(ob2);
ob3.setLength();
ob3.print();
break;
case 4:System.exit(0);
default:
System.out.println("\nWrong");
}
}while(true);
}
}
Bounded Buffer Problem
import java.util.*;
class BoundedBuffer
{
private int []buffer;
int in,out,n;
private volatile int count;
public BoundedBuffer(int size)
{
buffer=new int[size];
n=size;
count=0;
in=0;
out=0;
}
public synchronized void put(int x)
{
while(count==n)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception Occur");
}
}
buffer[in]=x;
in=(in+1)%n;
count++;
notifyAll();
System.out.println("Put "+x+" by "+Thread.currentThread().getName());
}
public synchronized int get()
{
int x;
while(count==0)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception Occur");
}
}
x=buffer[out];
out=(out+1)%n;
count--;
notifyAll();
System.out.println("Get "+x+ " by "+Thread.currentThread().getName());
return(x);
}
}
class Producer implements Runnable
{
private BoundedBuffer buf;
private int i;
public Producer(BoundedBuffer buf,int i)
{
this.buf=buf;
this.i=i;
new Thread(this,"Producer"+i).start();
}
public void run()
{
int j,x=0;
for(j=0;j<20;j++,x++)
buf.put(i*100+x);
}
}
class Consumer implements Runnable
{
private BoundedBuffer buf;
public Consumer(BoundedBuffer buf,int i)
{
this.buf=buf;
new Thread(this,"Consumer "+i).start();
}
public void run()
{
int j,x;
for(j=0;j<20;j++)
{
x=buf.get();
System.out.println("Consumer get value "+x);
}
}
}
class BoundedBufferTest
{
public static void main(String args[])
{
BoundedBuffer buf;
Scanner in=new Scanner(System.in);
int n;
System.out.println("Enter the number of item:=");
n=in.nextInt();
buf=new BoundedBuffer(n);
new Producer(buf,1);
new Producer(buf,2);
new Producer(buf,3);
new Consumer(buf,1);
new Consumer(buf,2);
new Consumer(buf,3);
}
}
Cigarette Smoker Problem
import java.util.*;
class Table
{
String []ingr;
boolean[]isIngrAv;
int n;
int noOfAg;
public Table(int n,int noOfAg)
{
ingr=new String[n];
for(int i=0;i<n;i++)
ingr[i]="A"+i;
this.n=n;
this.noOfAg=noOfAg;
isIngrAv=new boolean[n] ;
for(int i=0;i<n;i++)
isIngrAv[i]=false;
}
public synchronized boolean checkTable(int i)
{
for(int j=0;j<n;j++)
if((j!=i)&&!(isIngrAv[j]))
return false;
return true;
}
public synchronized void rolloutIngr(int p)
{
int i=(int)(n*Math.random());
for(int j=p;j<n;j+=noOfAg)
isIngrAv[j]=true;
if(p==noOfAg-1)
isIngrAv[i]=false;
}
public synchronized void consume(int i)
{
for(int j=0;j<n;j++)
isIngrAv[j]=false;
}
public synchronized void printTable()
{
System.out.print("Table contains:");
for(int j=0;j<n;j++)
if(isIngrAv[j])
System.out.print(ingr[j]+" ");
System.out.println();
}
}
class Smoker implements Runnable
{
Table table;
int i;
public Smoker(Table table,int i)
{
this.table=table;
this.i=i;
new Thread(this,"Smoker"+i).start();
System.out.println("Smoker"+i+" with"+table.ingr[i]+" is started");
}
public void run()
{
while(true)
{
System.out.println("Smoker"+i+" with"+table.ingr[i]+" is checking the table");
while(!table.checkTable(i))
synchronized(table)
{
try
{
table.wait();
}
catch(InterruptedException e){}
}
System.out.println("Smoker"+i+" with"+table.ingr[i]+" is consuming");
table.consume(i);
System.out.println("Smoker"+i+"is smoking");
table.printTable();
try
{
Thread.sleep(100*i);
}
catch(InterruptedException e){}
System.out.println("Smoker"+i+" has finished smoking and notifying agent");
synchronized(table)
{
table.notifyAll();
}
}
}
}
class Agent implements Runnable
{
Table table;
int p;
public Agent(Table table,int p)
{
this.table=table;
this.p=p;
new Thread(this,"Agent"+p).start();
System.out.println("Agent"+p+" is started");
}
public void run()
{
while(true)
{
table.rolloutIngr(p);
synchronized(table)
{
try
{
table.wait();
}
catch(InterruptedException e){}
}
}
}
}
class CigratteSmokerTest
{
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
System.out.println("No of Smokers?????????");
int n=in.nextInt();
System.out.println("No of Agent???????????");
int a=in.nextInt();
Table t=new Table(n,a);
for(int i=0;i<a;i++)
new Agent(t,i);
for(int i=0;i<n;i++)
new Smoker(t,i);
}
}
Pagination in Advance Java
<%--
Document : result
Created on : Dec 14, 2010, 5:43:38 PM
Author : admin
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>result Page</title>
</head>
<body bgcolor="#cdfcdf">
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
double max=0;
String srch=request.getParameter("txtKeyword");
Connection con = DriverManager.getConnection("jdbc:odbc:asn3");
Statement stmt = con.createStatement();
Statement stmt1 = con.createStatement();
String sql="select * from keys where keyword='"+srch+"'";
String sql1="select count(*) from keys where keyword='"+srch+"'";
ResultSet rs1=stmt1.executeQuery(sql1);
if(rs1.next())
max=rs1.getInt(1);
if(max==0)
response.sendRedirect("index.jsp?err=1");
else{
ResultSet rs=stmt.executeQuery(sql);
%>
<table border="0" width="100%">
<tr>
<td align="center" colspan="2"><a href="index.jsp">Back to search page</a></td>
</tr>
<tr>
<td align="right" width="40%" bgcolor="#aabbcc"><h1>Text for that you search</h1></td>
<td bgcolor="#ccbbaa"><h2><%= srch%> </h2></td>
</tr>
<%
if(request.getParameter("pg")!=null)
for(int j=0;j<(Integer.parseInt(request.getParameter("pg"))-1)*2;j++)
rs.next();
int j=0;
while(j<2)
{
j++;
if(rs.next()){
String desc=rs.getString(3);
String f=rs.getString(4);
String link=rs.getString(5);
out.print("<tr>");
out.print("<td width='15%' bgcolor='#bbccdd'>");
if(f!=null)
out.print(f);
out.print("</td>");
out.print("<td bgcolor='#ddccbb'>");
out.print(desc);
out.print("</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2' align='center' bgcolor='#456789'>");
out.print("<a href=''><b><h3>"+link+"</b></h3></a></td>");
out.print("</tr>");
}}%>
<tr>
<td colspan="2" align="center" bgcolor="#898998">
<% for(int i=1;i<=Math.ceil(max/2);i++)
{
if(request.getParameter("pg")!=null && i==(Integer.parseInt(request.getParameter("pg"))))
out.print(i+" ");
else
out.print("<a href=result.jsp?pg="+i+"&txtKeyword="+srch+">"+i+"</a> ");
}}%>
</td>
</tr>
</table>
</body>
</html>
package CoreClasses;
import java.util.*;
public class HugeInteger
{
private byte num[];
private byte sign;
private int len;
public HugeInteger()
{
num=new byte[40];
sign=0;
len=40;
}
public void setObject(String str)
{
int i,j;
for(i=39,j=str.length();j>1;i--,j--)
num[i]=(byte)(str.charAt(j-1)-'0');
if(str.charAt(0)=='-')
sign=1;
else if(str.charAt(0)=='+')
sign=0;
else
{
sign=0;
num[i]=(byte)(str.charAt(0)-'0');
}
setLength();
}
public void read()
{
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the no");
String str=new String();
str=sc.next();
num=new byte[40];
int i,j;
for(i=39,j=str.length();j>1;i--,j--)
num[i]=(byte)(str.charAt(j-1)-'0');
if(str.charAt(0)=='-')
sign=1;
else if(str.charAt(0)=='+')
sign=0;
else
{
sign=0;
num[i]=(byte)(str.charAt(0)-'0');
}
//setLength();
}
public void setLength()
{
for(int i=0;i<40;i++)
{
if(num[i]==0)
{
len--;
}
else
break;
}
}
public void print()
{
//int printing=40;
//i/f(len>40)
// printing=80;
System.out.println("Result: ");
if(sign==1)
System.out.print("-");
else
System.out.print("+");
int f=0;
for(int i=0;i<40;i++)
{
if(f==0 && num[i]==0)
{
//len--;
continue;
}
f=1;
System.out.print(num[i]);
}
if(f==0)
System.out.print("0");
}
public byte grt_no(byte x[],byte y[])
{
int i=0;
while(i<40)
{
if(x[i]>y[i])
return(1);
else if(x[i]<y[i])
return(2);
i++;
}
return(0);
}
public HugeInteger multiply(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
HugeInteger temp1=new HugeInteger();
if(sign!=n2.sign)
temp.sign=1;
else
temp.sign=0;
temp.num=new byte[80];
for(int i=0;i<80;i++)
temp.num[i]=0;
temp.len=80;
this.setLength();
n2.setLength();
temp.mul(num,n2.num);
temp.setLength();
if(temp.len<41)
{
for(int i=40;i<80;i++)
temp1.num[i-40]=temp.num[i];
}
else
{
System.out.println("\nOut of range");
return temp1;
}
return temp1;
}
private void mul(byte n1[],byte n2[])
{
int i,j,k,l;
byte temp,carry=0,carry1=0;
for(i=39,k=0;i>0;i--,k++)
{
carry=carry1=0;
for(j=39,l=79;j>0;j--,l--)
{
temp=(byte)(n1[j]*n2[i]+carry);
if(temp>=10)
{
carry=(byte)(temp/10);
num[l-k]=(byte)(num[l-k]+temp%10);
}
else
{
carry=0;
num[l-k]=(byte)(num[l-k]+temp);
}
if(num[l-k]>=10)
carry1=(byte)(num[l-k]/10);
else
carry1=0;
num[l-k]=(byte)(num[l-k]%10);
num[l-k-1]=(byte)(num[l-k-1]+carry1);
}
}
}
public HugeInteger add(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
if(sign!=n2.sign)
{
int g=grt_no(num,n2.num);
if(g==2)
temp.sign=n2.sign;
else
temp.sign=sign;
temp.sub(num,n2.num);
}
else
{
temp.sum(num,n2.num);
temp.sign=n2.sign;
}
return(temp);
}
private void sum(byte n1[],byte n2[])
{
byte carry=0,temp;
for(int i=39;i>=0;i--)
{
temp=(byte)(n1[i]+n2[i]+carry);
if(temp>=10)
{
carry=(byte)(temp/10);
num[i]=(byte)(temp%10);
}
else
{
num[i]=temp;
carry=0;
}
}
}
public HugeInteger subtract(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
if(n2.sign==0)
n2.sign=1;
else
n2.sign=0;
if(sign!=n2.sign)
{
int g=grt_no(num,n2.num);
if(g==2)
temp.sign=n2.sign;
else
temp.sign=sign;
temp.sub(num,n2.num);
}
else
{
int g=grt_no(num,n2.num);
if(g==2)
temp.sign=sign;
else
temp.sign=n2.sign;
temp.sum(num,n2.num);
}
return(temp);
}
private void sub(byte n1[],byte n2[])
{
byte l1,l2,borrow=0,diff,i,grt;
byte x1,x2;
byte temp[]=new byte[40];
grt=grt_no(n1,n2);
if(grt==2)
{
for(i=0;i<40;i++)
temp[i]=n1[i];
for(i=0;i<40;i++)
n1[i]=n2[i];
for(i=0;i<40;i++)
n2[i]=temp[i];
}
for(i=39;i>=0;i--)
{
x1=n1[i];x2=n2[i];
diff=(byte)(x1-x2-borrow);
if(diff<0)
{
diff=(byte)(diff+10);
borrow=1;
}
else
borrow=0;
num[i]=(byte)(diff);
}
}
public HugeInteger division(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
HugeInteger res=new HugeInteger();
if(sign!=n2.sign)
res.sign=1;
HugeInteger t=new HugeInteger();
t=this;
t.sign=0;
if(t.isLessThan(n2))
return res;
if(n2.isEqualTo(res))
{
System.out.println("\nDivide by zero error");
res.num[39]=-1;
return res;
}
int i,pos,j=39,k=0;
for(i=40-n2.len,k=0;i<40;i++,k++)
temp.num[i]=num[40-len+k];
if(n2.len==len)
{
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
res.num[39]++;
}
}
else
{
for(i=0;i<len-n2.len;)
{
if(n2.isGreaterThan(temp))
{
res.leftShift();
res.num[39]=0;
temp.leftShift();
temp.num[39]=num[40+n2.len-len+i];
temp.setLength();
i++;
}
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
res.num[39]++;
}
}
}
return res;
}
private void leftShift()
{
int i;
for( i=0;i<39;i++)
{
num[i]=num[i+1];
}
num[i]=0;
}
public HugeInteger modulo(HugeInteger n2)
{
HugeInteger temp=new HugeInteger();
//this.setLength();
//n2.setLength();
if(this.isLessThan(n2))
return temp;
if(n2.isEqualTo(temp))
{
System.out.println("\nModulo by zero error");
temp.num[39]=-1;
return temp;
}
int i,pos,j=39,k=0;
for(i=40-n2.len,k=0;i<40;i++,k++)
temp.num[i]=num[40-len+k];
if(n2.len==len)
{
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
}
}
else
{
for(i=0;i<len-n2.len;)
{
if(n2.isGreaterThan(temp))
{
//temp.print();
temp.leftShift();
temp.num[39]=num[40+n2.len-len+i];
temp.setLength();
i++;
}
while(temp.isGreaterThanOrEqualTo(n2))
{
temp.sub(temp.num,n2.num);
}
}
}
temp.setLength();
return temp;
}
public boolean isLessThan(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return (true);
if(sign!=1 && x.sign==1)
return (false);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return true;
else if(i==2&&sign==1)
return false;
else if(i==1&&sign==0)
return false;
else if(i==0)
return false;
else
return true;
}
public boolean isLessThanOrEqualTo(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(true);
if(sign!=1 && x.sign==1)
return(false);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return true;
else if(i==2&&sign==1)
return false;
else if(i==0)
return true;
else if(i==1&&sign==0)
return false;
else
return true;
}
public boolean isGreaterThan(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(false);
if(sign!=1 && x.sign==1)
return(true);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return false;
else if(i==2&&sign==1)
return true;
else if(i==1&&sign==0)
return true;
else
return false;
}
public boolean isGreaterThanOrEqualTo(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(false);
if(sign!=1 && x.sign==1)
return(true);
int i=grt_no(num,x.num);
if(i==2&&sign==0)
return false;
else if(i==2&&sign==1)
return true;
else if(i==0)
return true;
else if(i==1&&sign==0)
return true;
else
return false;
}
public boolean isEqualTo(HugeInteger x)
{
if(sign==1 && x.sign!=1)
return(false);
if(sign!=1 && x.sign==1)
return(false);
for(int i=0;i<40;i++)
if((num[i]-x.num[i])!=0)
return(false);
return(true);
}
public boolean notEqualTo(HugeInteger x)
{
if(sign!=x.sign)
return(true);
int i=grt_no(num,x.num);
if(i==0)
return false;
return true;
}
}
Huge Prime extending Huge Integer :-
import java.util.*;
import CoreClasses.HugeInteger;
class HugePrime extends HugeInteger
{
public boolean isPrime()
{
HugeInteger two=new HugeInteger();
HugeInteger zero=new HugeInteger();
HugeInteger three=new HugeInteger();
HugeInteger t=new HugeInteger();
HugeInteger temp=new HugeInteger();
HugeInteger t1=new HugeInteger();
two.setObject("2");
two.setLength();
zero.setObject("0");
three.setObject("3");
three.setLength();
if(this.isLessThan(two))
return false;
if(this.isEqualTo(two))
return true;
if((this.modulo(two)).isEqualTo(zero))
return false;
temp=this.division(two);
for(t=three;t.isLessThan(temp);)
{
t1=this;
if((t1.modulo(t)).isEqualTo(zero))
return false;
t=t.add(two);
t.setLength();
}
return true;
}
public boolean isCoPrime(HugeInteger n2)
{
HugeInteger one=new HugeInteger();
HugeInteger t1=new HugeInteger();
HugeInteger t=new HugeInteger();
HugeInteger temp=new HugeInteger();
HugeInteger zero=new HugeInteger();
HugeInteger two=new HugeInteger();
two.setObject("2");
one.setObject("1");
zero.setObject("0");
if(this.isEqualTo(one)||n2.isEqualTo(one))
return false;
if(this.isLessThan(n2))
temp=this;
else
temp=n2;
for(t=two;t.isLessThanOrEqualTo(temp);)
{
if((this.modulo(t)).isEqualTo(zero) && (n2.modulo(t)).isEqualTo(zero))
return false;
t=t.add(one) ;
t.setLength();
}
return true;
}
public HugeInteger inverse(HugeInteger n2)
{
HugeInteger res=new HugeInteger();
HugeInteger y=new HugeInteger();
HugeInteger two=new HugeInteger();
HugeInteger zero=new HugeInteger();
HugeInteger one=new HugeInteger();
HugeInteger temp=new HugeInteger();
HugeInteger mod=new HugeInteger();
zero.setObject("0");
zero.setLength();
one.setObject("1");
one.setLength();
two.setObject("2");
two.setLength();
if(this.modulo(two).isEqualTo(zero)&&n2.modulo(two).isEqualTo(zero))
return zero;
y.setObject("1");
res=this;
res.setLength();
do
{
y.setLength();
temp=res.multiply(y);
temp.setLength();
mod=temp.modulo(n2);
mod.setLength();
y=y.add(one);
}while(mod.notEqualTo(one));
y=y.subtract(one);
return y;
}
}
class HugePrimeTest
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
do
{
HugePrime ob1=new HugePrime();
HugePrime ob2=new HugePrime();
System.out.println("\n1.Prime\n2.Coprime\n3.Inverse\n4.exit\nEnter your choice\n ");
int ch=sc.nextInt();
switch(ch)
{
case 1:ob1.read();
if(ob1.isPrime())
System.out.println("Yes.Prime no");
else
System.out.println("NO,Not a prime no");
break;
case 2:ob1.read();
ob2.read();
if(ob1.isCoPrime(ob2))
System.out.println("Yes,CoPrimes");
else
System.out.println("NO,CoPrimes");
break;
case 3:
System.out.println("\nEnter 1st no to find inverse of it");
ob1.read();
ob1.setLength();
System.out.println("\nEnter the modulo no w.r.t. you want to find inverse");
ob2.read();
ob2.setLength();
HugeInteger ob3=new HugeInteger();
ob3=ob1.inverse(ob2);
ob3.setLength();
ob3.print();
break;
case 4:System.exit(0);
default:
System.out.println("\nWrong");
}
}while(true);
}
}
Bounded Buffer Problem
import java.util.*;
class BoundedBuffer
{
private int []buffer;
int in,out,n;
private volatile int count;
public BoundedBuffer(int size)
{
buffer=new int[size];
n=size;
count=0;
in=0;
out=0;
}
public synchronized void put(int x)
{
while(count==n)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception Occur");
}
}
buffer[in]=x;
in=(in+1)%n;
count++;
notifyAll();
System.out.println("Put "+x+" by "+Thread.currentThread().getName());
}
public synchronized int get()
{
int x;
while(count==0)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception Occur");
}
}
x=buffer[out];
out=(out+1)%n;
count--;
notifyAll();
System.out.println("Get "+x+ " by "+Thread.currentThread().getName());
return(x);
}
}
class Producer implements Runnable
{
private BoundedBuffer buf;
private int i;
public Producer(BoundedBuffer buf,int i)
{
this.buf=buf;
this.i=i;
new Thread(this,"Producer"+i).start();
}
public void run()
{
int j,x=0;
for(j=0;j<20;j++,x++)
buf.put(i*100+x);
}
}
class Consumer implements Runnable
{
private BoundedBuffer buf;
public Consumer(BoundedBuffer buf,int i)
{
this.buf=buf;
new Thread(this,"Consumer "+i).start();
}
public void run()
{
int j,x;
for(j=0;j<20;j++)
{
x=buf.get();
System.out.println("Consumer get value "+x);
}
}
}
class BoundedBufferTest
{
public static void main(String args[])
{
BoundedBuffer buf;
Scanner in=new Scanner(System.in);
int n;
System.out.println("Enter the number of item:=");
n=in.nextInt();
buf=new BoundedBuffer(n);
new Producer(buf,1);
new Producer(buf,2);
new Producer(buf,3);
new Consumer(buf,1);
new Consumer(buf,2);
new Consumer(buf,3);
}
}
Cigarette Smoker Problem
import java.util.*;
class Table
{
String []ingr;
boolean[]isIngrAv;
int n;
int noOfAg;
public Table(int n,int noOfAg)
{
ingr=new String[n];
for(int i=0;i<n;i++)
ingr[i]="A"+i;
this.n=n;
this.noOfAg=noOfAg;
isIngrAv=new boolean[n] ;
for(int i=0;i<n;i++)
isIngrAv[i]=false;
}
public synchronized boolean checkTable(int i)
{
for(int j=0;j<n;j++)
if((j!=i)&&!(isIngrAv[j]))
return false;
return true;
}
public synchronized void rolloutIngr(int p)
{
int i=(int)(n*Math.random());
for(int j=p;j<n;j+=noOfAg)
isIngrAv[j]=true;
if(p==noOfAg-1)
isIngrAv[i]=false;
}
public synchronized void consume(int i)
{
for(int j=0;j<n;j++)
isIngrAv[j]=false;
}
public synchronized void printTable()
{
System.out.print("Table contains:");
for(int j=0;j<n;j++)
if(isIngrAv[j])
System.out.print(ingr[j]+" ");
System.out.println();
}
}
class Smoker implements Runnable
{
Table table;
int i;
public Smoker(Table table,int i)
{
this.table=table;
this.i=i;
new Thread(this,"Smoker"+i).start();
System.out.println("Smoker"+i+" with"+table.ingr[i]+" is started");
}
public void run()
{
while(true)
{
System.out.println("Smoker"+i+" with"+table.ingr[i]+" is checking the table");
while(!table.checkTable(i))
synchronized(table)
{
try
{
table.wait();
}
catch(InterruptedException e){}
}
System.out.println("Smoker"+i+" with"+table.ingr[i]+" is consuming");
table.consume(i);
System.out.println("Smoker"+i+"is smoking");
table.printTable();
try
{
Thread.sleep(100*i);
}
catch(InterruptedException e){}
System.out.println("Smoker"+i+" has finished smoking and notifying agent");
synchronized(table)
{
table.notifyAll();
}
}
}
}
class Agent implements Runnable
{
Table table;
int p;
public Agent(Table table,int p)
{
this.table=table;
this.p=p;
new Thread(this,"Agent"+p).start();
System.out.println("Agent"+p+" is started");
}
public void run()
{
while(true)
{
table.rolloutIngr(p);
synchronized(table)
{
try
{
table.wait();
}
catch(InterruptedException e){}
}
}
}
}
class CigratteSmokerTest
{
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
System.out.println("No of Smokers?????????");
int n=in.nextInt();
System.out.println("No of Agent???????????");
int a=in.nextInt();
Table t=new Table(n,a);
for(int i=0;i<a;i++)
new Agent(t,i);
for(int i=0;i<n;i++)
new Smoker(t,i);
}
}
Pagination in Advance Java
<%--
Document : result
Created on : Dec 14, 2010, 5:43:38 PM
Author : admin
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>result Page</title>
</head>
<body bgcolor="#cdfcdf">
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
double max=0;
String srch=request.getParameter("txtKeyword");
Connection con = DriverManager.getConnection("jdbc:odbc:asn3");
Statement stmt = con.createStatement();
Statement stmt1 = con.createStatement();
String sql="select * from keys where keyword='"+srch+"'";
String sql1="select count(*) from keys where keyword='"+srch+"'";
ResultSet rs1=stmt1.executeQuery(sql1);
if(rs1.next())
max=rs1.getInt(1);
if(max==0)
response.sendRedirect("index.jsp?err=1");
else{
ResultSet rs=stmt.executeQuery(sql);
%>
<table border="0" width="100%">
<tr>
<td align="center" colspan="2"><a href="index.jsp">Back to search page</a></td>
</tr>
<tr>
<td align="right" width="40%" bgcolor="#aabbcc"><h1>Text for that you search</h1></td>
<td bgcolor="#ccbbaa"><h2><%= srch%> </h2></td>
</tr>
<%
if(request.getParameter("pg")!=null)
for(int j=0;j<(Integer.parseInt(request.getParameter("pg"))-1)*2;j++)
rs.next();
int j=0;
while(j<2)
{
j++;
if(rs.next()){
String desc=rs.getString(3);
String f=rs.getString(4);
String link=rs.getString(5);
out.print("<tr>");
out.print("<td width='15%' bgcolor='#bbccdd'>");
if(f!=null)
out.print(f);
out.print("</td>");
out.print("<td bgcolor='#ddccbb'>");
out.print(desc);
out.print("</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2' align='center' bgcolor='#456789'>");
out.print("<a href=''><b><h3>"+link+"</b></h3></a></td>");
out.print("</tr>");
}}%>
<tr>
<td colspan="2" align="center" bgcolor="#898998">
<% for(int i=1;i<=Math.ceil(max/2);i++)
{
if(request.getParameter("pg")!=null && i==(Integer.parseInt(request.getParameter("pg"))))
out.print(i+" ");
else
out.print("<a href=result.jsp?pg="+i+"&txtKeyword="+srch+">"+i+"</a> ");
}}%>
</td>
</tr>
</table>
</body>
</html>
Making dynamic sub class object in super class
This is my sub class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2;
import pkg1.SuperClass;
/**
*
* @author Prateek
*/
public class SubClass extends SuperClass{
public SubClass() {
}
public Object[] subMethod() throws Exception{
return this.tryMethod();
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2;
import pkg1.SuperClass;
/**
*
* @author Prateek
*/
public class SubClass extends SuperClass{
public SubClass() {
}
public Object[] subMethod() throws Exception{
return this.tryMethod();
}
}
Now this is my Super class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pkg1;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import pkg2.SubClass;
/**
*
* @author Prateek
*/
public class SuperClass {
public String msg=" Hiii";
public HashMap<String,String> hm=new HashMap<String, String>();
public SuperClass() {
}
public Object[] tryMethod()throws Exception{
String cls=this.getClass().toString();
cls=cls.substring(cls.indexOf(" ")+1);
Class c= Class.forName(cls);
Constructor co=c.getConstructor();
Object o[]=new Object[2];
o[0] =co.newInstance();
o[1] =co.newInstance();
// c.cast(o);
Class a[]=new Class[1];
a[0]=String.class;
Method method = c.getMethod("getOther",a);//, (Class) null);
Field field = c.getField("hm");
// SuperClass sc=new SuperClass();
// hm.put("abc", "xyz");
// field.set(o, hm);
String ret[]=new String[2];
ret[0] =(String)method.invoke(o[0],"hi");//,"hi");//,null);
ret[1] =(String)method.invoke(o[1],"hmmm");
return o;
}
public String getOther(String s){
msg=s;
return "Hello "+s;
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pkg1;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import pkg2.SubClass;
/**
*
* @author Prateek
*/
public class SuperClass {
public String msg=" Hiii";
public HashMap<String,String> hm=new HashMap<String, String>();
public SuperClass() {
}
public Object[] tryMethod()throws Exception{
String cls=this.getClass().toString();
cls=cls.substring(cls.indexOf(" ")+1);
Class c= Class.forName(cls);
Constructor co=c.getConstructor();
Object o[]=new Object[2];
o[0] =co.newInstance();
o[1] =co.newInstance();
// c.cast(o);
Class a[]=new Class[1];
a[0]=String.class;
Method method = c.getMethod("getOther",a);//, (Class) null);
Field field = c.getField("hm");
// SuperClass sc=new SuperClass();
// hm.put("abc", "xyz");
// field.set(o, hm);
String ret[]=new String[2];
ret[0] =(String)method.invoke(o[0],"hi");//,"hi");//,null);
ret[1] =(String)method.invoke(o[1],"hmmm");
return o;
}
public String getOther(String s){
msg=s;
return "Hello "+s;
}
}
Now using both class in my code:
SubClass sbcls=new SubClass();
//out.print(sbcls.subMethod());
SubClass a[]=new SubClass[2];
Object arr[] = sbcls.subMethod();
for(int i=0;i<arr.length;i++){
a[i]=(SubClass)arr[i];
}
for(SubClass b:a){
out.print(b.msg+"<br />");
}
//out.print(sbcls.subMethod());
SubClass a[]=new SubClass[2];
Object arr[] = sbcls.subMethod();
for(int i=0;i<arr.length;i++){
a[i]=(SubClass)arr[i];
}
for(SubClass b:a){
out.print(b.msg+"<br />");
}
what exactly the volatile keyword in java/c/c++.why we use it?Please explain sir.
ReplyDelete-- The volatile keyword is mostly used to indicate that a member variable of a class may get modified asynchronously by more than one thread.
Delete-- This thing is noticable that the volatile keyword is not implemented in many Java Virtual Machines.
-- The volatile keyword from the side of compiler tries to guarantee that all the threads should see the same value of a specified variable.
intervw questions chahey.....pls we need it
ReplyDeleteYou can refer this site-
Deletehttp://www.javatpoint.com/corejava-interview-questions
thank you bhaiya..
ReplyDelete