The Menu The class must have the following menu 1. Add a new number to the list
ID: 3622826 • Letter: T
Question
The MenuThe class must have the following menu
1. Add a new number to the list
2. Remove a number from the list
3. Find the average of the numbers
4. Find the smallest number in the list
5. Find the largest number in the list
6. QUIT
Executing the program
When the program executes, allow the user to select one of the options 1-6 Options
Option 1: Ask the user to enter a number and add it to the array list.
Option 2: Allow the user to enter the number he/she wants to remove. If the
number is in the list, remove it and print the message: Element successfully removed.
If the number is not in the list, print the message: Number is not in thelist (Use the API and read about the remove method)
Option 3: Find and print the sum of the numbers in the list. Be sure you take care of the case that the list is empty!
Options 4 and 5 are self explanatory. Be sure that you take care of the case that the list is empty!
Explanation / Answer
import java.io.*; import java.util.*; public class Myclass { static ArrayList<Integer> lst = new ArrayList<Integer>(); public static void main(String args[]) { Boolean quit = false; Scanner reader = new Scanner(System.in); while(!quit) { String input = reader.nextLine(); String parsed[]; parsed = input.split(" "); String cmd = parsed[0]; if (cmd.equals("add") ) { add( Integer.parseInt(parsed[1]) ); } else if( cmd.equals("remove")) { remove(Integer.parseInt(parsed[1])); } else if (cmd.equals("sum")) { sum(); } else if(cmd.equals("largest")) { largest(); } else if(cmd.equals("smallest")) { smallest(); } else if(cmd.equals("quit")) { quit = true; } else { System.out.println("Input not understood"); } } } static void largest() { int big = lst.indexOf(0); if(lst.isEmpty()) { System.out.println("List is empty"); } else { for(int i = 0; i < lst.size(); i++ ) { if (lst.indexOf(i) > big ) { big = lst.indexOf(i); } } } System.out.printf("Largest value is %d", big); System.out.println(""); } static void smallest() { int small = lst.indexOf(0); if(lst.isEmpty()) { System.out.println("List is empty"); } else { for(int i = 0; i < lst.size(); i++ ) { if (lst.indexOf(i) < small ) { small = lst.indexOf(i); } } } System.out.printf("Smalles value is %d", small); System.out.println(""); } static void sum() { int sum = 0; if(lst.isEmpty()) { System.out.println("List is empty"); } else { for(int i = 0; i < lst.size(); i++ ) { sum = sum + lst.indexOf(i); } } System.out.printf("The sum is %d", sum); System.out.println(""); } static void remove(int num) { if(lst.isEmpty()) { System.out.println("List is empty"); } else if (lst.contains(num)){ lst.remove(num); } else { System.out.printf("%d is not in the list", num); System.out.println(""); } } static void add(int num) { lst.add(num); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.