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

File Factorials.java contains a program that calls the factorial method of the M

ID: 3546354 • Letter: F

Question

File Factorials.java contains a program that calls the factorial method of the

MathUtils 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.

1. Returning 1 as the factorial of any negative integer is not correct

Explanation / Answer

package hmwk4;

import static java.lang.Math.pow;

import java.util.Scanner;



public class Hmwk4 {

public static double CalculatePi(int a){

Scanner scan = new Scanner(System.in);

System.out.println("Please enter a value for 'a'");

a = scan.nextInt();

double count = 0, denom = 1.0, pi = 0, difference;

  

for (int i = 0; i <= a; i++){

pi += Math.pow(-1.0, count) * (4 / denom);

count ++;

denom += 2;

difference = Math.PI - pi;

System.out.println("The value of pi is " + pi);

  

System.out.println("The difference is " +difference);

System.out.println();

}

return a;

}

public static int Fact(int n){

   int factorial;


   if(n==1)

   return 1;


   factorial = Fact(n-1) * n;

   return factorial;

   }

public static double CalculateE(int a){

Scanner scan = new Scanner(System.in);


System.out.println("Please enter a value for 'a'");

a = scan.nextInt();

// a = 100;

double result=1;

double denominator = 1;

for (int i = 1; i <= a; i++) {

denominator *=i;

result +=(1/denominator);


   }System.out.println("e = " +result);

return result;

}

public static void main(String[] args) {

int a = 0;

Scanner scan = new Scanner(System.in);


Hmwk4 obj1 = new Hmwk4();

while(true){

redo_the_loop:

System.out.println("Please a number between 1-3");

System.out.println("1: Calculate Pi");

System.out.println("2: Calculate 'e'");

System.out.println("3: Quit");

int choice = scan.nextInt();

  

if (choice < 1 || choice > 3)

System.out.println("Duhhh! Please choose a number from the above ment to begin");

   switch(choice){

   case 1: CalculatePi(a);break;

   case 2: CalculateE(a);break;

   case 3: System.out.println("Adios!"); break;

   }

}

}

  

  }

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