In: Computer Science
PLEASE READ AND DO NOT COPY AND PASTE THE ANSWER. PROGRAM NEEDS TO INCLUDE MYEXCEPTION.JAVA
File Factorials contains a program that calls the factorial method of theMathUtils class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for larger integers and that it always returns 1 for negative integers.
//
****************************************************************
// Factorials.java
//
// Reads integers from the user and prints the factorial of
each.
//
//
****************************************************************
import java.util.Scanner;
public class Factorials
{
public static void main(String[] args)
{
String keepGoing = "y";
Scanner scan = new
Scanner(System.in);
while (keepGoing.equals("y") ||
keepGoing.equals("Y"))
{
System.out.print("Enter an integer: ");
int val =
scan.nextInt();
System.out.println("Factorial(" + val + ") = "
+
MathUtils.factorial(val));
System.out.print("Another factorial? (y/n) ");
keepGoing =
scan.next();
}
}
}
//
****************************************************************
// MathUtils.java
//
// Provides static mathematical utility functions.
//
//
****************************************************************
public class MathUtils
{
//-------------------------------------------------------------
// Returns the factorial of the argument given
//-------------------------------------------------------------
public static int factorial(int n)
{
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
}
import java.util.Scanner;
// MathUtils.java
class MathUtils
{
// Return Factorial of User Input Value of n
public static int factorial(int n) throws myException
{
if(n<0) throw new myException("You have provided negative number ");
else if(n>16) throw new myException("Given input's factorial will cause the integer overflow");
else {
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
}
}
@SuppressWarnings("serial")
//myException Class
//Custom Exception class
class myException extends Exception{
myException(String s){
super(s);
}
}
// Factorial_Exception.java
// Reads integers from the user and prints the factorial of each.
public class Factorial_Exception
{
public static void main(String[] args)
{
String keepGoing = "y";
Scanner scan = new Scanner(System.in);
while (keepGoing.equals("y") || keepGoing.equals("Y"))
{
System.out.print("Enter an integer: ");
int val = scan.nextInt();
//placing try catch block here as it must not stop flow of the program
try {
System.out.println("Factorial(" + val + ") = "
+ MathUtils.factorial(val));
}
catch(myException s) {
System.out.println("Following exception occured: "+s.getMessage()+"\n");
}
System.out.print("Another factorial? (y/n) ");
keepGoing = scan.next();
}
}
}
Output Screenshot:
Please upvote if you find this to be helpful