Develop a class VeryLargeInteger that can handle arbitrary long integer numbers
ID: 3602001 • Letter: D
Question
Develop a class VeryLargeInteger that can handle arbitrary long integer numbers (both negative and positive) and the basic arith- metic operations (addition, subtraction, multiplication, division, and remainder). Use a string for the value, and a boolean for the sign. The operation would mimic pencil and paper methods. Do not use BigInteger.
Note: Implementations of addition/subtraction through repeated use of a constant increment/decrement will not be accepted. Implementations of multiplication and division that only use stepwise addition or subtraction will not be accepted.
Any help will be appreciated!!
/
VeryLargeInteger (VLI) is a class for arbitrary precision integer computation. /
public class VeryLargeInteger { /
Constructs a new VLI object from a long integer. @param init initial value
/
VeryLargeInteger(long init) { / YOUR CODE / }
/
Constructs a new VLI object from a String.
@param init initial value. Note, the string represents a valid VLI, but can
/
/
Computes this+other and returns the result in a new object. @param other the lefthand side operand.
@return a new VLI representing this+other
/
VeryLargeInteger add(VeryLargeInteger other) { / YOUR CODE / }
/
Computes thisother and returns the result in a new object. @param other the lefthand side operand.
@return a new VLI representing thisother
/
VeryLargeInteger sub(VeryLargeInteger other) { / YOUR CODE / }
1
be prefixed with a sign (either + or ). VeryLargeInteger(String init) { / YOUR CODE / }
/
Computes thisother and returns the result in a new object. @param other the lefthand side operand.
@return a new VLI representing thisother
/
VeryLargeInteger mul(VeryLargeInteger other) { / YOUR CODE / }
/
Computes this/other and returns the result in a new object. @param other the lefthand side operand.
@return a new VLI representing this/other
/
VeryLargeInteger div(VeryLargeInteger other) { / YOUR CODE / }
/
Computes this%other and returns the result in a new object. @param other the lefthand side operand.
@return a new VLI representing this%other
/
VeryLargeInteger mod(VeryLargeInteger other) { / YOUR CODE / }
/
Returns the textual representation of this VLI. @result a string representing this VLI
/
String toString() { / YOUR CODE / } / YOUR CODE /
}
Explanation / Answer
Ans:
Public class VeryLargeInteger {
public static void Main(string[] args){
Long val=new Long();
VeryLargeInteger(long init) {
this.val=init;
}
}
VeryLargeInteger add(VeryLargeInteger other) {
return this+other;
}
VeryLargeInteger sub(VeryLargeInteger other) { return this-other; }
String str=new String();
VeryLargeInteger(String init) { this.str=init;}
VeryLargeInteger mul(VeryLargeInteger other) {
return other*this;
}
VeryLargeInteger div(VeryLargeInteger other) {
return this/other;
}
VeryLargeInteger mod(VeryLargeInteger other) {
str=other.ToString();
}
String toString() {
System.out.println("Result=" +this.toString());
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.