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

Write a program that will read in a list of positive integers (including zero) a

ID: 3705683 • Letter: W

Question

Write a program that will read in a list of positive integers (including zero) and display some statistics regarding the integers.

The user is assumed to 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 an 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:

Display the count of how many numbers were read

Display the smallest number in the array

Display the largest number in the array

Display the median (the median is the integer that is stored in the middle position of the array)

Display the average of all the numbers

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:

You should copy the methods created in the Assignment (parts A-D) and use them to create this project program. DO NOT CHANGE THE METHODS! They should be EXACTLY as they were written for the assignment.

My Assignment codes are:

import java.util.*;

public class GetNumbers {
  
public static Scanner kbd;
public static final int MAXSIZE = 8;
public static void main(String[] args) {
  
kbd = new Scanner(System.in);
int list[]=new int[MAXSIZE];
  
System.out.println("Enter a list of up to " + MAXSIZE + " positive integers. Enter a negative value to stop.");
  
int n = getNums(list);
if(n == MAXSIZE){
System.out.println("The maximum size of the list has been reached.");
}

System.out.println("The count of items entered is: "+n);
}
public static int getNums(int [] list) {
int n1;
int i = 0;
do{
n1 = kbd.nextInt();
if (n1 >= 0 ){
list[i]=n1;
i++;
}
  
}while(n1 >=0 && i<list.length);
return i;
}
}

public class CalculateMedian {

public static double median(int [] list, int size) {
double median_value = 0.0;
if (size % 2 != 0) median_value = list [size/2];
else median_value = (double)(list[(size-1)/2] + list[size/2])/2.0;

return median_value;
}
}

public class CalculateAverage {

public static double average(int [] list, int size) {
int total = 0;
for(int i=0;i<size;i++){
total+=list[i];
}
return total/(double)size;
}
}

public class SearchArray {
public static int search(int [] list, int size, int key) {
int i;
for(i = 0; i <size; i++){
if (list [i] == key)
return i;
}
return -1;
}
}

Absolutely NO other global variables (class variables) are allowed (except for the keyboard object).

Create a MAXSIZE constant and set it to 100. You are only allowed to use the number 100 one time in your program - which is to set the MAXSIZE constant. 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, or the MAXSIZE constant to reference this rather than hard-coding numbers into your program.

The main program is responsible for all of the printing. There should be no screen printing in any of the four methods that came from your assignment parts A-D.

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.

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:

Enter a list of up to 100 positive integers.
Please enter the values in ascending order.
Enter a negative value to stop.

2 42 53 53 767 -1

The count of items entered is: 5
The smallest item in the list is: 2
The largest item in the list is: 767
The median of the list is: 53.0
The average of the list is: 183.4


Enter an integer to search for: 42
Item was found at position 1
Do you want to search for more numbers (Y or N)?
Y
Enter an integer to search for: 54
Item was not found.
Do you want to search for more numbers (Y or N)?
N

Explanation / Answer

GetNumbers.java

import java.util.*;

public class GetNumbers {

public static Scanner kbd;

public static final int MAXSIZE = 100;

public static void main(String[] args) {

char ch;

kbd = new Scanner(System.in);

int[] list = new int[MAXSIZE];

int numberOfElements = getNums(list);

int searchElement;

System.out.println(" The count of items entered is: "

+ numberOfElements);

System.out.println("The smallest item in the list is: "

+ findMin(list, numberOfElements));

System.out.println("The largest item in the list is: "

+ findMax(list, numberOfElements));

System.out.println("The median of the list is: "

+ median(list, numberOfElements));

System.out.println("The average of the list is: "

+ average(list, numberOfElements));

do

{

System.out.print("Enter Element to Search :");

searchElement = kbd.nextInt();

int pos = search(list, numberOfElements, searchElement);

if (pos != -1) {

System.out.println("The element is found at position :" + pos);

} else {

System.out.println("Item was not found.");

}

// Getting the character from the user 'Y' or 'y' or 'N' or 'n'

System.out.print(" Do you want to search for more numbers(Y/N) ?");

ch = kbd.next(".").charAt(0);

}while((ch == 'Y' || ch == 'y'));

kbd.close();

}

private static int findMin(int[] list, int numberOfElements) {

int min = list[0];

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

if (min > list[i])

min = list[i];

}

return min;

}

private static int findMax(int[] list, int numberOfElements) {

int max = list[0];

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

if (max < list[i])

max = list[i];

}

return max;

}

/**

* Gets a list of positive integers (including zero) from the user, and

* places them into an array which has been previously initialized. The

* method stops when the user enters a negative integer, or when the array

* is full.

*

* @param list

* The array into which the numbers will be placed.

* @return The count of numbers which were placed into the array.

*/

public static int getNums(int[] list) {

int counter = 0;

int value = 0;

System.out.println("Enter a list of up to " + MAXSIZE

+ " positive integers.");

System.out.println("Enter a negative value to stop.");

value = kbd.nextInt();

while (value != -1) {

list[counter] = value;

counter++;

value = kbd.nextInt();

}

if (counter == MAXSIZE) {

System.out

.println("The maximum size of the list has been reached.");

}

return counter;

}

// B: Used to find the Median

/**

* Calculates the median of the integers in array 'list'

*

* @param list

* The array containing the integers for which to calculate the

* median

* @param size

* The number of integers in array 'list'

* @return The median of the integers in the array

*/

public static double median(int[] list, int size) {

double medianValue = 0.0;

if (size % 2 != 0)

medianValue = list[size / 2];

else

medianValue = (double) (list[(size - 1) / 2] + list[size / 2]) / 2.0;

return medianValue;

}

// C: Method to find the average (missing create your own)

/**

* Calculates the average of the integers in array 'list'

*

* @param list

* The array containing the integers for which to calculate the

* average

* @param size

* The number of integers in array 'list'

* @return The average of the integers in the array

*/

public static double average(int[] list, int size) {

double sum = 0;

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

sum += list[i];

}

return sum / size;

}

// D: method named search which searches for a specified integer in an array

// of integer

/**

* Searches for an integer in an array

*

* @param list

* The array containing the list of integers to search

* @param size

* The number of integers in the array

* @param key

* The integer to search for

* @return The first position number of where key is found in the array, or

* -1 if key is not found in the array.

*/

public static int search(int[] list, int size, int key) {

int i;

for (i = 0; i < size; i++) {

if (list[i] == key)

return i;

}

return -1;

}

}

_________________

Output:

Enter a list of up to 100 positive integers.
Enter a negative value to stop.
2 42 53 53 767 -1

The count of items entered is: 5
The smallest item in the list is: 2
The largest item in the list is: 767
The median of the list is: 53.0
The average of the list is: 183.4
Enter Element to Search :42
The element is found at position :1

Do you want to search for more numbers(Y/N) ?Y
Enter Element to Search :54
Item was not found.

Do you want to search for more numbers(Y/N) ?N

__________________Thank You

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