using this file to start huge integer if possible public class HugeInteger { pri
ID: 3849500 • Letter: U
Question
using this file to start huge integer if possible
public class HugeInteger
{
private int[] intArray;
private int numDigits; // stores the number of digits in intArray
public HugeInteger(String s)
{
intArray = new int[40];
numDigits = 0;
// call parse(s)
}
public HugeInteger( )
{
intArray = new int[40];
numDigits = 0;
}
public void parse(String s)
{
// Add each digit to the arrays
// update numDigits
}
public static HugeInteger add(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// Create hugeInt3
// Loop
// Add digits from hugeInt1 and hugeInt2,
// Store in corresponding hugeInt3
// End
//
// return hugeInt3
}
public static HugeInteger subtract(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// Create hugeInt3
// Loop
// Subtract hugeInt2 digit from hugeInt1,
// Store in corresponding hugeInt3
// End
//
// return hugeInt3
}
public static boolean isEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// return true if the value represented by
// elements of hugeInt1.intArray is equal to
// value represented by elements of hughInt2.intArray
}
public static boolean isNotEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// return true if the value represented by
// elements of hugeInt1.intArray is not equal to
// value represented by elements of hughInt2.intArray
}
public static boolean isGreaterThan(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// return true if the value represented by
// elements of hugeInt1.intArray is greater than
// value represented by elements of hughInt2.intArray
}
public static boolean isLessThan(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// return true if the value represented by
// elements of hugeInt1.intArray is less than
// value represented by elements of hughInt2.intArray
}
public static boolean isGreaterThanOrEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// return true if the value represented by
// elements of hugeInt1.intArray is greater than or equal to
// value represented by elements of hughInt2.intArray
}
public static boolean isZero(HugeInteger hugeInt1 )
{
// return true if the value represented by
// elements of hugeInt1.intArray is 0
}
public String toString( )
{
// return string representation of this object
}
}
plus the HugeIntegerTest
I need an answer to the above java assignment using the template above and just filling in the missing code where the comments are. I also need a HugeIntegerTest class please. I've found several solutions. but none of them really use the structure above filling in the missing code where the comments are and that's what I need please. I've been working on this assignment for a few days now and just cannot get it. HELP PLEASE!!!
The original assignment:
(Huge Integer Class) Create a class HugeInteger which uses a 40-element array of digits to
store integers as large as 40 digits each. Provide methods parse, toString, add and subtract. Method
parse should receive a String, extract each digit using method charAt and place the integer
equivalent of each digit into the integer array. For comparing HugeInteger objects, provide the following
methods: isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo
and isLessThanOrEqualTo. Each of these is a predicate method that returns true if the relationship
holds between the two HugeInteger objects and returns false if the relationship does not hold. Provide
a predicate method isZero. If you feel ambitious, also provide methods multiply, divide and
remainder. [Note: Primitive boolean values can be output as the word true or the word false with
format specifier %b.]
Explanation / Answer
HugeInteger.java
public class HugeInteger {
public int[] intArray;
public int numDigits;
static String ss;
public HugeInteger(String s)
{
this. intArray = new int[40];
this. numDigits = 0;
// call parse(s)
parse(s);
}
public HugeInteger( )
{
this. intArray = new int[40];
this. numDigits = 0;
}
public void parse(String s)
{
ss=s;
// Add each digit to the arrays
// update numDigits
for(int i=0;i<s.length();i++){
this.intArray[i]=s.charAt(i)-'0';
//System.out.print(intArray[i]+" ");
this.numDigits++;
//System.out.println(intArray[i]+" "+numDigits);
}
}
public HugeInteger add(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// Create hugeInt3
// Loop
// Add digits from hugeInt1 and hugeInt2,
// Store in corresponding hugeInt3
// End
//
// return hugeInt3
HugeInteger hugeInt3 = new HugeInteger();
for(int i=0;i<hugeInt3.intArray.length;i++){
hugeInt3.intArray[i]=hugeInt1.intArray[i]+hugeInt2.intArray[i];
}
String data=hugeInt3.intArray.toString();
for(int j=0;j<data.length();j++)
System.out.println(hugeInt3.intArray[j]);
return hugeInt3;
}
public HugeInteger subtract(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
// Create hugeInt3
// Loop
// Subtract hugeInt2 digit from hugeInt1,
// Store in corresponding hugeInt3
// End
//
// return hugeInt3
HugeInteger hugeInt4 = new HugeInteger();
for(int i=0;i<hugeInt1.intArray.length;i++){
if(hugeInt1.intArray[i]>=hugeInt2.intArray[i])
hugeInt4.intArray[i]=hugeInt1.intArray[i]-hugeInt2.intArray[i];
else
hugeInt4.intArray[i]=hugeInt2.intArray[i]-hugeInt1.intArray[i];
}
String data=hugeInt4.intArray.toString();
for(int j=0;j<data.length();j++)
System.out.println(hugeInt4.intArray[j]);
return hugeInt4;
}
public static boolean isEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
for(int i=0;i<hugeInt2.intArray.length;i++)
System.out.println(hugeInt2.intArray[i]);
int count=0;
// return true if the value represented by
// elements of hugeInt1.intArray is equal to
// value represented by elements of hughInt2.intArray
for(int i=0;i<hugeInt2.numDigits;i++){
int a1=hugeInt1.intArray[i];
int a2=hugeInt2.intArray[i];
// System.out.println(a1+" "+a2);
if(a1!=a2){
//System.out.println(hugeInt2.intArray[i]);
count++;
}
}
//System.out.println("count"+count);
if(count>0)
return false;
else
return true;
}
public static boolean isnotEqualTo(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
for(int i=0;i<hugeInt2.intArray.length;i++)
System.out.println(hugeInt2.intArray[i]);
int count=0;
// return true if the value represented by
// elements of hugeInt1.intArray is equal to
// value represented by elements of hughInt2.intArray
for(int i=0;i<hugeInt2.numDigits;i++){
int a1=hugeInt1.intArray[i];
int a2=hugeInt2.intArray[i];
// System.out.println(a1+" "+a2);
if(a1!=a2){
//System.out.println(hugeInt2.intArray[i]);
count++;
}
}
//System.out.println("count"+count);
if(count>0)
return true;
else
return false;
}
public static boolean isZero(HugeInteger hugeInt1 )
{
int count=0;
// return true if the value represented by
// elements of hugeInt1.intArray is 0
for(int i=0;i<hugeInt1.intArray.length;i++){
if(hugeInt1.intArray[i]==0)
count++;
}
if(count>0)
return true;
else
return false;
}
public static boolean isGreaterThan(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
int sum1=0;
int sum2=0;
// return true if the value represented by
// elements of hugeInt1.intArray is greater than
// value represented by elements of hughInt2.intArray
for(int i=0;i<hugeInt1.intArray.length;i++){
sum1+=hugeInt1.intArray[i];
sum2+=hugeInt1.intArray[i];
}
if(sum1>sum2)
return true;
else return false;
}
public static boolean isGreaterThanEqualto(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
int sum1=0,sum2=0;
// return true if the value represented by
// elements of hugeInt1.intArray is less than
// value represented by elements of hughInt2.intArray
for(int i=0;i<hugeInt1.intArray.length;i++){
sum1+=hugeInt1.intArray[i];
sum2+=hugeInt1.intArray[i];
}
if(sum1>=sum2)
return true;
else
return false;
}
public static boolean isLessThan(HugeInteger hugeInt1, HugeInteger hugeInt2)
{
int sum1=0,sum2=0;
// return true if the value represented by
// elements of hugeInt1.intArray is less than
// value represented by elements of hughInt2.intArray
for(int i=0;i<hugeInt1.intArray.length;i++){
sum1+=hugeInt1.intArray[i];
sum2+=hugeInt1.intArray[i];
}
if(sum1<sum2)
return true;
else
return false;
}
public String tostring(){
return this.toString();
}
}
mainall.java
public class Mainall {
public static void main(String[] args) {
// TODO Auto-generated method stub
HugeInteger h=new HugeInteger("swa");
HugeInteger h2=new HugeInteger("swz");
HugeInteger h3=new HugeInteger();
HugeInteger hall= h3.add(h, h2);
// boolean b=h3.isnotEqualTo(h, h2);
// System.out.println(b);
boolean iszero=h3.isZero(h);
// System.out.println(iszero);
System.out.println(h3.isEqualTo(h, h2));
System.out.println(h3.isnotEqualTo(h, h2));
System.out.println(h3.isGreaterThan(h, h2));
System.out.println(h3.isGreaterThanEqualto(h, h2));
System.out.println(h3.isLessThan(h, h2));
}
}
output:
134
142
123
0
0
0
0
0
0
0
0
67
71
74
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
false
67
71
74
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
true
false
true
false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.