Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that can perform any of several possible options on a collection

ID: 3880969 • Letter: W

Question

Write a program that can perform any of several possible options on a collection of user specified integers. The options are: (1) Enter a new integer, (2) Find the largest value, (3) Find the smallest value, (4) Find the value closest to a user specified entry, (5) Find the average, (6) Display the values, (7) Exit (i.e., the program). Display options one under the other. Allow the user to select any option as many times as desired. If option (4) is selected, you must also prompt for the number to be used in the comparison. Write a separate method for each option. Re-display the menu after each option completes.

Explanation / Answer

Hi there,
please put below code in OperateInteger.java file. Compile and run the program.
It will display options as mentioned in question.
press 1 to add a value in list. repeat it to add some more value.
after each operation it will ask to press any key to continue.
please press any numeric or alphabate key.


in OperateInteger.java put below code

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class OperateInteger {
ArrayList<Integer> list = new ArrayList<Integer>();/*list to hold numbers*/
Scanner scan=new Scanner(System.in);

//add numbers in list
public void addInterger(){
    System.out.println("Enter an integer");
    list.add(scan.nextInt());
    //System.out.println(list);
}

//find largest in list
public void larget(){
    if(list.size()==0)
    {
      System.out.println("please enter some intiger in list");
    }
    else{
    int largest=Collections.max(list);
    System.out.println("Largest value is:"+ largest);
    }
}


//find smallest in list
public void smallest()
{
    if(list.size()==0)
    {
      System.out.println("please enter some intiger in list");
    }
    else{
    int smallest=Collections.min(list);
    System.out.println("Smallest value is:"+ smallest);
    }
}

//calculate average of number list
public void average()
{
    if(list.size()==0)
    {
      System.out.println("please enter some intiger in list");
    }
    else{
    double sum=0;
    for(Integer i:list)
    {
      sum=sum+i;
    }
    System.out.println("Average is:"+sum/list.size());
    }
}

//Display all value of list
public void PrintList(){
    if(list.size()==0)
    {
      System.out.println("please enter some integer in list");
    }
    else{
    System.out.println("The List is:");
    for(Integer i:list)
    {
      System.out.print(i+" ");
    }
    }
}

public void findClosest( int num)
{ if(num>0)
    {
      int closest =0;
      int diff=1000000000;
      int m;
      for(Integer i:list){
        if(i-num>=0){
          m=i-num;}
        else{
          m=-(i-num);}
        if(diff> m){
          closest=i;
          diff=i%num;
        }
      }
      System.out.println(closest);
    }
    else{
      System.out.println("please enter value other than zero");
    }
}

public static void main(String[] args) {
    //interger list to hold numbers
   
    int x=1;
    Scanner scan=new Scanner(System.in);
    int option;
    OperateInteger oi=new OperateInteger();
    while(x==1){
    System.out.println("1:Enter an integer");
    System.out.println("2:find the largest value");
    System.out.println("3:find the smallest value");
    System.out.println("4:find the closest value");
    System.out.println("5:find the average");
    System.out.println("6:Display the values");
    System.out.println("7:Exit");
    System.out.print("please Chose an option:");
    option=scan.nextInt();
   
    //switch statement is used to call option function
    switch(option)
    {
    case 1:
      oi.addInterger();
      break;
    case 2:
      oi.larget();
      break;
    case 3:
      oi.smallest();
      break;
    case 4:
      System.out.println("Enter a number");
      int num=scan.nextInt();
      oi.findClosest(num);
      break;
    case 5:
      oi.average();
      break;
    case 6:
      oi.PrintList();
      break;
    case 7:
      x=7;
      break;
    default:
      System.out.println("invalid option");
    }
   
    System.out.println("press any key to continue..");
    scan.next();
    System.out.println();
    }
    scan.close();
    System.out.println("Exiting the execution.......");
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote