Modify the MyMath class Add a method to calculate the factor
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 * method to return area of circle
20 *
21 * @param radius
22 * @return
23 */
24 public static double getCircleArea(double radius) {
25 DecimalFormat decimalFormat = new DecimalFormat(\"#.000\");
26 double area = Double
27 .parseDouble(decimalFormat.format(radius * Math.PI));
28 return area;
29 }
30
31 /**
32 * method to return max number
33 *
34 * @param num1
35 * @param num2
36 * @return
37 */
38 public static int getMax(int num1, int num2) {
39 if (num1 < num2)
40 return num2;
41 else
42 return num1;
43 }
44
45 }
8 import java.util.Scanner;
9
10 public class MyMathTest {
11
12 /**
13 * @param args
14 */
15 public static void main(String[] args) {
16
17 Scanner scanner = null;
18 try {
19 scanner = new Scanner(System.in);
20
21 do {
22 int choice;
23 System.out.println(\"***Menu***\");
24 System.out.println(\"1. Area\");
25 System.out.println(\"2. Max\");
26 System.out.println(\"0. Exit\");
27 System.out.print(\"Enter your choce:\");
28 choice = scanner.nextInt();
29 if (choice == 0)
30 break;
31 switch (choice) {
32 case 1: {
33 System.out.print(\"Enter the radius :\");
34 double radius = scanner.nextDouble();
35 System.out.println(\"Area of Circle:\"
36 + MyMath.getCircleArea(radius));
37 }
38 break;
39 case 2: {
40 System.out.print(\"Enter the First number:\");
41 int num1 = scanner.nextInt();
42 System.out.print(\"Enter the Second number:\");
43 int num2 = scanner.nextInt();
44 System.out.println(\"Max of Two numbers:\"
45 + MyMath.getMax(num1, num2));
46 }
47 break;
48
49 default:
50 System.out.println(\"Invalid Choice Selected!\");
51 break;
52 }
53 } while (true);
54
55 } catch (Exception e) {
56 // TODO: handle exception
57 }
58 }
59 }
Solution
import java.text.DecimalFormat;
public class MyMath {
/**
* method to return area of circle *
* @param radius
* @return
*/
public static int factorial(int n)
{
int fact=1;
for(int i=n;i>=1;i++)
{
fact=fact*i;
}
return fact;
}
public static double getCircleArea(double radius) {
DecimalFormat decimalFormat = new DecimalFormat(\"#.000\");
double area = Double
.parseDouble(decimalFormat.format(radius * Math.PI));
return area;
}
/**
* 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;
}
}
import java.util.Scanner;
public class MyMathTest {
/**
* @param args
*/
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.println(\"enter n :\");
int n=scanner.nextInt();
System.out.println(\"factorial : \"+MyMath.factorial(n));
break;
default:
System.out.println(\"Invalid Choice Selected!\");
break;
}
} while (true);
} catch (Exception e) {
// TODO: handle exception
}
}
}



