Using Java.... 1: Write a java program that use the factorial recursive method t
ID: 3665973 • Letter: U
Question
Using Java....
1: Write a java program that use the factorial recursive method to
A) Accept factorial number from keyboard.
B) Validate the input. Reject the invalid input until the input is accepted. (Reject input if it is character or string)
C) Use recursive call to calculate the factorial value.
D) Print the result
E) Accept the next number until terminated by user. (enter 0)
*** Write a class object to do the factorial calculation instead of method in the main program. ***
2: Do the similar program for power function by using two recursive methods. Compare two methods we addressed in the lecture. (display the CPU time for both methods)
3: The “odd/even factorial” of a positive integer is represented as n!! and is defined non-recursively as: (n)(n-2)(n-4)…(4)(2) if n is even and is (n)(n-2)(n-4)…(5)(3)(1) if n is odd. For example 7!! Equals 7*5*3*1 or 105 and 6!! Is 6*4*2 or 48. Write a method called oddevenfact that recursively calculates the odd/even factorial value of its single int parameter. The value returned by this method is a long. Validate the input.
*** Write a class object to do the Oddeven factorial calculation instead of method in the main program. ***
Explanation / Answer
1)
/**The java program that prompts user to enter a number.
* Print a eroor message if input invalid .
* Otherwise creates an instance of the Class
* Factorial and calls fact method and prints
* the factorial of user input number*/
//FactTester.java
import java.util.Scanner;
public class FactTester
{
public static void main(String[] args)
{
//declare variables
long num = 0;
boolean repeat=true;
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
System.out.println("Enter number or 0 to exit the program");
while(repeat)
{
try
{
//read input from user
num=scanner.nextLong();
//set repeat to false to stop reading input
//if input is valid
repeat=false;
}
catch (Exception e)
{
//otherwise contintue loop
scanner.nextLine();
System.out.println("Exception:Invalid input"
+ ".Enter valid input");
System.out.println("Enter number or 0 to exit the program");
}
}
//Create an instance of Factorial
Factorial factorial=new Factorial(num);
//print factorial of a number
System.out.println("Factorial of "+num+ " is "
+factorial.fact(num));
}
}
//Factorial.java
public class Factorial
{
private long n;
//Costructor takes num as input
public Factorial(long num)
{
this.n=num;
}
//Method fact that takes n as input and returns
//factorial of a number
public long fact(long n)
{
if(n==0 || n==1)
return 1;
else
//calls fact method recursively
return n*fact(n-1);
}
}
Sample output:
Enter number or 0 to exit the program
java
Exception:Invalid input.Enter valid input
Enter number or 0 to exit the program
A
Exception:Invalid input.Enter valid input
Enter number or 0 to exit the program
5
Factorial of 5 is 120
--------------------------------------------------------------------------------------------------------
2.
/**The java program that prompts base and power
* Print a eroor message if input invalid .
* Otherwise creates an instance of the Class
* Power and calls pow function and prints
* the power of base to the power.
*/
//FactTester.java
import java.util.Scanner;
public class PowTester
{
public static void main(String[] args)
{
//declare variables
int base = 0;
int power=0;
boolean repeat=true;
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
//Repeat the loop until user enter valid input for base
while(repeat)
{
try
{
System.out.println("Enter base");
//read input from user
base=scanner.nextInt();
repeat=false;
}
catch (Exception e)
{
//otherwise contintue loop
scanner.nextLine();
System.out.println("Exception:Invalid input");
}
}
repeat=true;
//Repeat the loop until user enter valid input for power
while(repeat)
{
try
{
System.out.println("Enter power");
//read input from user
power=scanner.nextInt();
repeat=false;
}
catch (Exception e)
{
//otherwise contintue loop
scanner.nextLine();
System.out.println("Exception:Invalid input");
}
}
//Create an instance of Power
Power powerObject=new Power(base,power);
//print factorial of a number
System.out.println(base+"^"+power+"= "+powerObject.pow(base,power));
}
}
sample output:
Enter base
A
Exception:Invalid input
Enter base
10
Enter power
A
Exception:Invalid input
Enter power
2
10^2= 100
------------------------------------------------------------------------------------------------------------------------
/**The java program CpuTimes that calls fact and pow
* functions of Factorial and Power class and prints
* the time taken by two functions in nano seconds*/
//CpuTimes.java
public class CpuTimes
{
public static void main(String[] args)
{
long start=System.nanoTime();
Factorial factorial=new Factorial();
long factValue=factorial.fact(10);
long end=System.nanoTime();
System.out.println("Factorial "+factValue);
System.out.println("Time taken(nano seconds) "+(end-start));
start=System.nanoTime();
Power powObject=new Power();
long powValue=powObject.pow(2, 10);
end=System.nanoTime();
System.out.println("Power "+powValue);
System.out.println("Time taken (nano seconds)"+(end-start));
}
}
Sample output:
Factorial 3628800
Time taken(nano seconds) 421523
Power 1024
Time taken (nano seconds)437214
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3)
/**The java program that prompts for the integer
* value and finds the even or odd.
* Then calls the corresponding method
* to find the factorial or even or odd
* and print the value to console*/
//odd/even factorial program
//EvenOddTester.java
import java.util.Scanner;
public class EvenOddTester
{
public static void main(String[] args)
{
//declare variables
long num = 0;
boolean repeat=true;
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
System.out.println("Enter number ");
while(repeat)
{
try
{
//read input from user
num=scanner.nextLong();
//set repeat to false to stop reading input
//if input is valid
repeat=false;
}
catch (Exception e)
{
//otherwise contintue loop
scanner.nextLine();
System.out.println("Exception:Invalid input"
+ ".Enter valid input");
System.out.println("Enter number ");
}
}
//calls method oddevenfact that returns the factorial
//of even factorial and odd factorial
//print factorial of a number
System.out.println("Factorial of "+num+ " is "
+oddevenfact(num));
}
//Method oddevenfact method that take num as input
//argumet and calls even method if num is even
//otherwise calls odd method
private static long oddevenfact(long num)
{
if(num%2==0)
return even(num);
else
return odd(num);
}
//Method to find factorial of odd number
private static long odd(long num)
{
if(num==1)
return 1;
else
return num*odd(num-2);
}
//Method to find factorial of even number
private static long even(long num)
{
if(num==0)
return 1;
else
return num*even(num-2);
}
}
sample output:
Enter number
7
Factorial of 7 is 105
Enter number
6
Factorial of 6 is 48
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.