This programming assignment is to modify a working program in order to correct some problems within it. Sometimes the fastest way to become familiar with a new language is to see it in use. To that end, you’ll evaluate a fairly simple program that incorporates some of the language constructs that are universal across programming languages (and that we’ve discussed in class): boolean expressions, branching (logical processing), looping (iterative processing), etc. These concepts should be familiar to you; however, for most of you, it will be the first time you work with them in java. The program will also gently introduce you to a couple of constructs, that perhaps you haven’t seen before: exception handling (which we’ll study later in the semester) and passing runtime arguments into a program from the command line.

The program is named Factorial (Factorial.java is downloadable from the assignment in Blackboard), and it calculates the factorial of integer values passed into it as follows (as always, the dollar sign represents the OS prompt, and to the right of it is the command you would enter; the system’s output follows):

$ java Factorial 4
4! = 24

In the example above, the program was invoked through the JRE, and passed in an argument of 4. The program responded by essentially saying “the factorial of 4 is 24.” We know this is true because:

4! = 4 x 3 x 2 x 1 = 24

A write up on factorial of a number is available here if needed:


In order to demonstrate looping in multiple ways, the program is also equipped to handle multiple input values (0 to n). For example:

$ java Factorial 3 4 5
3! = 6
4! = 24
5! = 120

The program is deliberately devoid of any sort of documentation or comments. Normally, this is a bad practice, but in this case you won’t be distracted by anything but code (code can be overwhelmed by verbose commentary). Comments about the program and its various pieces are included later in this document.

There are two problems deliberately incorporated into the program:

1) Mathematical factorial is only valid for non-­-negative integers (0 … 8). Due to a flaw in the program’s logic, it incorrectly calculates the factorial of negative integers as 1 (see the Wikipedia link above):

$ java Factorial 0 -1 -2
0 != 1
-1! = 1
-2! = 1

Note that 0! = 1 is correct; however, -­-1! is undefined, not 1, as is the factorial of any negative number.

Note also that the program already has some basic input validation which can be used as an example of one approach. When a non-­-integer value is encountered, the program issues a warning:

$ java Factorial one 2.2 3/3
Warning: one is not an integer!
Warning: 2.2 is not an integer!
Warning: 3/3 is not an integer!

2) Due to a poor selection of primitive data types, the program incorrectly calculates the factorial of larger numbers. For example, the program incorrectly calculates 20! to be -­-2102132736, when in fact it is 2432902008176640000. Clearly, the product of 20 positive factors should be positive!


Correct the program logic so that it behaves as follows when negative integers are encountered:

$ java Factorial -1 -2 4
Warning: -1! is undefined! 4! = 24
Warning: -2! is undefined!

Correct the program so that it can properly handle reasonably large numbers (at least up to 20!).

Factorial.java will run without any arguments; however, the user gets no feedback (because there’s nothing to compute). It’s customary to provide feedback on how to invoke a program when it’s done so incorrectly. For this option you will need to modify the program so that if the user invokes it with no arguments (i.e. no numbers to process) they will get the following feedback:

Factorial usage: java Factorial [ []]

Change the while loop in the factorial() method to a for loop.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.