Question
Hello, I'm suppose to define a class, Mat2By2, that implements the Mat2By2API interface in Java using NetBeans IDE 8.2. I have it running and working,
Hello, I'm suppose to define a class, Mat2By2, that implements the Mat2By2API interface in Java using NetBeans IDE 8.2. I have it running and working, but the part when it prompts the user on "Which Fibonaci number do you wish to compute?" When inputing any number, it is expected to output a result of series of numbers but instead only outputs the number 1. The way it is suppose to output for example is when the user inputs the number 250 for the fibonacci as prompted by the program, the out put should generate 789632582613173050928273894363433289368675876375. Below are the codes I use for this program and my sample output. Help on resolving this issue is appreciative. Thanks.
Mat2By2
package mat2by2
import java.math.BigInteger; import java.util.Scanner;
public class Mat2By2 implements Mat2By2API { // declares the big integers needed for the Matrix Elements (me) for the program BigInteger me11; BigInteger me12; BigInteger me21; BigInteger me22; // default constructgor initialized the matrix to identitiy matrix public Mat2By2(){ this.me11 = BigInteger.valueOf(1); this.me12 = BigInteger.valueOf(0); this.me21 = BigInteger.valueOf(0); this.me22 = BigInteger.valueOf(1); } /** * Creates a 2x2 matrix with the specified entries * @param me11 a BigInteger object representing top-left entry * @param me12 a BigInteger object representing top-right entry * @param me21 a BigInteger object representing bottom-left entry * @param me22 a BigInteger object representing bottom-right entry */ public Mat2By2(BigInteger me11, BigInteger me12, BigInteger me21, BigInteger me22){ this.me11 = me11; this.me12 = me12; this.me21 = me21; this.me22 = me22; } // copy constructor with elements same as another given matrix public Mat2By2(Mat2By2 m){ this.me11 = m.me11; this.me12 = m.me12; this.me21 = m.me21; this.me22 = m.me22; } // computes the sum of the matrix public Mat2By2 mat_add(Mat2By2 m2){ Mat2By2 result = new Mat2By2(); Mat2By2 m1 = new Mat2By2(this); result.me11 = m1.me11.add(m2.me11); result.me12 = m1.me12.add(m2.me12); result.me21 = m1.me12.add(m2.me21); result.me22 = m1.me12.add(m2.me22); return result; } // computes the subtraction of the matrix public Mat2By2 mat_sub(Mat2By2 m2){ Mat2By2 result = new Mat2By2(); Mat2By2 m1 = new Mat2By2(this); result.me11 = m1.me11.subtract(m2.me11); result.me12 = m1.me12.subtract(m2.me12); result.me21 = m1.me21.subtract(m2.me21); result.me22 = m1.me22.subtract(m2.me22); return result; } // computes the multiplication of the matrix. public Mat2By2 mat_mult(Mat2By2 m2){ Mat2By2 result = new Mat2By2(); Mat2By2 m1 = new Mat2By2(); result.me11 = m1.me11.multiply(m2.me11).add(m1.me12.multiply(m2.me21)); result.me12 = m1.me11.multiply(m2.me12).add(m1.me12.multiply(m2.me22)); result.me21 = m1.me21.multiply(m2.me11).add(m1.me22.multiply(m2.me21)); result.me22 = m1.me21.multiply(m2.me12).add(m1.me22.multiply(m2.me22)); return result; } // this function returns the nth Fibonacci number using the fast power method static BigInteger fib_fast(int n) { Mat2By2 F = new Mat2By2(BigInteger.valueOf(1),BigInteger.valueOf(1),BigInteger.valueOf(1),BigInteger.valueOf(0)); if (n == 0) return BigInteger.valueOf(0); Mat2By2 res = new Mat2By2(F.fastPower(n-1)); return res.me11; } // function that returns the nth Fibonacci number using the naive method static BigInteger fib_naive(int n) { Mat2By2 F = new Mat2By2(BigInteger.valueOf(1),BigInteger.valueOf(1),BigInteger.valueOf(1),BigInteger.valueOf(0)); if (n == 0) return BigInteger.valueOf(0); Mat2By2 res = new Mat2By2(F.naivePower(n-1)); return res.me11; } // calculate the matrix determinant public BigInteger determinant(){ BigInteger ans; ans = this.me11.multiply(this.me22).subtract(this.me21.multiply(this.me12)); return ans; }
// implements methods from the Mat2By2API interface public BigInteger getBottomRight(){ return me22; } public BigInteger getBottomLeft(){ return me21; } public BigInteger getTopRight(){ return me12; } public BigInteger getTopLweft(){ return me11; }
// nth of power of the matrix naive method public Mat2By2 naivePower(int n){ Mat2By2 result = new Mat2By2(); Mat2By2 m = new Mat2By2(this); for(int i=0; i /** * @param args the command line arguments */ // main method of the program public static void main(String[] args) { Scanner scan = new Scanner(System.in); Mat2By2 m1; Mat2By2 m2; // declares variables for the user to input numbers in the following matrices BigInteger tl,tr, bl, br; //tl(top-left), tr(top-right), bl(bottom-left), br(bottom-right) // prompts user to make input for the first matrix System.out.println("Enter the top-left, top-right bottom-left and bottom-right entries of the first matrix: "); tl = scan.nextBigInteger(); tr = scan.nextBigInteger(); bl = scan.nextBigInteger(); br = scan.nextBigInteger(); m1 = new Mat2By2(tl,tr,bl,br); // prompts user to make input for the second matrix System.out.println(" Enter the top-left, top-right bottom-left and bottom-right entries of the second matrix: "); tl = scan.nextBigInteger(); tr = scan.nextBigInteger(); bl = scan.nextBigInteger(); br = scan.nextBigInteger(); m2 = new Mat2By2(tl,tr,bl,br); // displays matrix m1 System.out.print(" m1 = "); m1.display(); // displays matrix m2 System.out.print("m2 = "); m2.display(); // prints the results of the matrix product System.out.print(" (m1 - m2)(m1 + m2) = "); Mat2By2 m = m1.mat_sub(m2).mat_mult(m1.mat_add(m2)); m.display(); // prints the determinant of the matrix product System.out.print("|(m1 - m2)(m1 + m2)| = " + m.determinant()); System.out.print(" "); // prints the result of matrices based on m1-m2 & m1+m2 created from the user's input System.out.println(" Which Fibonaci number do you wish to compute? "); int n = scan.nextInt(); System.out.println("Fib("+n+") = "+fib_fast(n)); // using fast power method System.out.println("Fib("+n+") = "+fib_naive(n)); // using naive method // outputs the analysis table System.out.println(" Empirical Analysis of naive vs. Fast Matrix Exponentiation in Generating 19 Terms of the Fibonacci Sequence. "); System.out.println("============================================================"); System.out.println("Term\t\tNaive Time(10^-6s)\tFast Time(10^-6s)"); System.out.println("------------------------------------------------------------"); // assigns the integer to 1000 as the start of the empirical analysis instructed in the assignment n = 1000; long fast_time, naive_time; // lets the empirical analysis run until it reaches 10000. while(n @Override public Mat2By2API add(Mat2By2API augend) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public Mat2By2API subtract(Mat2By2API subtrahend) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public Mat2By2API multiply(Mat2By2API multiplier) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public BigInteger getTopLeft() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } } Mat2By2API package mat2by2; import java.math.BigInteger; /* * The interface for a streamlined 2x2 matrix whose * entries are 'Big Integers'. This interface provides the * public instance methods for basic 2x2 matrix operations: * addition, subtraction, multiplication and determinant. * @author Duncan * @since 99/99/9999 */ public interface Mat2By2API { /** * Computes the sum of this matrix and the specified matrix * @param augend holds a 2x2 BigInt matrix * @return the sum of this 2x2 matrix and the specified 2x2 matrix * @throws IllegalArgumentException when augend is not a Mat2By2 object. */ Mat2By2API add(Mat2By2API augend); /** * Computes the difference of this matrix and the specified matrix * @param subtrahend holds a 2x2 BigInt matrix * @return the difference of this 2x2 matrix and the specified 2x2 matrix * @throws IllegalArgumentException when subtrahend is not a Mat2By2 object. */ Mat2By2API subtract(Mat2By2API subtrahend); /** * Computes the product of this matrix and the specified matrix * @param multiplier holds a 2x2 BigInt matrix * @return the product of this 2x2 matrix and the specified 2x2 matrix * @throws IllegalArgumentException when multiplier is not a Mat2By2 object. */ Mat2By2API multiply(Mat2By2API multiplier); /** * Gives the determinant of this matrix * @return the determinant of this matrix */ BigInteger determinant(); /** * Gives the string representation of this matrix in in-line format: * [[top-left, top-right], [bottom-left, bottom-right]] * @return a string representation of this 2x2 matrix in in-line * notation. */ String toString(); /** * Gives the top-left entry of this matrix * @return the top-left entry */ BigInteger getTopLeft(); /** * Gives the top-right entry of this matrix * @return the top-right entry */ BigInteger getTopRight(); /** * Gives the bottom-left entry of this matrix * @return the bottom-left entry */ BigInteger getBottomLeft(); /** * Gives the bottom-right entry of this matrix * @return the bottom-right entry */ BigInteger getBottomRight(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started