Modify the factorial method with a recursive call

Assignment Help JAVA Programming
Reference no: EM13941536

It is interesting to watch recursion "in action." Modify the factorial method in Fig. 15.3 to print its local variable and recursive-call parameter. For each recursive call, display the outputs on a separate line, and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful. Your goal here is to design and implement an output format that makes it easier to understand recursion. You may want to add such display capabilities to other recursion examples and exercises throughout the text.

// FactorialCalculator.java
// Recursive factorial method.

public class FactorialCalculator
{
// recursive declaration of method factorial
public long factorial( long number )
{
if ( number <= 1 ) // test for base case
return 1; // base cases: 0! = 1 and 1! = 1
else // recursion step
return number * factorial( number - 1 );
} // end method factorial

// output factorials for values 0-10
public void displayFactorials()
{
// calculate the factorials of 0 through 10
for ( int counter = 0; counter <= 10; counter++ )
System.out.printf( "%d! = %dn", counter, factorial( counter ) );
} // end method displayFactorials
} // end class FactorialCalculator
---------------------------------------------------------------------------

// Fig. 15.4: FactorialTest.java
// Testing the recursive factorial method.

public class FactorialTest
{
// calculate factorials of 0-10
public static void main( String args[] )
{
FactorialCalculator factorialCalculator = new FactorialCalculator();
factorialCalculator.displayFactorials();
} // end main
} // end class FactorialTest
----------------------------------------------------------------------

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800

Attachment:- FactorialCalculator.zip

Reference no: EM13941536

Questions Cloud

Annualized dividend yield-portfolio with a beta : The S&P 500 Index price is 1492.28 and its annualized dividend yield is 2.30%. LIBOR is .2%. How many futures contracts will you need to hedge a $240 million portfolio with a beta of 1.16 for one year?
Summarize your thoughts on microsoft project : Write a two to three (2-3) page paper in which you summarize your thoughts on Microsoft Project: Give your opinion on how easy or difficult MS Project seems to be from a user experience standpoint
What would be the total combined direct labor cost : What would be the total combined direct labor cost for the two months? A) $122,752.00B) $120,736.00C) $120,881.60D) $122,606.40
Feature and parametric analysis : A comprehensive analysis of existing and similar offerings on the market already including a feature and parametric analysis indicating how your proposal fits into the existing scenario.
Modify the factorial method with a recursive call : You may want to add such display capabilities to other recursion examples and exercises throughout the text.
Value of each investment based on requried rate of return : You have finally saved $10,000 and are ready to make your first investment. You have the three following alternatives for investing that money: Capital Cities ABC, Inc. bonds with a par value of $1,000 and a coupon interest rate of 8.75 percent, are ..
Study the demographic profiles of the tourists : To study the demographic profiles of the tourists such as ages, genders, monthly total income, etc.
Decide among several investment alternatives : Nicole Nelson has come into an inheritance from her grandparents. She is attempting to decide among several investment alternatives. The return after one year is dependent primarily on the interest rate during the next year.
Does the criminal justice system have prejudicial overtones : Measures to avoid injustice and oppression at the hands of the criminal justice system-with or without the use of capital punishment. Does the criminal justice system have prejudicial overtones? How can these be addressed?

Reviews

Write a Review

JAVA Programming Questions & Answers

  What happen if one servlet makes references to other servlet

With regard to automatic servlet reloading in Tomcat 3.1, what happens if one servlet makes references to other servlets in the same webapp?

  Write program in java for total amount of customer-s order

Write down program in Java which would ask for clerk to enter total amount of customer's order. Program will then compute seven percent (7%) sales tax.

  Write a class encapsulating a pc-based game

Write a class encapsulating a PC-based game, which inherits from Game. A PC-bases game has the following additional attributes: the minimum megabytes of RAM needed to play the fame, the number of megabytes needed on the hard drive to install the fame..

  Design an object-oriented java application

You are to design an object-oriented Java application to let the user play a dice game that uses two dices. The player bets on a value and the dices roll

  Determine the best way to gather business rules

You have been hired by Happy Health to determine the best way to gather business rules for a new phone system.

  Create a bar chart that illustrates sales.

Hal's Used Cars has four salespeople named Pam, Leo, Kim, and Bob. Accept values for the number of cars sold by each salesperson in a month, and create a bar chart that illustrates sales. Save the file as BarGraph.java.

  Using the following data definition classpublic class

using the following data definition classpublic class circle private int radiuspublic circleint r radius rpublic int

  Calculate your age in the year 2060.

Declare and initialize an integer variable named currentYear. Initialize this variable with the value of the currentYear. Use four digits for year. Use four digits for the year.

  Create a class named movie that can be used with a video

extend above with a Rental class. This class should store a Movie that is rented, an integer representing the ID of the customer who rented the movie, and an integer indicating how many days late the movie is.

  Write an interface for an abstract method

Write an interface, PointingDevice, containing:  an abstract method, getXCoord that returns an int and an abstract method, getYCoord that returns an int.

  Write a class that has three overloaded static methods

Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes

  Java program to determine roots of a quadratic equation

Write a java program to determine roots of a quadratic equation, the program should use GUI to display the output, either complex or simple roots.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd