*****************MAKE SURE UR CODE WORKS AND ITS JAVA************** You are to w
ID: 441382 • Letter: #
Question
*****************MAKE SURE UR CODE WORKS AND ITS JAVA************** You are to write a program that will implement positive integers that can be up to 50 digits long. Specifically, add whatever is necessary so that the class fragment shown below behaves correctly. Leave all variable and method names exactly as they appear. Do not change the two methods that are already written. Your big integers should be represented in the array with one decimal digit per array element and the low order digit should be at index 0. For example, ifExplanation / Answer
Please rate...
Program BigInt.java:
=====================================================
class BigInt
{
private int digit[]; // represent the integer as an array of digits
private int size; // number of digits in the integer
private final int max = 50; // maximum number of digits in the integer
public BigInt()
{
digit = new int[max];
for (int ct = 0; ct <max; ct ++)
{
digit[ct] = 0;
}
}
public BigInt(String num)
{
// constructor with initial String value
size = num.length();
digit = new int[max];
for (int ct = size - 1; ct >= 0; ct --)
{
digit[ct] = Integer.parseInt(num.substring(size - ct - 1, size - ct));
}
}
public BigInt(int num)
{
// constructor with initial integer value
}
public BigInt(BigInt num)
{
// copy constructor
}
public String toString()
{
// override Object’s version
String intString = "";
for (int ct = size - 1; ct >= 0; ct --)
{
intString = intString + String.valueOf(digit[ct]);
}
return intString;
}
public int compareTo(BigInt other)
{
// return 1 if this greater than other
if(this.digit.toString().compareTo(other.digit.toString())>1)
return 1;
// return -1 if this less than other
if(this.digit.toString().compareTo(other.digit.toString())<1)
return -1;
// return 0 if both equal
if(this.digit.toString().compareTo(other.digit.toString())==0)
return 0;
return 0;
}
public BigInt plus(BigInt arg)
{
BigInt t = new BigInt();
int temp;
int carry = 0;
int i;
for(i=0;i<(this.digit.length>arg.digit.length?this.size:arg.size);i++)
{
temp=carry+this.digit[i]+arg.digit[i];
if(temp>9)
{
carry=1;
t.digit[i]=temp%10;
}
else
{
carry=0;
t.digit[i]=temp;
}
}
return t;
}
/*public BigInt times(BigInt other)
{
// multiply two BigInts
} */
private void times10()
{
// I found this useful, but you might not.
// Not required, but if you do write it be careful
// since it changes the BigInt. You might prefer
// writing a version that creates a new BigInt
}
public static void main(String args[])
{
BigInt ob1=new BigInt("1234");
BigInt ob2=new BigInt("2345");
BigInt ob3;
ob3 = ob2.plus(ob1);
System.out.println(ob3.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.