A3: Factorials of Interest

On This Page


Overview

During this assignment, you will:

  • Calculate the factorial of a number n using a loop
  • Use factorials to approximate the mathematical constant e
  • Use factorials to calculate ex
  • Use ex to calculate continuous interest
  • Use static methods to organize a utility class

Background Information

The factorial of a nonnegative integer n is written as n! (pronounced "n factorial"). The definition of n! is:

When n = 0

n! = 1

And for values of n greater than 0:

n! = n * (n-1) * (n-2) * (n-3) * ... * 1

Some examples:

0! = 1
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120

The letter e is used as a symbol for the base of the natural logarithms and is about 2.71828. You can calculate e to some precision, using factorials, as follows: (more...)

e = 1 + 1 /1! + 1/2! + 1 /3! + ...

Which is mathematically stated as:

The exponential of the form ex occurs frequently in practice. For instance, calculation of continuous interest uses ex. We can calculate ex using the following formula:

ex = 1 + x / 1! + x2 / 2! + x3 / 3! + ...

Note that you can calculate continuous interest for the final value S of some initial investment P for some percentage rate r over some term in years t using the following formula: (more...)

S = Pert

Specifications

Write an application to calculate the final value of an investment using continuous interest. Request the initial investment, annual percentage rate and term in years from the command line. Display the final value of your investment to the screen.

Sample Output

This program calculates the final value of
an investment using the continuous interest method.

Initial investment: 100
Annual percentage rate: .05
Term in years: 2

Final value: $110.52
Recursive Final value: $110.52

Additional Specifications

You must create two static methods for this assignment, though you may have as many additional methods as you like.

The first method must calculate and return ex. The required accuracy must be to within +/- 0.00001 of Java's Math.exp() method. The method header (signature) must be:

public static double exp(double x)

The second method calculates the return on an investment using continuous interest. After accepting arguments for initial investment, annual percentage rate and term in years, the method returns the final value of the investment. The method header must be:

public static double finalValue(double pv, double rate, double term)

The name of your class must be InterestCalculator. Do not change the name of the class or method headers (signatures) from those specified or you will loose points.

Do not use any methods from the java.lang.Math class in this assignment.

Test Cases

Testing is an important part of software development. Usually, programmers develop tests for each unit of code they produce. This is know as "unit testing" and allows the programmer to verify that the code they develop works correctly.

To assist you in developing your code, I provide tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.

Install the tests by copying the following file into the same directory as your .java source code files:

Run the tests as you would any other Java program. You may need to compile your source code before compiling and running the test code.

You can use the following calculator to generate test values of your own. Enter values for the three arguments and press the calculate button.

Principle: Rate: Term:

Extra Credit

The following are worth extra credit points:

  1. Create a second exponential function that uses recursion to calculate ex. (2 points)
  2. Format the output to display only two decimal places. (1 point)
  3. To accomplish this, you will need to use the DecimalFormat class found in java.text. An example of using the DecimalFormat class would look like this:

    // Set up pattern for dollars and cents
    DecimalFormat formatter = new DecimalFormat("$#.00");
    // Following would print as $110.12
    System.out.println("\nFinal value: "
        + formatter.format(110.12345));
    

Make certain that your README.txt file lists any extra credit attempted.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the course Syllabus.

Program Compilation

  • 4: Source code compiles with no errors or warnings
  • 0: Does not compile

Functionality

  • 10: Demonstrates mastery of the assignment
    • Has extra features or demonstrates techniques beyond the assignment
    • Applies concepts from the lesson(s) appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • No errors encountered during operation
    • All test cases pass
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Meets all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
    • All test cases pass
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
    • All but one test case passes
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
    • At least 1/2 of all test cases pass
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
    • Less than 1/2 of all test cases pass
  • 0: Does not execute

Code Documentation

  • 4: Code is well-documented
  • 3: Code has minor documentation errors
    • Has 1 documentation error
  • 2: Code has some documentation errors
    • Has 2-3 documentation errors
  • 1: Code has many documentation errors
    • Has more than 3 documentation errors
  • 0: No apparent attempt to document code

REAME.txt File

  • 2: README.txt file submitted with specified information included
  • 1: README.txt submitted but some information was not included
  • 0: No README.txt submitted

Maximum Score: 20, plus extra credit

What to Turn In

Submit your assignment following the instructions for homework. Include the following items for grading:

  1. README.txt file
  2. InterestCalculator.java
  3. Any other source code needed to make your program function

You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.

Home | WebCT | Announcements | Schedule | Expectations | Syllabus
Help | FAQ's | HowTo's | Links

Last Updated: September 21 2003 @11:14:55