Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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(); }

image text in transcribed

Q Search (+1) Files Servi. Start Page Mat2By2 javaMat2By2APl.java E Source Packages mat2by2 104 d Mat2By2.java Mat2By2APL.ja output-Mat2By2 (run) Test Packages Enter the top-left, top-right bottom-left and bottom-right entries of the first matrix: 34 45 67 87 Test Libraries Enter the top-left, top-right bottom-eft and bottom-right entries of the second matrix: 78 78 78 78 [[34, [(78, 45], 78], [67, [78, 8711 7811 m1- m2 (ml -m2) (m1 m2) [[112, 123], [123, 12311 + 1(m1 -m2)(n1 + m2)I =-1353 Which Fibonaci number do you wish to compute Fib (666) = 1 Fib (666)1 Empirical Analysis of naive vs. Fast Matrix Exponentiation in Generating 19 Terms of the Fibonacci Sequence getTopLweft Naviga.. Memb... Naive Time (10-6s) Fast Time (10^-65) Mat2By2: Mat2By2API 1000 1500 2008 2508 3008 3508 4000 4508 5008 5508 6000 6500 T00B 7508 8000 8500 2606 1140 1029 3078 Mat2By20 Mat2By2(Biginteger m Mat2By2(Mat2By2 m) 1086 1586 1261 o add(Mat2 By2API auge o determinanto : Biginte 1597 1325 O displayo O fastPower(int n): Mat2 fib-ta stint n): BigIntec fib-naive(int n) : Bialnti getBottor Left() : BigInt O getBottomRighto: Bigl get TopLeftO : Balntegi getTopLuveft0 : Biglnte 1521 1714 178 2328 1825 2561 1971 3505 1375 getTooRight0 main(Stringll args) O mat add(Mat2By2 m2 mat multiMat2Ru? m2 9508 10080 2089 1245 1530 1214 BUILD SUCCESSFUL (total time: 16 seconds<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions

Question

Complexity of linear search is O ( n ) . Your answer: True False

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago