Question

***Code to solve***

package functions;

import java.math.*; // for BigInteger

public class NodeTransitionFunction {

   public NodeTransitionFunction(Integer exp, Integer KVal) {
       // CONSTUCTOR: Sets the class to calculate f(x) = (x ^ exp) mod KVal

       // TODO
   }
  

  
  
   public Integer apply(Integer val) {
       // PRE: -
       // POST: Implements f(val)
       return null;
   }
  

   public BigInteger apply(BigInteger val) {
       // PRE: -
       // POST: Implements f(val), with val as a BigInteger

       // TODO
      
       return null;
   }


  
   public static void main(String[] args) {
       NodeTransitionFunction f = new NodeTransitionFunction(3, 33);
      
       System.out.println(f.apply(5));
   }
  
}

Note that if you implement apply() in a straightforward way, you will almost certainly exceed Javas Integer.MAX_VALUE: try i

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package functions;

import java.math.*; // for BigInteger

public class NodeTransitionFunction {

    private Integer exp;
    private Integer KVal;

    public NodeTransitionFunction(Integer exp, Integer KVal) {
        // CONSTRUCTOR: Sets the class to calculate f(x) = (x ^ exp) mod KVal
        this.exp = exp;
        this.KVal = KVal;
    }

    public Integer apply(Integer val) {
        Integer result = 1;
        for (int i = 0; i < exp; i++) {
            result *= val % KVal;
        }
        return result % KVal;
    }


    public BigInteger apply(BigInteger val) {
        BigInteger result = BigInteger.valueOf(1);
        for (int i = 0; i < exp; i++) {
            result = result.multiply(val);
        }
        return result.mod(BigInteger.valueOf(KVal));
    }


    public static void main(String[] args) {
        NodeTransitionFunction f = new NodeTransitionFunction(3, 33);
        System.out.println(f.apply(5));
    }
}

Add a comment
Know the answer?
Add Answer to:
***Code to solve*** package functions; import java.math.*; // for BigInteger public class NodeTransitionFunction {    public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT