You need to write a menu driven program. The program allows a user to enter five
ID: 3765345 • Letter: Y
Question
You need to write a menu driven program.
The program allows a user to enter five numbers and then asks the user to select a choice from a menu. The menu should offer following four options –
Display the smallest number entered
Display the largest number entered
Display the sum of the five numbers entered
Display the average of the five numbers entered
You must use a "switch" statement in your code to determine what action to take. Provide an error message if an invalid choice is entered.
Run the program five times, once with each option and once with an invalid option. Each run is to use the following set of data:
18, 21, 17, 44, 9.
Explanation / Answer
Solution:
package com.urbanpro.java;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Numbers {
static ArrayList<Integer> numList = new ArrayList<Integer>(5);
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter five numbers(Separated with spaces) : ");
for (int i = 0; i < 5; i++) {
numList.add(scan.nextInt());
}
System.out.println("Menu");
System.out.println("Press 1 : Display the smallest number entered");
System.out.println("Press 2 : Display the largest number entered");
System.out.println("Press 3 : Display the sum of the five numbers entered");
System.out.println("Press 4 : Display the average of the five numbers entered");
System.out.println("Please enter your choice.(Press any other number to EXIT.)");
int input = scan.nextInt();
scan.close();
switch (input) {
case 1:
smallestNum();
break;
case 2:
largestNum();
break;
case 3:
sum();
break;
case 4:
avg();
break;
default:
System.out.println("ERROR: Your have entered invalid input.");
System.exit(0);
}
}
private static void avg() {
float sum = sum();
int size = numList.size();
float average = sum / size;
System.out.println("The average of all number = " + average);
}
private static float sum() {
float sum = 0;
for (Integer ele : numList)
sum += ele;
System.out.println("The Sum of all number = " + sum);
return sum;
}
private static void largestNum() {
int largest = numList.indexOf(Collections.max(numList));
System.out.println("The largest number = " + numList.get(largest));
}
private static void smallestNum() {
int smallest = numList.indexOf(Collections.min(numList));
System.out.println("The smallest number = " + numList.get(smallest));
}
}
Output:
Please Enter five numbers(Separated with spaces) :
18 21 17 44 9
Menu
Press 1 : Display the smallest number entered
Press 2 : Display the largest number entered
Press 3 : Display the sum of the five numbers entered
Press 4 : Display the average of the five numbers entered
Please enter your choice.(Press any other number to EXIT.)
1
The smallest number = 9
Please Enter five numbers(Separated with spaces) :
18 21 17 44 9
Menu
Press 1 : Display the smallest number entered
Press 2 : Display the largest number entered
Press 3 : Display the sum of the five numbers entered
Press 4 : Display the average of the five numbers entered
Please enter your choice.(Press any other number to EXIT.)
2
The largest number = 44
Please Enter five numbers(Separated with spaces) :
18 21 17 44 9
Menu
Press 1 : Display the smallest number entered
Press 2 : Display the largest number entered
Press 3 : Display the sum of the five numbers entered
Press 4 : Display the average of the five numbers entered
Please enter your choice.(Press any other number to EXIT.)
3
The Sum of all number = 109.0
Please Enter five numbers(Separated with spaces) :
18 21 17 44 9
Menu
Press 1 : Display the smallest number entered
Press 2 : Display the largest number entered
Press 3 : Display the sum of the five numbers entered
Press 4 : Display the average of the five numbers entered
Please enter your choice.(Press any other number to EXIT.)
4
The Sum of all number = 109.0
The average of all number = 21.8
Please Enter five numbers(Separated with spaces) :
18 21 17 44 9
Menu
Press 1 : Display the smallest number entered
Press 2 : Display the largest number entered
Press 3 : Display the sum of the five numbers entered
Press 4 : Display the average of the five numbers entered
Please enter your choice.(Press any other number to EXIT.)
5
ERROR: Your have entered invalid input.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.