Question
Javascript Problem. Trying to modify existing fibonacci code in order to use a class Int couple as a return value instead of an array? Here
Javascript Problem. Trying to modify existing fibonacci code in order to use a class Int couple as a return value instead of an array? Here is the initial fibonacci code, which prints the nth number as dictated by number set in print line (bolded, arbitrarily set at 40): public class FibonacciProg { public static int fibonacci(int numberA){ if (numberA == 0){ return 0; } else{ int fib2Array[] = fib2(numberA); return fib2Array[0]; } } public static int[] fib2(int numberA){ if (numberA == 1 || numberA == 2){ // include numberA === 2, because 2nd fibonacci number is also 1 return new int[] {1, 1}; // change this to return {1, 1} } else{ int[] Q = fib2(numberA-1); int f1 = Q[0]; int f2 = Q[1]; return new int[] {f1+f2, f1}; } } public static void main(String[] args) { System.out.print("Fibonacci"+fibonacci(40); } System.out.println(); } }
I'm trying to modify this such that IntCouple will be used in place of an array as a return type.
IntCouple should have:
1. two non-private attributes, initial_instance and secondary_instance which are both ints.
2. a constructor that accepts two int parameters and initializes the two attributes initial_instance and secondary_instance.
..Such that new IntCouple(initializers) //where initializers is the initial values for the object creates a new initialized instance of IntCouple.
So would the code to achieve this just be:
public static class IntCouple(int x, int y) {
public static int Initial_instance = x;
public static int secondary_instance = y;
}
Or do I just want:
public static class IntCouple {
public static int initial_instance;
public static int secondary_instance;
}
I'm just a little confused how to construct a class to be used as a return type for another function (to be used in place of an array), and then how I integrate this into the code I've already written to find the nth fibonacci number. I don't really understand using a class within a class so any help would be very appreciated. Thank you.
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