Given the following interface class
interface BigOperation
{
public BigInteger operation (Biglnteger x, Biglnteger y);
}
Write Lambda expressions to implement BigOperation to simulate three arithmetic operations / and % on Biglnteger. You can call them op1, op2 and op3 respectively. Note that the three operations in Biglnteger are called multiply, divide and remainder respectively. Using these three operations, we wish to multiply all the digits in a Biglnteger. For example, if the Biglnteger is 234, the product is 2 * 3 * 4 = 24. The method has the following method heading:
static Biglnteger product (BigInteger n, BigOperation op1, BigOperation op2,
BigOperation op3)
Compare the implementation of this method using two approaches: Iterative and Recursive. Put the Lambda expressions in a main method. Construct a few Biglnteger objects, invoke the methods to test our design. The following shows some of the sample outputs:
Given big integer 8584803
(Iterative) e
(Recursive) 0
Given big integer 12345
(Iterative) 120
(Recursive) 120
I have no idea how to proceed with the code after what I have as attached. I wanted to try looping through the BigInteger N
with a foreach but it doesn't seem to work. Next, I wanted to do a for(int i = 0)
, etc. loop but I do not know how to get location reference to a BigInteger
variable. I don't know how to use lambda for this.
Thanks in advance for answering my queries.
import java.math.BigInteger;
import java.util.Random;
interface BigOperation{
public BigInteger operation(BigInteger x,BigInteger y);
}
class LearningBigInteger {
static BigInteger product(BigInteger n,BigOperation op1, BigOperation op2, BigOperation op3){
}
public static void main(String[] args) {
BigOperation multiply = BigInteger::multiply;
BigOperation divide = BigInteger::divide;
BigOperation remainder = BigInteger::remainder;
Random r = new Random();
BigInteger n = BigInteger.valueOf(Math.abs(r.nextInt()));
BigInteger m = product(n,multiply,divide,remainder);
}
}
Try this.
interface BigOperation {
BigInteger operation(BigInteger x, BigInteger y);
}
static class Iterative {
static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
BigInteger product = BigInteger.ONE;
for ( ; x.compareTo(BigInteger.ZERO) > 0; x = op2.operation(x, BigInteger.TEN))
product = op1.operation(product, op3.operation(x, BigInteger.TEN));
return product;
}
}
static class Recursive {
static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
if (x.compareTo(BigInteger.TEN) < 0)
return x;
else
return op1.operation(op3.operation(x, BigInteger.TEN),
product(op2.operation(x, BigInteger.TEN), op1, op2, op3));
}
}
and
static void test(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
System.out.println("Given bit integer " + x);
System.out.println("(Iterative) " + Iterative.product(x, op1, op2, op3));
System.out.println("(Recursive) " + Recursive.product(x, op1, op2, op3));
}
public static void main(String[] args) {
BigOperation multiply = BigInteger::multiply;
BigOperation divide = BigInteger::divide;
BigOperation remainder = BigInteger::remainder;
test(new BigInteger("8584803"), multiply, divide, remainder);
test(new BigInteger("12345"), multiply, divide, remainder);
}
output:
Given bit integer 8584803
(Iterative) 0
(Recursive) 0
Given bit integer 12345
(Iterative) 120
(Recursive) 120