This is a homework assignment that have been unable to complete. I've been working on it for 6 days with no success. Everything I try causes the code to fail.
The code I'm working on is a test program to run against this code.
// method factorial // computes the factorial for 01 to 12! using a table lookup // factorials larger than 12! exceed the largest value that can be stored // in a Java primitive integer public class Factorial { public static int factorial(int n) { final int f[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600}; return f[n]; } }
The test program needs to catch any exceptions that occur so the program doesn't terminate. The number range is factorials for 0 through 12 only. If the user enters a negative number, a number higher than 12, blank, special characters, or alphabet the exception should be handled.
import java.util.InputMismatchException; import java.util.Scanner; public class FactorialTest { public static void main (String args[]) { Factorial f = new Factorial(); // create scanner object to read user input Scanner scanner = new Scanner(System.in); // prompt user to enter a number between 0 and 12 System.out.println("Please enter a number between 0 and 12"); // store value entered by user int n = scanner.nextInt(); // assert the value of n is between 0 and 12 assert (n >= 0 && n <= 12); System.out.println("Factorial of "+n+" is = "+f+""); try { } catch (InputMismatchException inputMismatchException) { System.err.printf("/nException: %s\n", inputMismatchException); scanner.nextLine(); // discard input so user can try again System.out.println("You must enter a number between 0 and 12"); } if (n < 0) System.out.println("Number should be non-negative."); else { for (n = 0; n <= 12; n++); } } }
I checked with my college campus but they do not have any tutors to help with Java programming. I also contacted my instructor but he is not available on weekends. This assignment is due on 2/23/2014.
The final output needs to look something like this:
Enter an integer number: abc
You must enter an integer - please re-enter:
Enter an integer number: -5
Factorial of this value cannot be represented as an integer.
Please re-enter your integer:
Enter an integer number: 13
Factorial of this value cannot be represented as an integer.
Please re-enter your integer:
Enter an integer number: 9
The factorial of 9 is 362880.
Done!
Output when a letter is entered
Please enter a number between 0 and 12
a
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at FactorialTest.main(FactorialTest.java:25)
Output when a number between 0 and 12 is entered
Please enter a number between 0 and 12
8
Factorial of 8 is = Factorial@171bbc9
Output when a negative number is entered
Factorial of -9 is = Factorial@6f7ce9
Number should be non-negative.
The program is not working the way I need it to. When a number between 0 and 12 is entered the factorial amount should be pulled from main program and display in the output but it isn't
When a negative number is entered the output should only display "Number should be non-negative" and allow the user to enter another number.
When a letter is entered the output should display " Please enter a number between 0 and 12" and allow the user to enter another number.
Any assistance you can provide on what I'm doing wrong is greatly appreciated.
phendrickson