In: Computer Science
Throwing Exceptions - 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.
Submit the following:
Modified MathUtils.java
Modified Factorials.java
myException.java
//
****************************************************************
// 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;
}
}
//
****************************************************************
// 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();
}
}
}
class
GFG {
// This function
finds factorial of
// large numbers and
prints them
static
void
factorial(
int
n)
{
int
res[] =
new
int
[
500
];
//
Initialize result
res[
0
]
=
1
;
int
res_size =
1
;
//
Apply simple factorial formula
//
n! = 1 * 2 * 3 * 4...*n
for
(
int
x =
2
; x <= n; x++)
res_size
= multiply(x, res, res_size);
System.out.println(
"Factorial
of given number is "
);
for
(
int
i = res_size -
1
; i >=
0
;
i--)
System.out.print(res[i]);
}
// This function
multiplies x with the number
// represented by
res[]. res_size is size of res[] or
// number of digits
in the number represented by res[].
// This function uses
simple school mathematics for
// multiplication.
This function may value of res_size
// and returns the
new value of res_size
static
int
multiply(
int
x,
int
res[],
int
res_size)
{
int
carry =
0
;
//
Initialize carry
//
One by one multiply n with individual
//
digits of res[]
for
(
int
i =
0
; i < res_size; i++)
{
int
prod = res[i] * x + carry;
res[i]
= prod %
10
;
// Store last
digit of
//
'prod' in res[]
carry
= prod/
10
;
// Put rest in
carry
}
//
Put carry in res and increase result size
while
(carry!=
0
)
{
res[res_size]
= carry %
10
;
carry
= carry /
10
;
res_size++;
}
return
res_size;
}
// Driver
program
public
static
void
main(String
args[])
{
factorial(
100
);
}
}
import java.util.Scanner;
import javax.swing.JOptionPane;
public class App {
public static void main(String[] args) {
int value;
//E.g. 4!=4*3*2*1
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter a value for factorial");
value=keyboard.nextInt();
try{
System.out.println(calculate(value));}catch(NumberFormatException e){
System.out.println("invalid error. No negative numbers");
}
}
private static int calculate(int value){
if(value==1 || value==0){
return 1;
}
return calculate(value-1)*value;
}
}
-----------------------------------------------------------------------------------------
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();
}
}
}
public class MathUtils
{
public static int factorial(int n)
{
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
}