Kulas_Code20

Menu

Sep 17th, 2021 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package activities;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Menu {
  6.  
  7.     static Scanner sc = new Scanner(System.in);
  8.  
  9.     // Populate Array Elements
  10.     public static  void populate(int[] arr) {
  11.         System.out.println("POPULATE ARRAY ELEMENT:");
  12.         System.out.println("Please enter t number: ");
  13.         for(int i=0; i<5; i++) {
  14.             arr[i] = sc.nextInt();
  15.         }
  16.     }
  17.  
  18.     public static  void printing(int[] arr, int size) {
  19.         System.out.println("PRINTING ARRAY ELEMENT:");
  20.         for (int i = 0; i < size; i++) {
  21.             System.out.print(" " + arr[i]);
  22.         }
  23.     }
  24.  
  25.     public static void insert(int[] arr, int size) {
  26.         System.out.println("INSERT NEW ITEM TO THE ARRAY ELEMENTS:");
  27.         System.out.println("Please enter the item to be inserted:");
  28.         int newItem = sc.nextInt();
  29.         System.out.println("Please enter the location to be inserted:");
  30.         int loc = sc.nextInt();
  31.        
  32.         for (int x = size-1; x >=(newItem-1); x--) {
  33.              if(loc == x) {
  34.                  arr[x+1] = arr[x];
  35.              }
  36.         }
  37.         arr[newItem-1] = loc;
  38.         for(int i=0; i<size; i++) {
  39.             System.out.print(" " + arr[i]);
  40.         }
  41.     }
  42.  
  43.     public static void delete(int[] arr) {
  44.         System.out.println("DELETE AN ITEM:");
  45.         System.out.println("Please enter the item to be deleted:\n");
  46.         int delItem = sc.nextInt();
  47.          for(int i = 0; i < arr.length; i++){
  48.             if(arr[i] == delItem){
  49.                 // shifting elements
  50.                 for(int j = i; j < arr.length - 1; j++){
  51.                     arr[j] = arr[j+1];
  52.                 }
  53.                 break;
  54.             }
  55.         }
  56.     }
  57.    
  58.     public static  void sequentialSearch(int[] arr, int size) {
  59.         System.out.println("SEQUENTIAL SEARCH:");
  60.         System.out.println("Please enter the item to be search:");
  61.         int search = sc.nextInt();
  62.         int count = 0;
  63.          for (int x = 0; x < size; x++) {
  64.             if (search == arr[x]) {
  65.                 System.out.println("Item " + search + " is found at location " + count);
  66.                 break;
  67.             }
  68.             count++;
  69.          }
  70.     }
  71.  
  72.     public static  void findMinimum(int[] arr, int size) {
  73.         System.out.println("FIND MINIMUM:\n");
  74.         int min = arr[0];
  75.         for (int j = 1; j < size; j++) {
  76.             if (arr[j] < min) {
  77.                 min = arr[j];
  78.             }
  79.         }
  80.         System.out.println("Minimum value among the list is " + min);
  81.     }
  82.  
  83.     public static  void findMaximum(int[] arr, int size) {
  84.         System.out.println("FIND MAXIMUM:\n");
  85.         int max = arr[0];
  86.         for (int n = 1; n < size; n++) {
  87.             if (arr[n] > max) {
  88.                 max = arr[n];
  89.             }
  90.         }
  91.         System.out.println("Maximum value among the list is " + max);
  92.     }
  93.  
  94.     public static void menu() {
  95.         String[] menuList = { "\n1 - POPULATE ARRAY ELEMENTS\n", "2 - PRINTING ARRAY ELEMENTS\n",
  96.                 "3 - INSERT NEW ELEMENTS\n", "4 - DELETE AN ITEM\n", "5 - SEQUENTIAL SEARCH\n", "6 - FIND MINIMUM\n",
  97.                 "7 - FIND MAXIMUM\n", "0 - EXIT\n" };
  98.         for (String menuItem : menuList) {
  99.             System.out.print(menuItem);
  100.         }
  101.     }
  102.  
  103.     public static void main(String[] args) {
  104.         int size = 10;
  105.         int[] myArr = new int[size+1];
  106.         int choice = 0;
  107.         do{
  108.             menu();
  109.             choice = sc.nextInt();
  110.             switch(choice) {
  111.                 case 1:
  112.                     populate(myArr);
  113.                     break;
  114.                 case 2:
  115.                     printing(myArr, size);
  116.                     break;
  117.                 case 3:
  118.                     insert(myArr, size);
  119.                     break;
  120.                 case 4:
  121.                     delete(myArr);
  122.                     break;
  123.                 case 5:
  124.                     sequentialSearch(myArr, size);
  125.                     break;
  126.                 case 6:
  127.                     findMinimum(myArr, size);
  128.                     break;
  129.                 case 7:
  130.                     findMaximum(myArr, size);
  131.                     break;
  132.             }
  133.         }while(choice != 0);
  134.         System.out.println("Exited...");
  135.     }
  136. }// End of the Class
  137.  
Add Comment
Please, Sign In to add comment