Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Factorial Test Program

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Factorial Test Program

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Factorial Test Program

    What happens when you compile and execute the posted code?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    phendrickson (February 23rd, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Factorial Test Program

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

    After you fix your post so that your code is posted correctly (read the link above, then use the Edit Post button), please describe specifically what you need help with and/or ask specific questions. If you're getting errors, post those. If the output is not what is required, post a sample run and describe what needs to be changed.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    phendrickson (February 23rd, 2014)

  6. #4
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Factorial Test Program

    Study the try-catch block and the exceptions thrown by primitives e.g. int and arrays, so that you can tackle this assignment head on. You're pretty close to getting it, the try-catch block is all that remains.
    Who holds the KEY to all knowledge?

  7. The Following User Says Thank You to Mugambbo For This Useful Post:

    phendrickson (February 23rd, 2014)

  8. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Factorial Test Program

    Thank you for your help, I added the try-catch but the program is not working the way I need it to.

  9. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Factorial Test Program

    program is not working the way I need it to.
    Can you explain?
    What does the code do?
    What do you want it to do?
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    phendrickson (February 23rd, 2014)

  11. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Factorial Test Program

    The final output needs to look 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!

    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.
    Instead this is the output I get

    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)

    When a number between 0 and 12 is entered the factorial amount should be pulled from the main program and display in the output but it isn't, instead the is what my output looks like.

    Please enter a number between 0 and 12
    8
    Factorial of 8 is = Factorial@171bbc9

    When a negative number is entered the output should only display "Number should be non-negative" and allow the user to enter another number.
    Instead, this is the output I'm getting.

    Factorial of -9 is = Factorial@6f7ce9
    Number should be non-negative.

  12. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Factorial Test Program

    What do you want the program to do when the user enters bad data? Normally a program will print an error message and repeat the request to the user to enter the data correctly.
    When using the nextInt() method the code should have it inside of a try{}catch block that catches the exception that is thrown and prints an error message. That whole thing must be inside of a loop (while) that continues looping until the user enters good data.

    Factorial@171bbc9
    That is the String returned by the Object class's toString() method when referencing the Factorial class. What did you expect to be printed when the code prints f? f is a reference to a Factorial class instance.
    To get a value from the Factorial class, you need to call its method.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    phendrickson (February 23rd, 2014)

  14. #9
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Factorial Test Program

    @Phendrickson, if you had caught the exception (InputMismatchException) in a try catch block, it wouldn't have displayed this exception, but rather, any string of your choosing.
    Who holds the KEY to all knowledge?

  15. The Following User Says Thank You to Mugambbo For This Useful Post:

    phendrickson (February 23rd, 2014)

Similar Threads

  1. [SOLVED] Trouble with Writing test program
    By Onlyname in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 3rd, 2013, 01:41 PM
  2. Factorial of a Number (program completed but doubt in that)
    By arunjava in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 16th, 2013, 04:26 AM
  3. How to make a java program that gets average test scores?
    By johnsonparkar224 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 2nd, 2013, 06:46 AM
  4. How to make a java program that gets average test scores?
    By johnsonparkar224 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 2nd, 2013, 06:33 AM
  5. Memory test game program !
    By sharpedge in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 10th, 2013, 11:41 AM