Question: Please help me code the following in: JAVA! Please read the instructions THOROUGHLY and use many COMMENTS! Full points will be awarded, thanks in advance!
Please help me code the following in: JAVA!
Please read the instructions THOROUGHLY and use many COMMENTS!
Full points will be awarded, thanks in advance!
Code guide:
/*------------------------------------------------------------------------
-----------
*
* sum( n ) is a summation algorithm defined as follows:
*
* (1) sum( n ) = n + (n-1) + (n-2) + ... + 1
* (1a) sum( 1 ) = 1
*
* and from this definition, we can rewrite this formula in terms of
itself, such that:
*
* (2) sum( n ) = n + sum( n - 1 )
*
* and we can do this again
*
* (3) sum( n ) = n + ( n - 1) + sum( n - 2 )
*
* and so on, and so forth, we finally end up with the same as above
*
* (1) sum( n ) = n + (n-1) + (n-2) + ... + 1
*
*------------------------------------------------------------------------
----------- */
import java.awt.Dimension;
import javax.swing.*;
public class RecursionLab {
private static JTextArea myArea = new JTextArea();
private static int count = 0;
public static void main( String args[] ) { //invoke the recursive
method here...
/**
* TODO: switch between the two commented lines below and
execute this code,
* observing the output for both the iterative solution
and the recursive solution.
* To watch the recursive behaviour in action, set a
breakpoint on the if statement
* inside the recursiveSum() function
*
*/
int solution = iterativeSum( 20 );
//int solution = recursiveSum( 20 );
//Some GUI details
myArea.setText(("Result is : " + solution + " " +
myArea.getText()));
JScrollPane myPane = new JScrollPane( myArea );
myPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALW
AYS);
myPane.setPreferredSize(new Dimension(600,300));
JOptionPane.showMessageDialog( null, myPane );
//good form to include an exit call when GUIing in Java
System.exit(0);
}
/** recursion is similar to iterative looping, but we
* use method calls to repeat computations (or decompose the
problem)
* instead of explicit looping control structures
*/
public static int recursiveSum( int n ) {
updateRecursiveDisplay(n); //overhead for
nice output, not required
if( n == 1) //if we're at the base case...
return 1; //then return the answer to
the simplest problem which we know how to solve
else //otherwise, we rely on the fact
that sum( n ) = n + sum( n - 1 ) and keep recursing
return ( n + recursiveSum( n - 1) );
} //for this method to terminate, we
must be breaking the problem down into smaller
//and
smaller problems, until we reach the simplest form of the problem which we
know
//how to
solve (in this case, it's the fact that sum( 1 ) == 1 )
//the iterative counterpart to the above recursion
/otice how it's longer? At times, an iterative solution may
require more code than the recursive counterpart,
//but, the recursive solution is slower and more memory intensive.
We can always recast recursion as iteration.
public static int iterativeSum( int i ) {
int total = 0;
for( int n = i; n >= 1; n--) {
updateIterativeDisplay(n);
total = total + n;
}
return total;
}
public static void updateIterativeDisplay(int n) {
count++;
String text = myArea.getText();
text += " /*******************Loop iteration " + count +
"**************************************";
text += " Calling iterativeSum( int n = " + n +" ).
Total += " + n;
text +=
" ***********************************************************************
****/";
myArea.setText( text );
}
//ignore this method unless interested in the output string
public static void updateRecursiveDisplay(int n) {
count++;
String text = myArea.getText();
if( count == 1 ) {
text += " return ( n + recursiveSum(
n - 1 ) ) ";
text += " CALL STACK IN MAIN MEMORY
";
}
text += " /*******************Method invocation " +
count + "*********************";
text += " Calling recursiveSum( int n = " + n +" ). ";
text += " The return statement from this function will
resolve in " + (n-1) + " more recursive method calls...";
if( n != 1 ) {
text += " The return statement which
invokes the recursive call is \"return ( " + n + " + recursiveSum( "+ (n -
1) +" ));";
} else {
text += " The base case has been hit. The
return statement is \"return 1;\" which is the value returned to the
expression above. ";
text += " Notice how hitting the base case
will provide a solid, known piece of information from which we will
construct more known ";
text += " information by bubbling up
through all of the other, yet-to-be-determined return expressions";
}
text +=
" ***********************************************************************
****/";
2. From Summation to Factorial The definition of the factorial function is defined below. Using the summation code as a guide, write a very similar recursive method that calculates the factorial of some input n. This method should be like all recursive functions in that it knows how to solve a trivial case (case1), and the function knows how to decompose larger cases into smaller cases (case2). Just as for and while loops use a boolean expression to govern loop termination, so too will we use a boolean condition in combination with an if to control repetition, as demonstrated here if (case1)( return base case solution; //for summation, factorials, exponent, and Fibonacci, this is 1 else (2) result recursive call return result; /typically the recursive call decrements by 1 or n/2 Build this new recursive method and test your code with a short main driver. 2. From Summation to Factorial The definition of the factorial function is defined below. Using the summation code as a guide, write a very similar recursive method that calculates the factorial of some input n. This method should be like all recursive functions in that it knows how to solve a trivial case (case1), and the function knows how to decompose larger cases into smaller cases (case2). Just as for and while loops use a boolean expression to govern loop termination, so too will we use a boolean condition in combination with an if to control repetition, as demonstrated here if (case1)( return base case solution; //for summation, factorials, exponent, and Fibonacci, this is 1 else (2) result recursive call return result; /typically the recursive call decrements by 1 or n/2 Build this new recursive method and test your code with a short main driver
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
