Modify the MyMath class: Add a method to calculate the factorial of a number. A
ID: 3775804 • Letter: M
Question
Modify the MyMath class:
Add a method to calculate the factorial of a number. A factorial is calculation that multiplies together all of the numbers between 1 and the specified number. For instance, 5! (the ! symbol is used to denote factorial) is 1 * 2 * 3 * 4 * 5 = 120. 4! = 1 * 2 * 3 * 4 = 24.
Use a for loop to calcuate the factorial.
Make the method static.
Do not ask for any input in the MyMath class. Do not create a Scanner object. The factorial method should take one parameter and return a value.
Modify the Test class:
Do not create a MyMath object.
Gather all user input in the Test class. Do not ask for any input in the MyMath class.
Modify the menu for the user:
Add an option to find the factorial of a number.
Add an option to quit.
Use a while or do-while loop to repeat the program until the user chooses to quit.
*** This is what I have so far
14 import java.text.DecimalFormat;
15
16 public class MyMath {
17
18
19 public static double getCircleArea(double radius) {
20 DecimalFormat decimalFormat = new DecimalFormat("#.000");
21 double area = Double
22 .parseDouble(decimalFormat.format(radius * Math.PI));
23 return area;
24 }
25 public static int factorial(int n){
26 int fact = 1;
27 for(int i=1; i<=n; i++){
28 fact = fact * i;
29 }
30 return fact;
31 }
32
33 /**
34 * method to return max number
35 *
36 * @param num1
37 * @param num2
38 * @return
39 */
40 public static int getMax(int num1, int num2) {
41 if (num1 < num2)
42 return num2;
43 else
44 return num1;
45 }
46
47 }
8 import java.util.Scanner;
9
10 public class MyMathTest {
11
12
13 public static void main(String[] args) {
14
15 Scanner scanner = null;
16 try {
17 scanner = new Scanner(System.in);
18
19 do {
20 int choice;
21 System.out.println("***Menu***");
22 System.out.println("1. Area");
23 System.out.println("2. Max");
24 System.out.println("3. Factorial");
25 System.out.println("0. Exit");
26 System.out.print("Enter your choce:");
27 choice = scanner.nextInt();
28 if (choice == 0)
29 break;
30 switch (choice) {
31 case 1: {
32 System.out.print("Enter the radius :");
33 double radius = scanner.nextDouble();
34 System.out.println("Area of Circle:"
35 + MyMath.getCircleArea(radius));
36 }
37 break;
38 case 2: {
39 System.out.print("Enter the First number:");
40 int num1 = scanner.nextInt();
41 System.out.print("Enter the Second number:");
42 int num2 = scanner.nextInt();
43 System.out.println("Max of Two numbers:"
44 + MyMath.getMax(num1, num2));
45 }
46 break;
47 case 3:
48 System.out.print("Enter the factorial number:");
49 int num1 = scanner.nextInt();
50 System.out.println("Fqactorial of "+num1+"! is :"
51 + MyMath.factorial(num1));
52 break;
53 default:
54 System.out.println("Invalid Choice Selected!");
55 break;
56 }
57 } while (true);
58
59 } catch (Exception e) {
60 // TODO: handle exception
61 }
62 }
63 }
Explanation / Answer
Hi friend, your program is working fine. All of your requirement is already available in posted program.
Can you please tell me what is the modification you want ?
########### MyMath.java ###########
import java.text.DecimalFormat;
public class MyMath {
public static double getCircleArea(double radius) {
DecimalFormat decimalFormat = new DecimalFormat("#.000");
double area = Double
.parseDouble(decimalFormat.format(radius*radius * Math.PI));
return area;
}
public static int factorial(int n){
int fact = 1;
for(int i=1; i<=n; i++){
fact = fact * i;
}
return fact;
}
/**
* method to return max number
*
* @param num1
* @param num2
* @return
*/
public static int getMax(int num1, int num2) {
if (num1 < num2)
return num2;
else
return num1;
}
}
############ MyMathTest.java ###############
import java.util.Scanner;
public class MyMathTest {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
do {
int choice;
System.out.println("***Menu***");
System.out.println("1. Area");
System.out.println("2. Max");
System.out.println("3. Factorial");
System.out.println("0. Exit");
System.out.print("Enter your choce:");
choice = scanner.nextInt();
if (choice == 0)
break;
switch (choice) {
case 1: {
System.out.print("Enter the radius :");
double radius = scanner.nextDouble();
System.out.println("Area of Circle:"
+ MyMath.getCircleArea(radius));
}
break;
case 2: {
System.out.print("Enter the First number:");
int num1 = scanner.nextInt();
System.out.print("Enter the Second number:");
int num2 = scanner.nextInt();
System.out.println("Max of Two numbers:"
+ MyMath.getMax(num1, num2));
}
break;
case 3:
System.out.print("Enter the factorial number:");
int num1 = scanner.nextInt();
System.out.println("Fqactorial of "+num1+"! is :"
+ MyMath.factorial(num1));
break;
default:
System.out.println("Invalid Choice Selected!");
break;
}
} while (true);
} catch (Exception e) {
// TODO: handle exception
}
}
}
/*
Sample run:
***Menu***
1. Area
2. Max
3. Factorial
0. Exit
Enter your choce:1
Enter the radius :4
Area of Circle:12.566
***Menu***
1. Area
2. Max
3. Factorial
0. Exit
Enter your choce:3
Enter the factorial number:6
Fqactorial of 6! is :720
***Menu***
1. Area
2. Max
3. Factorial
0. Exit
Enter your choce:0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.