Answer the given question with a proper explanation and step-by-step solution. public class mathFormula { // Iterative implementation public static int iterative(int n) { if (n == 0) return 1; if (n == 1) return 2; int f1 = 1, f2 = 2, fn = 0; for (int i = 2; i <= Math.abs(n); i++) { fn = f1 - f2; f1 = f2; f2 = fn; } return (n < 0 && n % 2 == 0) ? -fn : fn; } // Recursive implementation public static int recursive(int n) { if (n == 0) return 1; if (n == 1) return 2; if (n > 1) return recursive(n - 1) - recursive(n - 2); return -recursive(-n); } // Memoization implementation public static int memoization(int n) { int[] memo = new int[Math.abs(n) + 1]; memo[0] = 1; memo[1] = 2; return memoizationHelper(n, memo); } public static int memoizationHelper(int n, int[] memo) { if (memo[Math.abs(n)] != 0) return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; memo[Math.abs(n)] = memoizationHelper(n - 1, memo) - memoizationHelper(n - 2, memo); return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; } // Main method to test the implementations public static void main(String[] args) { int n = -10; System.out.println("Iterative: " + iterative(n)); System.out.println("Recursive: " + recursive(n)); System.out.println("Memoization: " + memoization(n)); } } //Hi i tried to run the above code but i kept receiving errors below. Kind assist me and correct it

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Answer the given question with a proper explanation and step-by-step solution.

public class mathFormula { // Iterative implementation public static int iterative(int n) { if (n == 0) return 1; if (n == 1) return 2; int f1 = 1, f2 = 2, fn = 0; for (int i = 2; i <= Math.abs(n); i++) { fn = f1 - f2; f1 = f2; f2 = fn; } return (n < 0 && n % 2 == 0) ? -fn : fn; } // Recursive implementation public static int recursive(int n) { if (n == 0) return 1; if (n == 1) return 2; if (n > 1) return recursive(n - 1) - recursive(n - 2); return -recursive(-n); } // Memoization implementation public static int memoization(int n) { int[] memo = new int[Math.abs(n) + 1]; memo[0] = 1; memo[1] = 2; return memoizationHelper(n, memo); } public static int memoizationHelper(int n, int[] memo) { if (memo[Math.abs(n)] != 0) return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; memo[Math.abs(n)] = memoizationHelper(n - 1, memo) - memoizationHelper(n - 2, memo); return (n < 0 && n % 2 == 0) ? -memo[Math.abs(n)] : memo[Math.abs(n)]; } // Main method to test the implementations public static void main(String[] args) { int n = -10; System.out.println("Iterative: " + iterative(n)); System.out.println("Recursive: " + recursive(n)); System.out.println("Memoization: " + memoization(n)); } } //Hi i tried to run the above code but i kept receiving errors below. Kind assist me and correct it. Thank you

 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 11 out of bounds for length 11
   at mathFormula.memoizationHelper(mathFormula.java:32)
   at mathFormula.memoizationHelper(mathFormula.java:33)
   at mathFormula.memoization(mathFormula.java:28)
   at mathFormula.main(mathFormula.java:42)

Process finished with exit code 1

Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education