Java Program: how functions can be defined both recursively and by formulas usin
ID: 638497 • Letter: J
Question
Java Program: how functions can be defined both recursively and by formulas using standard mathematical operations.
In each of the 3 cases you are asked to implement a function in two ways(recursively and non-recursively). You are then to write a program that computes the values of the function in both ways and verifies that both methods return the same values. Compute the functions for a range of values, say up to 15. Each case will consist of one Java file(one class for each) with the two functions implemented as methods and the main program.
When implementing the functions, each should take a single long parameter and return a long value. No user input. Assigned any vaule to the long.
In one case the two methods actually do not match.
This is very important: Put a comment at the top of each Java file that explains whether the two methods of computing match or do not match. This is a very important part of this project, you will lose points for omitting this analysis.
Case A
The first function can be computed by the simple formula
A(x) = x2
The function can also be computed recursively:
A(0) = 0
A(1) = 1
A(2) = 4
A(n) = 3
Explanation / Answer
Here you go :)
Comment if you have any doubts.
//##########################################################################
//CaseA class
/*
* Both the methods return the same values
*/
public class CaseA
{
public static void main(String[] args)
{
for(int i=0;i<15;i++)
{
System.out.println("For x= "+i);
System.out.println("Method1: "+method1(i));
System.out.println("Method2: "+method2(i));
}
}
public static long method1(long x)
{
return x*x;
}
public static long method2(long x)
{
if(x==0)return 0;
if(x==1)return 1;
if(x==2)return 4;
return 3*method2(x-1)-3*method2(x-2) +method2(x-3);
}
}
//##########################################################################
//CaseB class
/*
* Both the methods returns the same values
*/
public class CaseB
{
public static void main(String[] args)
{
for(int i=0;i<15;i++)
{
System.out.println("For x= "+i+" Method1: "+method1(i)+" Method1: "+method2(i));
}
}
public static long method1(long x)
{
return (long)(x*x*Math.pow(2, x)+x*Math.pow(3, x));
}
public static long method2(long x)
{
if(x==0)return 0;
if(x==1)return 5;
if(x==2)return 34;
if(x==3)return 153;
if(x==4)return 580;
return 12*method2(x-1)-57*method2(x-2) +134*method2(x-3)-156*method2(x-4)+72*method2(x-5);
}
}
//##################################################################################
//CaseC class
/*
* The methods return different values
*/
public class CaseC
{
public static void main(String[] args)
{
for(int i=0;i<15;i++)
{
System.out.println("For x= "+i+" Method1: "+method1(i)+" Method1: "+method2(i));
}
}
public static long method1(long x)
{
return 12+8*x-3*x*x+x*x*x;
}
public static long method2(long x)
{
if(x==0)return 12;
if(x==1)return 18;
return 2*method2(x-1)-method2(x-2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.