Create a class Hugeinteger which uses 40-element array of digits to store integers as large as 40 digits each.Provide methods inputHugeInteger,outputHugeInteger, addHugeIntegers, and subtractHugeIntegers.For comparing HugeInteger objects, provide methods isEqualTo, isNotEqualTo,isGreaterThan,isLessThan,isGreaterThanOrEqualTo , and isLessThanOrEqualTo-each of these is a "predicate" method that simply returns true if the relationship holds beteen two HugeIntegers and returns false if the relationship does not hold. Provide a predicate method isZero. If you feel ambitious, also provide the method multiplyHugeIntegers, the method divideHugeIntegers and the method moduleHugeIntegers.(File must be save as: F009.java)
import javax.swing.JOptionPane;
class HugeInteger
{
int a[]=new int[40];
int b[]=new int[40];
int p[]=new int[40];
int nofd,x,y=0,q,z=0,n=0;
void inputHugeInteger()
{
String st;
st=JOptionPane.showInputDialog("Enter any hugeInteger: ");
int i,n;
n=st.length();
for (i=0; i<n; i++)
a[i]=(st.charAt(n-i-1)-48);
nofd=n;
}
void outputHugeInteger()
{
String st="";
//int i;
if(y==1)
st="- ";
if(y==0)
st="";
for (int i=x-1; i>=0; i--)
st=st+b[i];
//System.out.println(st);
JOptionPane.showMessageDialog(null,st);
}
void addHugeInteger(HugeInteger ob)
{
int i,carry=0,sum;
for (i=0; i<nofd && i<ob.nofd; i++)
{
sum=(a[i]+ob.a[i]+carry);
b[i]=sum%10;
carry=sum/10;
}
for ( ; i<nofd; i++)
{
sum=a[i]+carry;
b[i]=sum%10;
carry=sum/10;
}
for ( ; i<ob.nofd; i++)
{
sum=ob.a[i]+carry;
b[i]=sum%10;
carry=sum/10;
}
if (carry>0)
{
b[i]=carry;
i++;
}
y=0;
x=i;
}
boolean check( int s[],int n[])
{
int i,f=0;//,borrow=0,sub;
if(nofd<q)
return true;
else if(nofd==q)
{
for(i=nofd-1;i>=0;i--)
{
if(s[i]>n[i])
return false;
else if(s[i]<n[i])
return true;
//else
z=1;
}
}
return false;
}
void subHugeInteger(HugeInteger ob)
{
int i,borrow=0,sub;
q=ob.nofd;
for(i=0;i<ob.nofd;i++)
p[i]=ob.a[i];
y=0;
if(check(a,p))
{
n=1;
for(i=0;i<nofd;i++)
{
p[i]=a[i];
a[i]=ob.a[i];
ob.a[i]=p[i];
}
while(i<ob.nofd)
a[i]=ob.a[i++];
x=ob.nofd;
ob.nofd=nofd;
nofd=x;
y=1;
}
//y=n;
for (i=0;i<ob.nofd; i++)
{
sub=a[i]-ob.a[i]-borrow;
if (sub<0)
{
sub+=10;
borrow=1;
}
else
borrow=0;
b[i]=sub;
}
while(borrow>0&& i<nofd)
{
sub=a[i]-borrow;
if (sub<0)
{
sub+=10;
borrow=1;
}
else
borrow=0;
b[i++]=sub;
}
while(i<nofd)
b[i]=a[i++];
while(b[i-1]==0 && i>1)
i--;
x=i;
}
boolean isEqualTo(HugeInteger ob)
{
boolean test=true;
for (int i=0; i<nofd||i<ob.nofd; i++)
if (a[i]!=ob.a[i])
test=false;
return test;
}
boolean isNotEqualTo(HugeInteger ob)
{
for (int i=0; i<nofd; i++)
if (a[i]!=ob.a[i])
return true;
return false;
}
boolean isGreaterThan(HugeInteger ob)
{
int i;
q=ob.nofd;
for(i=0;i<ob.nofd;i++)
p[i]=ob.a[i];
if(check(a,p))
return false;
return true;
}
boolean isLessThan(HugeInteger ob)
{
int i;
q=ob.nofd;
for(i=0;i<ob.nofd;i++)
p[i]=ob.a[i];
if(!check(a,p))
return false;
return true;
}
boolean isGreaterThanOrEqualTo(HugeInteger ob)
{
if (nofd>ob.nofd)
return true;
else if (nofd<ob.nofd)
return false;
else
for (int i=nofd-1; i>=0; i--)
if (a[i]<ob.a[i])
return false;
else if(a[i]>ob.a[i])
return true;
return true;
}
boolean isLessThanOrEualTo(HugeInteger ob)
{
if (nofd<ob.nofd)
return true;
else if (nofd>ob.nofd)
return false;
else
for (int i=nofd-1; i>=0; i--)
if (a[i]<ob.a[i])
return true;
else if(a[i]>ob.a[i])
return false;
return true;
}
}
class F009
{
public static void main(String args[])
{
HugeInteger a,b;
int choice;
String s,s1,s2,s3;
//boolean t;
a=new HugeInteger();
b=new HugeInteger();
do
{
a.inputHugeInteger();
b.inputHugeInteger();
String st="";
st+="Enter your choice from 1 to 9.\n";
st+="1. addHugeInteger.\n";
st+="2. subHugeInteger.\n";
st+="3. isEqualTo.\n";
st+="4. isNotEqualTo.\n";
st+="5. isGreaterThan.\n";
st+="6. isLessThan.\n";
st+="7. isGreaterThanOrEqualTo.\n";
st+="8. isLessThanOrEqualTo.\n";
st+="9. Exit.";
String input=JOptionPane.showInputDialog(st);
choice=Integer.parseInt(input);
switch(choice)
{
case 1:
a.addHugeInteger(b);
a.outputHugeInteger();
break;
case 2:
a.subHugeInteger(b);
a.outputHugeInteger();
break;
case 3:
if (a.isEqualTo(b))
JOptionPane.showMessageDialog(null,"Equal.");
else
JOptionPane.showMessageDialog(null,"Not Equal.");
break;
case 4:
if (a.isNotEqualTo(b))
JOptionPane.showMessageDialog(null,"Not Equal.");
else
JOptionPane.showMessageDialog(null,"Equal.");
break;
case 5:
if (a.isGreaterThan(b))
JOptionPane.showMessageDialog(null,"Greater than.");
else
JOptionPane.showMessageDialog(null,"Not Greater than.");
break;
case 6:
if (a.isLessThan(b))
JOptionPane.showMessageDialog(null,"Less than.");
else
JOptionPane.showMessageDialog(null,"Not less than.");
break;
case 7:
if (a.isGreaterThanOrEqualTo(b))
JOptionPane.showMessageDialog(null,"Greater Than or equal to.");
else
JOptionPane.showMessageDialog(null,"Not Greater than or equal to.");
break;
case 8:
if (a.isLessThanOrEualTo(b))
JOptionPane.showMessageDialog(null,"Less than or equal to.");
else
JOptionPane.showMessageDialog(null,"Not less than or equal to.");
break;
}
}
while(choice!=9);
System.exit(0);
}
}////////////////////////////////////////////
Copy & paste this code in your TC & run, then you will get output.......
If you have any problem please comment below.........