Question

In: Computer Science

The purpose of this lab is to manipulate an array of integers. The assignment is to...

The purpose of this lab is to manipulate an array of integers. The assignment is to write a program that: 1. Declares an array of integers of size equal to constant int SIZE = 10. 2. Implements the following methods:  A method that displays a menu. The menu should be displayed after each completed menu selection.  A method that prompts the user to initialize the array. The entire array does not need to be initialized. The user may enter only as many (or as few) values as they would like in the array. The user will enter a negative value to indicate no more to be added. Hint: This implies that the number of elements in the array does not necessarily have to be the same as the maximum size of the array.  A method that displays the contents of the array. Only elements currently contained in the array should be horizontally displayed.  A method that displays the minimum, maximum, sum and average of all the elements currently contained in the array.  A method that accepts a number and determines how many times (if any) that number appears as an element in the array.  A method that allows the user to add a number to the end of the array.  A method that allows you to insert a specified number at a specified index within the currently filled portion of the array. Hint: In order to accomplish this, the method must first check to see if there is room in the array to accept a new number. If the array is currently full (e.g. number of elements equals the maximum size of the array) then the user has to first delete an element from the array. Think carefully about what an insert into the array implies. The physical size of the array cannot change, therefore what must happen if a value is to be inserted somewhere in the middle of the array without losing any of the existing elements.  A method that removes an element at a specified index in the array. Hint: As with the insert method, the physical size of the array cannot change, therefore how could we simulate an element of the array being deleted? CSCI 125 Array Manipulation Prof. Kadri Page 2 of 4 Sample run: Menu only displayed once here. Your program should display the menu after each completed menu selection. 1. Initialize Array 2. Display Array 3. Add element to the end 4. Add an element at a specific index 5. Remove an element at specific index 6. Show min, max, sum and average 7. Search 8. Exit 1. Initialize Array Enter integer values to fill the array -ve value to stop: 1 2 3 4 5 6 8 -9 2. Display Array 1 2 3 4 5 6 8 6. Show min, max, sum and average Min=1 Max=8 Sum=29 Average=4.14 7. Search Enter number to search for: 2 2 occurs 1 time. 5. Remove an element at specific index Enter index between 0 and 6 7 Invalid index Enter index between 0 and 6 1 2. Display Array 1 3 4 5 6 8 4. Add an element at a specific index Enter index between 0 and 5 6 Invalid index Enter index between 0 and 5 3 Enter number to insert 9 CSCI 125 Array Manipulation Prof. Kadri Page 3 of 4 2. Display Array 1 3 4 9 5 6 8 1. Initialize Array Enter integer values to fill the array -ve value to stop: 1 2 3 4 5 6 7 8 9 10 11 -9 Array is full. One or more numbers could not be inserted 2. Display Array 1 2 3 4 5 6 7 8 9 10 3. Add element to the end Array is full. 8. Exit Goodby

import java.util.Scanner;
public class ArrayMenu
{
    static int count;
    static Scanner kb = new Scanner(System.in);
    
    public static void main()
    {
        int item=0;
        int[] numArray=new int[100];
        count=0;
        
        
        while (item !=8)
        {
            menu();
            item=kb.nextInt();
            if (item==1)
                initializeArray(numArray);
            else if (item==2)
                printArray(numArray);
        }
        
        System.out.println("Goodby!");
        
    }

    public static void menu()
    {
        System.out.println("1. Initialize Array");
        System.out.println("2. Display Array");
        System.out.println("3. Add element to the end");
        System.out.println("4. Add an element at a specific index");
        System.out.println("5. Remove an element at specific index");
        System.out.println("6. Show min, max, sum and average");
        System.out.println("7. Search");
        System.out.println("8. Exit");
        System.out.print(": ");
    }
    
    public static void initializeArray(int[] arr)
    {
        count=0;
        int num;
        System.out.print("Enter integer values to fill the array -vevalue to stop: ");
        do
        {
            num = kb.nextInt();
            if (num >=0)
            {
                arr[count]=num;
                count++;
            }
        } while (num > 0);
        
    }
    public static void printArray(int[] arr)
    {
        for (int i=0; i< count; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }

  }

CODING LANGAUGE JAVA

Solutions

Expert Solution

import java.util.Scanner;

import java.util.Scanner;

class ArrayMenu {

    static int count;

    static Scanner kb = new Scanner(System.in);

    public static void main(String[] args) {

        int item = 0;

        int[] numArray = new int[100];

        count = 0;

        while (item != 8) {

            menu();

            item = kb.nextInt();

            if (item == 1)

                initializeArray(numArray);

            else if (item == 2)

                printArray(numArray);

            else if (item == 3)

                add(numArray);

            else if (item == 4)

                add_index(numArray);

            else if (item == 5)

                remove(numArray);

            else if (item == 6)

                show(numArray);

            else if (item == 7)

                Search(numArray);

            else

                break;

        }

        System.out.println("Goodby!");

    }

    public static void menu() {

        System.out.println("1. Initialize Array");

        System.out.println("2. Display Array");

        System.out.println("3. Add element to the end");

        System.out.println("4. Add an element at a specific index");

        System.out.println("5. Remove an element at specific index");

        System.out.println("6. Show min, max, sum and average");

        System.out.println("7. Search");

        System.out.println("8. Exit");

        System.out.print(": ");

    }

    public static void initializeArray(int[] arr) {

        count = 0;

        int num;

        System.out.print("Enter integer values to fill the array -vevalue to stop: ");

        do {

            num = kb.nextInt();

            if (num >= 0) {

                arr[count] = num;

                count++;

            }

        } while (num > 0);

    }

    public static void printArray(int[] arr) {

        for (int i = 0; i < count; i++)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    public static void add(int[] arr) {

        // read element from user and insert to arr

        System.out.print("Enter element to insert at end: ");

        arr[count] = kb.nextInt();

        count++;

    }

    public static void add_index(int[] arr) {

        // read index and element from user and insert to arr

        System.out.print("Enter index at which value is to be inserted: ");

        int index = kb.nextInt();

        System.out.print("Enter element to insert: ");

        // shift all elements after index to right

        for (int i = count; i > index; i--) {

            arr[i] = arr[i - 1];

        }

        arr[index] = kb.nextInt();

        count++;

    }

    // System.out.println("5. Remove an element at specific index");

    public static void remove(int[] arr) {

        // read index from user

        System.out.print("Enter index from which value is to be removed: ");

        int index = kb.nextInt();

        // shift all elements after index to left

        for (int i = index + 1; i < count; i++) {

            arr[i - 1] = arr[i];

        }

        count--;

    }

    // System.out.println("6. Show min, max, sum and average");

    public static void show(int[] arr) {

        // declare required variables

        int min = arr[0], max = arr[0], sum = 0;

        // calculate

        for (int i = 0; i < count; i++) {

            if (arr[i] > max)

                max = arr[i];

            if (arr[i] < min)

                min = arr[i];

            sum += arr[i];

        }

        // calculate average

        float average = (float) sum / count;

        // display results

        System.out.println("\nMinimum element: " + min);

        System.out.println("Maximum element: " + max);

        System.out.println("Sum: " + sum);

        System.out.println("Average: " + average + "\n");

    }

    public static void Search(int[] arr) {

        // read element to search

        System.out.print("Enter element to search: ");

        int search = kb.nextInt();

        int found = -1;

        // search in array

        for (int i = 0; i < count; i++) {

            if (arr[i] == search) {

                found = i;

                break;

            }

        }

        if (found != -1)

            System.out.println(search + " found at index " + found);

        else

            System.out.println(search + " not found");

    }

}

Output:


Related Solutions

Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
There is a lab assignment. Basically includes array and some other basic stuff. Comment if you...
There is a lab assignment. Basically includes array and some other basic stuff. Comment if you have any questions. Thank you. I have got all classes here so that it might be more make sense. BUS CLASS package classes; //DO NOT ERASE THE TODO STUBS! WRITE YOUR SOLUTIONS BELOW THE STUBS! public class Bus extends Commercial {       private int numPassengers;       public Bus()    {        super();        numPassengers = 0;    }      ...
Purpose of Assignment  The purpose of this assignment is to allow the students to understand and...
Purpose of Assignment  The purpose of this assignment is to allow the students to understand and practice the measurement of present value, future value, and interest rate using Microsoft® Excel®.  Assignment Steps  Resources: Microsoft® Office® 2013 Accessibility Tutorials, Microsoft® Excel®, Time Value of Money Calculations Template Calculate the following time value of money problems using Microsoft® Excel®: If we place $8,592.00 in a savings account paying 7.5 percent interest compounded annually, how much will our account accrue to in 9.5 years? What is the present value of...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar...
Purpose of Assignment The purpose of this assignment is to allow the students to become familiar with and practice the measurement of Net Present Value (NPV), payback, and Weighted Average Cost of Capital (WACC) using Microsoft® Excel®. Assignment Steps Resources: Microsoft® Excel®, Capital Budgeting Decision Models Template Calculate the following problems using Microsoft® Excel®: Calculate the NPV for each project and determine which project should be accepted. Project A Project B Project C Project D Inital Outlay (105,000.000) (99,000.00) (110,000.00)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT