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

Create a new JAVA class inside your project folder. The name of your new JAVA cl

ID: 3627458 • Letter: C

Question

Create a new JAVA class inside your project folder. The name of your new JAVA class should be ListStats. Write a program that will read in a list of positive integers (including zero) and display some statistics regarding the integers. The user will enter the list in sorted order, starting with the smallest number and ending with the largest number. Your program must store the all of the integers in an array. The maximum number of integers that you can expect is 100, however there may not necessarily be that many. Your program should allow the user to continue entering numbers until they enter a negative number or until the array is filled - whichever occurs first. If the user enters 100 numbers, you should output a message stating that the maximum size for the list has been reached, then proceed with the calculations.
After you have placed all of the integers into an array, your program should perform the following tasks, in this order:
1. Display the count of how many numbers were read
2. Display the smallest number in the array
3. Display the largest number in the array
4. Display the median (the median is the integer that is stored in the middle position of the array)
5. Display the average of all the numbers
6. Allow the user to search the array for a specified value.
• First, ask the user to enter an integer to search for.
• Next, search the array to determine if the given integer is in the array.
• If the integer is not found, display a message stating that the integer is not in the list.
• If the integer is found, then display the position number of where you found the integer. If the integer happens to be in the array more than once, then you only need to tell the first position number where you found it.
• After performing the search, ask the user if he/she wants to search for more integers. Continue searching for numbers until the user answers "N".
Technical notes and restrictions:
• Remember that the keyboard object should be declared as a class variable (global).
• You are only allowed to use the number 100 one time in your program! You are not allowed to use the numbers 101, 99, 98, or any other number that is logically related to 100 anywhere in your program. If you need to make reference to the array size then, use the length variable to reference this rather than hard-coding numbers into your program.
• You are required to write and use at least FOUR methods in this program:
• A method to get the numbers from the user, and place them into an array. This method should return the number of integers that it actually retrieved. The array will be passed back through a parameter (remember that the method can make changes to an array's contents and the main program will automatically see the results).
• A method to calculate, and return, the median of the array
• A method to calculate, and return, the average of the numbers in the array
• A method to search for a specified value in the array. This method should return the first position number where the item is found, or return -1 if the item is not found.
• The two calculating methods, and the searching method, should not get any values from the user, and should not print anything to the screen. The main program is responsible for all of the printing.

The method which gets the numbers will need (of course) to get values from the user, but should not print anything to the screen.
• Absolutely NO global variables (class variables) are allowed except for the keyboard object.
• Remember that at this stage, COMMENTING IS A REQUIREMENT! Make sure you FULLY comment your program.
You also should include comments that explain what sections of code is doing. Notice the key word "sections"! This means that you need not comment every line in your program. Write comments that describe a few lines of code rather than "over-commenting" your program.
• You MUST write comments about every method that you create. Your comments must describe the purpose of the method, the purpose of every parameter the method needs, and the purpose of the return value (if the method has one).
• Build incrementally. Don't tackle the entire program at once. Write a little section of the program, then test it AND get it working BEFORE you go any further. Once you know that this section works, then you can add a little more to your program. Stop and test the new code. Once you get it working, then add a little bit more.
• Make sure you FULLY test your program! Make sure to run your program multiple times, inputting combinations of values that will test all possible conditions for your IF statements and loops. Also be sure to test border-line cases.

Explanation / Answer

import java.util.*;

public class ListStats {

    private static Scanner kb = new Scanner(System.in);
   
    /*
    * Takes an int array as a parameter
    * Fills the array with integers entered by the user,
    * terminating when a negative number is entered.
    * Returns the number of integers entered.
    */
    public static int inputNumbers(int[] arr) {
        System.out.println("Enter integers for the list.");
        System.out.println("Enter the integers in sorted order, smallest first.");
        System.out.println("Enter a negative number to terminate entry.");
       
        // length will keep track of how many numbers have been entered.
        int length = 0;
       
        // Read in the numbers from the user one by one, until a negative number is entered
        // or the array holding the numbers is full
        while(true) {
            System.out.print(">> ");
            int entry = kb.nextInt();
            kb.nextLine();
            if (entry < 0) break;
            arr[length] = entry;
            length++;
            if (length >= arr.length) {
                System.out.println("Maximum list size reached.");
                break;
            }
        }
       
        return length;
    }
   
    /*
    * Takes an array of ints as a parameter
    * and returns the median of the array
    * Assumes that the array is sorted smallest to biggest
    * and that all the array entries have been initialized
    */
    public static int median(int[] l) {
        return l[l.length/2];
    }
   
    /* Takes an array of ints as a parameter
    * and returns the average of the ints in the array
    * Assumes all the entries of the array have been initialized
    */
    public static double average(int[] l) {
        double sum = 0; // variable to hold the sum of the array's entries
       
        // Add each entry to the sum
        for (int entry : l) sum += entry;
       
        return sum / l.length;
    }
   
    /* Takes an array of ints and an int as parameters
    * Searches for the int parameter in the array
    * If the int is found, returns the index in the array where it first appears
    * If not found, returns -1
    */
    public static int search(int[] l, int target) {
        for (int i=0; i<l.length; i++) {
            if (l[i] == target) return i;
        }
        return -1;
    }
   
    public static void main(String[] args) {
        final int MAXSIZE = 100; // max list size;
        int [] arr = new int[MAXSIZE]; // array to hold the list of ints
        int length = inputNumbers(arr);
        int [] list = Arrays.copyOf(arr,length);
        System.out.println("Number of entries: "+length);
        System.out.println("Smallest entry: "+list[0]);
        System.out.println("Largest entry: "+list[list.length-1]);
        System.out.println("Median entry: "+median(list));
        System.out.println("Average of the entries: "+average(list));

        // Let the user search the list for integers
        while(true) {
            // Get an integer from the user
            System.out.print("Enter an integer to search the list for: ");
            int target = kb.nextInt();
            kb.nextLine();
           
            // Search the list
            int index = search(list,target);
           
            // Report the result
            if (index == -1) {
                System.out.println("The integer was not found in the list.");
            } else {
                System.out.println("Index of the integer in the list: "+index);
            }
           
            // Quit searching if the user wants to quit
            System.out.println("Search again? (Enter N to quit searching): ");
            if (kb.nextLine().equals("N")) break;
        }
    }

}

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