I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out.
package exercise01;
/***************************************************************************
* <p> This program demonstrates the technique of recursion
and includes
* recursive methods that are defined for a variety of
mathematical
* functions.
*
* <br>A recursive method is one that directly or indirectly
calls itself
* and must include:
* <br>(1) end case: stopping condition
* which terminates/ends recursion
* <br>(2) reduction: reduces the problem into a subproblem,
which is a
* smaller or simpler version of the original
problem.
*
* <br> The recursive call is a call of the method with a
smaller or
* different argument. Normally, a recursive call reduces the
original
* problem by bringing it increasingly closer to an end case, until
it
* becomes the end case.
***************************************************************************/
public class RecursionClient {
/***********************************************************
* returns the result of an real value x to the nth
power.
* @param n the integer n
* @throws IllegalArgumentException for negative
exponents.
*
*********************************************************/
public static double exp(double x, int n) {
if (n==0)
return 1;
return x*exp(x,n-1);
}
/************************************************************
* returns the result of a factorial down to zero
factorial
* @param n positive integer and zero
* @throws IllegalArgumentException for negative
numbers.
*
**********************************************************/
public static int factorial(int n) {
if (n <= 1)
return 1;
return n*factorial(n-1);
}
/***********************************************************
* returns the result of the fibonacci sequence of
numbers.
* @param n the integer n
* @throws IllegalArgumentException for negative
numbers.
*
*********************************************************/
public static int fibonacci(int n) {
if (n==0)return 0;
if(n==1) return 1;
return
fibonacci(n-1)+fibonacci(n-2);
}
/***********************************************************
* returns the result of an integer x to the nth
power.
* @param n the integer n
* @throws IllegalArgumentException for negative
exponents.
*
*********************************************************/
public static int pow(int x, int n) {
/*
* power(x,n)
*/
if(n==1)
return x;
return x*pow(x,n-1);
}
/***********************************************************
* returns the result of the sum of n integers.
* @param n the integer n
* @throws IllegalArgumentException for negative
numbers.
*
*********************************************************/
public static int sum(int n) {
if (n==1)
return 1;
return n+sum (n-1);
}
/***********************************************************
* returns the result of the sum of n integers.
* @param n the integer n
* @throws IllegalArgumentException for negative
numbers.
*
*********************************************************/
public static int sumOdd(int n, int x) {
if(n==0)
return 0;
return x+sumOdd(n-1,x+2);
}
public static int sumOdd(int n) {
return sumOdd(n, 1);
}
/***********************************************************
* runs the program
* @param args program arguments
*
*********************************************************/
public static void main(String[] args) {
int n = 10;
//count of nth factorial
System.out.println("-------------
nth factorial --------------");
for (int i = 0; i < n; i++ )
{
System.out.print(i + "\t");
}
System.out.println();
//value for nth factorial
for (int i = 0; i < n; i++ )
{
System.out.print(factorial(i) + "\t");
}
System.out.println();
n = 12;
System.out.println();
//count of nth fibonacci
System.out.println("--------------
nth fibonacci -------------");
for (int i = 0; i < n; i++ )
{
System.out.print(i + "\t");
}
System.out.println();
//nth value in fibonacci
series
for (int i = 0; i < n; i++ )
{
System.out.print(fibonacci(i) + "\t");
}
System.out.println();
//two to the power of n
n = 16;
double two$n = Math.pow(2,
n);
System.out.println();
System.out.println("--------------
pow(2, n) -------------");
System.out.println("pow(2, n): " +
n + " gives " + two$n);
System.out.println("pow(2, n): " +
n + " gives " + pow(2, n));
System.out.println();
//e to the power of n
n = 8;
double e$n = Math.pow(Math.E,
n);
System.out.println("--------------
exp(x,n) -------------");
System.out.println("e(n): " + n + "
gives " + e$n);
System.out.println("exp(e, n): " +
n + " gives " + exp(Math.E, n));
n = 10;
System.out.println();
//summation of n integers
int sum = n * (n + 1) / 2;
System.out.println("--------------
sum(n) -------------");
System.out.println("sum of " + n +
" integers: " + sum);
System.out.println("sum of " + n +
" integers: " + sum(n));
n = 5;
System.out.println();
System.out.println("--------------
sumOdd(n) -------------");
//summation of first n odd
integers
int sumOdd = 0;
for (int i = 1; i < n + 1; i++ )
{
sumOdd = sumOdd
+ 2 * i - 1;
System.out.print(sumOdd + " ");
}
System.out.println();
System.out.println("first " + n + "
odd integers: " + sumOdd);
System.out.println("first " + n + "
odd integers: " + sumOdd(n));
}
}
Answer:-
You have included comments for almost entire code, but i have added some comments to base and recursive case
code :-
package exercise01;
/***************************************************************************
* <p> This program demonstrates the technique of recursion
and includes
* recursive methods that are defined for a variety of
mathematical
* functions.
*
* <br>A recursive method is one that directly or indirectly
calls itself
* and must include:
* <br>(1) end case: stopping condition
* which terminates/ends recursion
* <br>(2) reduction: reduces the problem into a subproblem,
which is a
* smaller or simpler version of the original
problem.
*
* <br> The recursive call is a call of the method with a
smaller or
* different argument. Normally, a recursive call reduces the
original
* problem by bringing it increasingly closer to an end case, until
it
* becomes the end case.
***************************************************************************/
public class RecursionClient {
/***********************************************************
* returns the result of an real value x to the nth
power.
* @param n the integer n
* @throws IllegalArgumentException for negative
exponents.
*
*********************************************************/
public static double exp(double x, int n) {
if
(n==0)
//if n equals 0 return 1 means we need to multiply untill 1
only
return
1;
return
x*exp(x,n-1); // multiplying n with n upto n
times
}
/************************************************************
* returns the result of a factorial down to zero
factorial
* @param n positive integer and zero
* @throws IllegalArgumentException for negative
numbers.
*
**********************************************************/
public static int factorial(int n) {
if (n <=
1)
// factorial end case ,because we have to multiply each number
untill 1
return 1;
return n*factorial(n-1);
// n*n-1 ..... 1
}
/***********************************************************
* returns the result of the fibonacci sequence of
numbers.
* @param n the integer n
* @throws IllegalArgumentException for negative
numbers.
*
*********************************************************/
public static int fibonacci(int n) {
if (n==0)return
0; // fibanocci starts
from adding 0 +1
if(n==1) return
1; // this is also for
same cause (adding 0+1)
return
fibonacci(n-1)+fibonacci(n-2); //for adding adjacent numbers so we
are calling two functions for two below numbers
}
/***********************************************************
* returns the result of an integer x to the nth
power.
* @param n the integer n
* @throws IllegalArgumentException for negative
exponents.
*
*********************************************************/
public static int pow(int x, int n) {
/*
* power(x,n)
*/
if(n==1)
// for multiplying untill 1 not 0 like exponential
return x;
return x*pow(x,n-1);
}
/***********************************************************
* returns the result of the sum of n integers.
* @param n the integer n
* @throws IllegalArgumentException for negative
numbers.
*
*********************************************************/
public static int sum(int n) {
if
(n==1)
//adding upto 1
return
1;
return n+sum
(n-1); //decreasing number by
one
}
/***********************************************************
* returns the result of the sum of n integers.
* @param n the integer n
* @throws IllegalArgumentException for negative
numbers.
*
*********************************************************/
public static int sumOdd(int n, int x) {
if(n==0)
return 0;
return
x+sumOdd(n-1,x+2); //for adding odd
numbers
}
public static int sumOdd(int n) {
return sumOdd(n, 1);
}
/***********************************************************
* runs the program
* @param args program arguments
*
*********************************************************/
public static void main(String[] args) {
int n = 10;
//count of nth factorial
System.out.println("------------- nth factorial
--------------");
for (int i = 0; i < n; i++
) {
System.out.print(i + "\t");
}
System.out.println();
//value for nth
factorial
for (int i = 0; i < n; i++
) {
System.out.print(factorial(i) + "\t");
}
System.out.println();
n = 12;
System.out.println();
//count of nth fibonacci
System.out.println("-------------- nth fibonacci
-------------");
for (int i = 0; i < n; i++
) {
System.out.print(i + "\t");
}
System.out.println();
//nth value in fibonacci
series
for (int i = 0; i < n; i++
) {
System.out.print(fibonacci(i) + "\t");
}
System.out.println();
//two to the power of n
n = 16;
double two$n = Math.pow(2,
n);
System.out.println();
System.out.println("-------------- pow(2, n) -------------");
System.out.println("pow(2, n):
" + n + " gives " + two$n);
System.out.println("pow(2, n):
" + n + " gives " + pow(2, n));
System.out.println();
//e to the power of n
n = 8;
double e$n = Math.pow(Math.E,
n);
System.out.println("-------------- exp(x,n) -------------");
System.out.println("e(n): " +
n + " gives " + e$n);
System.out.println("exp(e, n):
" + n + " gives " + exp(Math.E, n));
n = 10;
System.out.println();
//summation of n
integers
int sum = n * (n + 1) /
2;
System.out.println("-------------- sum(n) -------------");
System.out.println("sum of " +
n + " integers: " + sum);
System.out.println("sum of " +
n + " integers: " + sum(n));
n = 5;
System.out.println();
System.out.println("-------------- sumOdd(n) -------------");
//summation of first n odd
integers
int sumOdd = 0;
for (int i = 1; i < n + 1;
i++ ) {
sumOdd
= sumOdd + 2 * i - 1;
System.out.print(sumOdd + " ");
}
System.out.println();
System.out.println("first " +
n + " odd integers: " + sumOdd);
System.out.println("first " +
n + " odd integers: " + sumOdd(n));
}
}
I just need to add comment for the code blow. Pleas add comment for each method...
File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...
Use the Summation recursive program you did in the class to also work with minus integers. For example, the sum of -3 will be -6 which is (-3)+(-2)+(-1)+0. USE THIS CODE package project5; import java.util.Scanner; public class SingleRecursion { /** Main method */ public static long sum(int n) { if (n<0) throw new IllegalArgumentException ("Can't calculate factorial of negative"); if (n==1) return 1; else if (n==0) return 1; else ...
You must implement the following methods using Java's Stack Object. /** * Computes the factorial of n * @param n-integer value greater or equal to 0 * @return n! */ public static int factorial( int n ) { } /** * Computes the nth term of the Fibonacci sequence * @param n -nth term to find * @return -the nth term */ public static int fibonacci( int n ) {} /** * Find the min value using the comparable interface...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
How do I separate the method into different class? I try
separate the testing and method class into 2 different class. And
when I test it, it said that the method is undefined. And it ask me
to create the method in the main class. I don't know what is the
problem.
This is the access to the code
https://repl.it/@Teptaikorn/test
Thank you very much
MazeSolver.java MazeTerster.... TwoDim AutoA... testfile.txt Main.java x > 0 N Binary TreeN... X LinkedBinary... Maze.java 1...
I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...
This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and how can I fix this?? import java.util.Scanner; import java.io.*; public class Topic7Hw { static Scanner sc = new Scanner (System.in); public static void main(String[] args) throws IOException { PrintWriter outputFile = new PrintWriter ("output.txt"); outputFile.println("hello"); final int MAX_NUM = 10;...
Java, how would i do this
public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...
I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...