CS 2130 Computational structures Write Java programs that use command-line input
ID: 3908879 • Letter: C
Question
CS 2130 Computational structures
Write Java programs that use command-line input and call the following integer functions. All calculations for each function should be encapsulated within the function. Use a long datatype instead of int for all input and output variables. Do NOT use string variables in any of the algorithms. A sample main program will be provided as an example.
Turn in source code for each function, along with test output.For each of the above functions, state whether or not the function is one-to-one. Justify your answers. If a function is not one-to-one, show two inputs that have the same output.
5. RecursiveSeq: The input variable X is an integer between 1 and 50. The function should return as Y the X-th term of a sequence defined recursively by:f(1) = 1f(2) = 3f(X) = 2*f(X-1) – 2*f(X-2) for X = 3,4,5,...Your function code should use recursion (not a loop). Test your function with the following inputs:X = 2 X = 10 X = 24 X = 33 X = 40
Explanation / Answer
public class RecursiveSeq { public static long RecursiveSeq(long X) { if(X == 1) { return 1; } else if(X == 2) { return 3; } else { return (2 * RecursiveSeq(X-1)) - (2 * RecursiveSeq(X - 2)); } } public static void main(String[] args) { System.out.println(RecursiveSeq(2)); System.out.println(RecursiveSeq(10)); System.out.println(RecursiveSeq(24)); System.out.println(RecursiveSeq(33)); System.out.println(RecursiveSeq(40)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.