Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am looking for someone to help me START this program. I am not looking

ID: 3631372 • Letter: H

Question

Hello, I am looking for someone to help me START this program. I am not looking for a complete answer, as I would like to try and complete it myself, however a start would help me immensely! I am so stuck with this that I don't even know where to begin.

Thanks!

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.


Hint:
You will have to have at least one loop to continue
until "quit" is entered.

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.

Ex.
if 4 is inputted than 4! = 4 * 3 * 2 * 1 with a result
of 24

Hint:
You might want to think about a loop to complete this.


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.

Ex.
if 3 is read in and BASE is equal to 2, then 2 ^ 3 = 2 * 2 * 2 = 8

Note:
You must use a loop to calculate this result.

Note:
Do NOT use Math.pow anywhere in this program!
It is not necessary.
Any program using Math.pow will suffer
a large deduction of credit.


If a user enters a word besides "factorial", "power", or "quit" display an error message and
terminate the program.

Note:
The calculations of both methods, "power" and "factorial" can be completed with a loop.
You do not need any recursion to complete these methods, and we recommend AGAINST using
recursion in this course unless you are an advanced student.


To help find problems while you are programming you are required to use the
debug switch statement, as covered in lab.


Note:
In mathematis 0! (zero factorial) is defined, a positive BASE raised to a zero power
is defined, and a negative power is defined. If we allow a type double result, but
for simplicity of the programming assignment we are not handling those values.


Note:
When you handin your program make sure DEBUG = True.

Sample data file #1:
--------------------
factorial
3
power
4
quit


Output for sample data file #1 with DEBUG = true:
-------------------------------------------------
DEBUG
Word read in is: factorial

DEBUG
Finding the factorial of 3

DEBUG
Finding the factorial of 3
Currently working on 3
With an intermediate result of 3

DEBUG
Finding the factorial of 3
Currently working on 2
With an intermediate result of 6

The factorial of 3 is 6.

DEBUG
Word read in is: power

DEBUG
Finding 2 raised to the power of 4

DEBUG
Finding 2 raised to the power of 4
Currently working 2 raised to the power of 1
With an intermediate result of 2

DEBUG
Finding 2 raised to the power of 4
Currently working 2 raised to the power of 2
With an intermediate result of 4

DEBUG
Finding 2 raised to the power of 4
Currently working 2 raised to the power of 3
With an intermediate result of 8

DEBUG
Finding 2 raised to the power of 4
Currently working 2 raised to the power of 4
With an intermediate result of 16

2 raised to the power of 4 power is 16.

DEBUG
Word read in is: quit


Output for sample data file #1 with DEBUG = false:
--------------------------------------------------
The factorial of 3 is 6.

2 raised to the power of 4 is 16.




public class MathFunctions
{
public static void main(String[] args)
{
// Declare named constants
final int BASE = 2;

// Declare all need variables.

// Use defensive programming for the value read in.

// Loop until sentinel is found.

// More defensive programming

// Read in the value.

// Figure out which math function the user wants.

// Call appropriate method.

// Print appropriate outputs.

// End of Loop

} // End of main method

public static int power(int iVal, int BASE, boolean DEBUG)
{
// Defensive programming

// Calculate result

// Return result

} // End of power method

public static int factorial(int iVal, boolean DEBUG)
{
// Defensive programming

// Calculate results

// Return result

} // End of factorial method
} //End of class MathFunctions

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote