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

  Write the methods used in section two of main

Write the constructor for the SortTraversableLinkedList. It should create the head and tail nodes, and set the size of the list to 0, as in a typical linked list.

  Write a gui program that maintains a set of contacts

Write a GUI program that maintains a set of contacts. Each contact has name, address, and a list of phone numbers. This contacts should be be stored in a HashMap

  Design a java application to read a file containing data

Design a Java application that will read a file containing data related to the passengers on the Titanic. The description of the file is shown below. The application should provide statistical results on the passengers including

  Implement a game called bunko-poker

Implement a game called Bunko-Poker. The gameplay is an easily programmed version of the popular game Yahtzee. Your program will make use of the supplied static functions Dice.roll() and, in cases where you might need the string ordered, Dice.ordered..

  Implement a simple paddle ball game

Implement a simple paddle ball game. Paddle Ball Game Overview The paddle ball game is a simplification of the Pong game. In the Pong game, a ball is moving around the display, bouncing off walls.

  The objective is to implement a menu-based system

The objective is to implement a menu-based system for Bank Accounts in order to simulate a very simple banking system. Many structures have to be declared to manage bank accounts.

  Determine the purpose of using javascriptr on a website

write a 200- to 300-word short-answer response to the followingwhat is the purpose of using javascriptr on a website?

  Portable file system implement a portable file system with

implement a portable file system with java language. i do have some requirements.implement a portable file system pfs.

  Creating a list of shapes stored in an array

A Sample program is provided that creates a list of shapes stored in an array. This program uses classes: Shapes, Square, Rectangle and ShapesList.

  If there are 4 command line arguments

If there are 4 command line arguments (a b c filename.dat) then the engine should take a,b,c and store the resulting x1 and x2 in filename.dat working in silent mode (no use of screen)

  A regular polygon is an n-sided polygon

A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree(i.e the polygon is both equilateral and equiangular).

  1 introductionin order to debug an application it is

1 introductionin order to debug an application it is sometimes useful to know where a given object comes from or where

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