Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package activities;
- import java.util.Scanner;
- public class Menu {
- static Scanner sc = new Scanner(System.in);
- // Populate Array Elements
- public static void populate(int[] arr) {
- System.out.println("POPULATE ARRAY ELEMENT:");
- System.out.println("Please enter t number: ");
- for(int i=0; i<5; i++) {
- arr[i] = sc.nextInt();
- }
- }
- public static void printing(int[] arr, int size) {
- System.out.println("PRINTING ARRAY ELEMENT:");
- for (int i = 0; i < size; i++) {
- System.out.print(" " + arr[i]);
- }
- }
- public static void insert(int[] arr, int size) {
- System.out.println("INSERT NEW ITEM TO THE ARRAY ELEMENTS:");
- System.out.println("Please enter the item to be inserted:");
- int newItem = sc.nextInt();
- System.out.println("Please enter the location to be inserted:");
- int loc = sc.nextInt();
- for (int x = size-1; x >=(newItem-1); x--) {
- if(loc == x) {
- arr[x+1] = arr[x];
- }
- }
- arr[newItem-1] = loc;
- for(int i=0; i<size; i++) {
- System.out.print(" " + arr[i]);
- }
- }
- public static void delete(int[] arr) {
- System.out.println("DELETE AN ITEM:");
- System.out.println("Please enter the item to be deleted:\n");
- int delItem = sc.nextInt();
- for(int i = 0; i < arr.length; i++){
- if(arr[i] == delItem){
- // shifting elements
- for(int j = i; j < arr.length - 1; j++){
- arr[j] = arr[j+1];
- }
- break;
- }
- }
- }
- public static void sequentialSearch(int[] arr, int size) {
- System.out.println("SEQUENTIAL SEARCH:");
- System.out.println("Please enter the item to be search:");
- int search = sc.nextInt();
- int count = 0;
- for (int x = 0; x < size; x++) {
- if (search == arr[x]) {
- System.out.println("Item " + search + " is found at location " + count);
- break;
- }
- count++;
- }
- }
- public static void findMinimum(int[] arr, int size) {
- System.out.println("FIND MINIMUM:\n");
- int min = arr[0];
- for (int j = 1; j < size; j++) {
- if (arr[j] < min) {
- min = arr[j];
- }
- }
- System.out.println("Minimum value among the list is " + min);
- }
- public static void findMaximum(int[] arr, int size) {
- System.out.println("FIND MAXIMUM:\n");
- int max = arr[0];
- for (int n = 1; n < size; n++) {
- if (arr[n] > max) {
- max = arr[n];
- }
- }
- System.out.println("Maximum value among the list is " + max);
- }
- public static void menu() {
- String[] menuList = { "\n1 - POPULATE ARRAY ELEMENTS\n", "2 - PRINTING ARRAY ELEMENTS\n",
- "3 - INSERT NEW ELEMENTS\n", "4 - DELETE AN ITEM\n", "5 - SEQUENTIAL SEARCH\n", "6 - FIND MINIMUM\n",
- "7 - FIND MAXIMUM\n", "0 - EXIT\n" };
- for (String menuItem : menuList) {
- System.out.print(menuItem);
- }
- }
- public static void main(String[] args) {
- int size = 10;
- int[] myArr = new int[size+1];
- int choice = 0;
- do{
- menu();
- choice = sc.nextInt();
- switch(choice) {
- case 1:
- populate(myArr);
- break;
- case 2:
- printing(myArr, size);
- break;
- case 3:
- insert(myArr, size);
- break;
- case 4:
- delete(myArr);
- break;
- case 5:
- sequentialSearch(myArr, size);
- break;
- case 6:
- findMinimum(myArr, size);
- break;
- case 7:
- findMaximum(myArr, size);
- break;
- }
- }while(choice != 0);
- System.out.println("Exited...");
- }
- }// End of the Class
Add Comment
Please, Sign In to add comment