Modify the MyMath classAdd a method to find the average of a
Modify the MyMath class:Add a method to find the average of a list of integers. You will be asking for input in this method so use the Scanner class. You will not be taking any input as parameters and you will not be returning a value:
Make the method a static method so it can be used without creating an object.
Use an array to store the input list.
Prompt the user for the number of integers. Use this input to set the size of the array.
Prompt the user for the numbers to find the average of. Store these numbers in the array (use a for loop to gather the input). Ask for the numbers one at a time (ask for a number, wait for user input, ask for the next number, wait for user input, etc.)
Use a for statement to calculate the average. Note that the average will be a floating point data type so you will need to do a conversion. For instance, the average of 1, 2, 3, 4, and 6 is 3.2, not 3.0.
Display the result with an output message. Include the numbers being averaged in the output message. Make sure the output message clearly states what the user is looking at.
Modify the Test class:
Do not create a MyMath object.
Modify the menu for the user:
Add an option to find the average of a list of integers. Do not make it the last option. The last option should be the option to quit.
*** This is what I have so far
14 import java.text.DecimalFormat;
15
16 public class MyMath {
17
18 public static double getCircleArea(double radius) {
19
20 DecimalFormat decimalFormat = new DecimalFormat(\"#.000\");
21
22 double area = Double
23
24 .parseDouble(decimalFormat.format(radius*radius * Math.PI));
25
26 return area;
27
28 }
29
30 public static int factorial(int n){
31
32 int fact = 1;
33
34 for(int i=1; i<=n; i++){
35
36 fact = fact * i;
37
38 }
39
40 return fact;
41
42 }
43
44 /**
45
46 * method to return max number
47
48 *
49
50 * @param num1
51
52 * @param num2
53
54 * @return
55
56 */
57
58 public static int getMax(int num1, int num2) {
59
60 if (num1 < num2)
61
62 return num2;
63
64 else
65
66 return num1;
67
68 }
69
70 }
8 import java.util.Scanner;
9
10 public class MyMathTest {
11
12 public static void main(String[] args) {
13
14 Scanner scanner = null;
15
16 try {
17
18 scanner = new Scanner(System.in);
19
20 do {
21
22 int choice;
23
24 System.out.println(\"***Menu***\");
25
26 System.out.println(\"1. Area\");
27
28 System.out.println(\"2. Max\");
29
30 System.out.println(\"3. Factorial\");
31
32 System.out.println(\"0. Exit\");
33
34 System.out.print(\"Enter your choice:\");
35
36 choice = scanner.nextInt();
37
38 if (choice == 0)
39
40 break;
41
42 switch (choice) {
43
44 case 1: {
45
46 System.out.print(\"Enter the radius :\");
47
48 double radius = scanner.nextDouble();
49
50 System.out.println(\"Area of Circle:\"
51
52 + MyMath.getCircleArea(radius));
53
54 }
55
56 break;
57
58 case 2: {
59
60 System.out.print(\"Enter the First number:\");
61
62 int num1 = scanner.nextInt();
63
64 System.out.print(\"Enter the Second number:\");
65
66 int num2 = scanner.nextInt();
67
68 System.out.println(\"Max of Two numbers:\"
69
70 + MyMath.getMax(num1, num2));
71
72 }
73
74 break;
75
76 case 3:
77
78 System.out.print(\"Enter the factorial number:\");
79
80 int num1 = scanner.nextInt();
81
82 System.out.println(\"Factorial of \"+num1+\"! is :\"
83
84 + MyMath.factorial(num1));
85
86 break;
87
88 default:
89
90 System.out.println(\"Invalid Choice Selected!\");
91
92 break;
93
94 }
95
96 } while (true);
97
98 } catch (Exception e) {
99
100 // TODO: handle exception
101
102 }
103
104 }
105
106 }
107
Solution
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(\"4. Average\");
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;
case 4:
MyMath.average();
break;
default:
System.out.println(\"Invalid Choice Selected!\");
break;
}
} while (true);
} catch (Exception e) {
// TODO: handle exception
}
}
}
MyMath.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class MyMath {
public static double getCircleArea(double radius) {
DecimalFormat decimalFormat = new DecimalFormat(\"#.000\");
double area = Double
.parseDouble(decimalFormat.format(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;
}
public static void average(){
Scanner scan = new Scanner(System.in);
System.out.print(\"Enter the number of integers: \");
int n = scan.nextInt();
int a[]= new int[n];
for(int i=0; i<n; i++){
System.out.print(\"Enter an integer: \");
a[i] = scan.nextInt();
}
int sum = 0;
for(int i=0; i<n; i++){
sum = sum + a[i];
}
double avg = sum/(double)n;
System.out.println(\"Average is \"+avg);
}
/**
* 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;
}
}
Output:
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:1
Enter the radius :2
Area of Circle:6.283
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:3
Enter the factorial number:2
Fqactorial of 2! is :2
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:3
Enter the factorial number:5
Fqactorial of 5! is :120
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:2
Enter the First number:4
Enter the Second number:3
Max of Two numbers:4
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:4
Enter the number of integers: 5
Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 5
Average is 3.0
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:4
Enter the number of integers: 4
Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Average is 2.5
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:4
Enter the number of integers: 5
Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 6
Average is 3.2
***Menu***
1. Area
2. Max
3. Factorial
4. Average
0. Exit
Enter your choce:0






