Sunday, 18 August 2013

Large Integer Calculator Java Homework

Large Integer Calculator Java Homework

We were assigned to Create a LargeInteger class for arithmetic operations,
namely, addition and subtraction,that will not be bounded by the limit set
by the primitive data types in Java. However the BigInteger class is not
allowed. Using codes from different people in the internet I took a shot
at making it.
What's the problem with it?
public class LargeInteger {
final static int BASE = 1000000000;
final static int BASE_DECIMAL_DIGITS = 9;
private int[] digits;
private static LargeInteger valueOf(String string) {
// TODO Auto-generated method stub
return null;
}
public LargeInteger(int... digits) {
for(int digit : digits) {
if(digit < 0 || BASE <= digit) {
throw new IllegalArgumentException("digit " + digit +
" out of range!");
}
}
this.digits = digits.clone();
}
public String toString() {
return "Big" + Arrays.toString(digits);
}
//conversion
public String toDecimalString() {
Formatter f = new Formatter();
f.format("%d", digits[0]);
for(int i = 1 ; i < digits.length; i++) {
f.format("%09d", digits[i]);
}
return f.toString();
}
//addition
public LargeInteger plus(LargeInteger that) {
int[] result = new int[this.digits.length];
int carry = 0;
for(int i = this.digits.length-1; i > 0; i--) {
int digSum = carry + this.digits[i] + that.digits[i];
result[i] = digSum % BASE;
carry = digSum / BASE;
}
if(carry > 0) {
int[] temp = new int[result.length + 1];
System.arraycopy(result, 0, temp, 1, result.length);
temp[0] = carry;
result = temp;
}
return new LargeInteger(result);
}
//minus
public static int[] minus(int[] a, int[] b) {
int len = length(a);
int blen = length(b);
int[] c = new int[len];
int carry = 0;
for (int i = 0; i < len; i = i+1) {
int sum = a[i] - carry;
if (i < blen) sum -= b[i];
if (sum < 0) {
sum += 10;
carry = 1;
} else {
carry = 0;
}
c[i] = sum;
}
return c;
}
private static int length(int[] a) {
// TODO Auto-generated method stub
return 0;
}
//comparison
public int compareTo(LargeInteger that) {
if(this.digits.length < that.digits.length) {
return -1;
}
if (that.digits.length < this.digits.length) {
return 1;
}
for(int i = 0; i < this.digits.length; i++) {
if(this.digits[i] < that.digits[i]) {
return -1;
}
if(that.digits[i] < this.digits[i]) {
return 1;
}
}
return 0;
}
public int hashCode() {
int hash = 0;
for(int digit : digits) {
hash = hash * 13 + digit;
}
return hash;
}
//equals
public boolean equals(Object o) {
return o instanceof LargeInteger &&
this.compareTo((LargeInteger)o) == 0;
}
}
we were asked to follow this skeleton code:
public class LargeInteger {
/**
* Creates a LargeInteger object with its value set to the parameter.
*
* @param val initial value of the LargeInteger object
*/
public LargeInteger(String val) {
//TODO: Create the constructor
}
/**
* Adds the value of this BigInteger to object to the other BigInteger
object
* and returns the sum
*
* @param other Other BigInteger object that is to be added to this
BigInteger object
* @return Sum of this BigInteger with the other BigInteger
*/
public LargeInteger add(LargeInteger other) {
//TODO: Create the add method for adding BigIntegers
}
/**
* Minus the value of the other BigInteger object from this BigInteger
object
* and returns the difference
*
* @param other Other BigInteger object that is to be subtracted to
this BigInteger object
* @return Difference of this BigInteger with the other BigInteger
*/
public LargeInteger minus(LargeInteger other) {
//TODO: Create the minus method for adding BigIntegers
}
/**
* Multiplies the value of this BigInteger to object to the other
BigInteger object
* and returns the product
*
* @param other Other BigInteger object that is to be multiplied to
this BigInteger object
* @return Product of this BigInteger with the other BigInteger
*/
public LargeInteger times(LargeInteger other) {
//TODO: Create the times method for adding BigIntegers
}
/**
* Compares this LargeInteger object with the specified object for
equality
* @param x Object to which LargeInteger is to be compared
* @return true if and only if the specified Object is a LargeInteger
object whose value
* is numerically equal to this LargeInteger
*/
@Override
public boolean equals(Object x) {
//TODO: Create the equals method for comparing the equality of
this object with other object
}
/**
* Returns the string decimal representation of this LargeInteger
*
* @return String decimal representation of this LargeInteger
*/
@Override
public String toString() {
//TODO: Create the toString method for getting the decimal
representation
}
}

No comments:

Post a Comment