A math teacher has requested we create a program that will, based on user input,
ID: 3630970 • Letter: A
Question
A math teacher has requested we create a program that will, based on user input,either find the factorial of a number or find BASE raised to the power of the
number read in. BASE is a named constant, equal to 2 for this assignment, which
could be changed later on if desired.
The program will continue to run until the user enters "quit" to quit the
program run.
Note:
The commands "factorial", "power", and "quit" must be named constants in the
program. A suggestion for the constant variables names is COMMAND_1, COMMAND_2,
and COMMAND_3. You cannot not use FACTORIAL, POWER, or QUIT as the constant
variable names.
If a user enters "factorial" you need to call the method factorial which has the
following header:
public static int factorial(int iVal, boolean DEBUG)
In the method factorial you will need to verify that the integer is greater than
0. If this condition is met, then find and return the factorial of the number
read in. If not, print an error message and terminate the program.
If a user enters "power" you need to call the method power which has the
following header:
public static int power(int iVal, int BASE, boolean DEBUG)
In the method power you will need to verify that the integer is greater than 0.
If this condition is met then compute and return BASE raised to the value iVal.
If not, print an error message and terminate the program.
If a user enters a word besides "factorial", "power", or "quit" display an error message and
terminate the program.
Explanation / Answer
import java.io.*;
import java.util.*;
{
public static void main(String[] args)
{
Scanner input;
// Declare named constants
final int BASE = 2;
try
{
// Declare all need variables.
File file=new File("data.txt");
String command1="factorial";
String command2="power";
String command3="quit";
input=new Scanner(file);
while(input.hasNext())
{
String command=input.nextLine();
System.out.println("Word read in is:"+command);
if(command.equals(command3))
break;
int val=input.nextInt();
if(command.equals(command1))
{
System.out.println("The factorial of "+val+" is "+ factorial(val,true)+".");
}
else if(command.equals(command2))
System.out.println("2 raised to the power of"+ val+" power is "+power(val,BASE,true));
}
}
catch(FileNotFoundException e)
{
input=null;
}
}
public static int power(int iVal, int BASE, boolean DEBUG)
{
int pow=BASE;
if(DEBUG)
{
for(int i=1;i<=iVal;i++)
{
System.out.println("DEBUG");
System.out.println(" Finding 2 raised to the power of "+iVal);
System.out.println("Currently working 2 raised to the power of "+iVal);
System.out.println("With an intermediate result of "+ i);
pow=pow*BASE;
}
return pow;
}
else
return 0;
} // End of power method
public static int factorial(int iVal, boolean DEBUG)
{
int fact=1;
if(iVal==1)
// Defensive programming
for(int i=iVal;i>0;i--)
{
System.out.println("DEBUG");
System.out.println(" Finding the factorial of "+iVal);
System.out.println("Currently working on "+ iVal);
System.out.println("With an intermediate result of "+ i);
fact=fact*i;
}
// Calculate results
return fact;
// Return result
} // End of factorial method
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.