Question: I am having a difficult time with this can I get some assistance? Goals Arrays and array utilities. Problem The median is the middle value
I am having a difficult time with this can I get some assistance?
Goals
Arrays and array utilities.
Problem
The median is the middle value in a sorted list of values.
For example if you have the following values: 5, 2, 1, 4, 3
You first sort them and get: 1, 2, 3, 4, 5
If the number of values is odd, getting the median is easy: it is the middle value in the array of the sorted values.
In the sorted list above, it would be the value at index 2 (note that array indexes start at 0) which is the value 3. The median is therefore 3.
If the number of values is even, you will need to take the average or mean of the middle two values.
For example, lets remove a number from the sorted list:
1, 2, 3, 4
Here, we need to get the values at index 1 (the value 2) and index 2 (the value 3), add them together and divide by 2. In this case, it would be 2+3 which would be 5/2 which is 2.5. The median therefore is 2.5.
User Story
Prompt the user for numbers until they enter the number 999. They can only enter a maximum of 5 numbers (be sure to check to make sure they dont provide more than 5). After they finish entering the numbers, print out the average of the numbers and the median of the number.
For example:
Enter an integer: 2
Enter an integer: 4
Enter an integer: 1
Enter an integer: 3
Enter an integer: 999
Average: 2.5
Median: 2.5
Tasks
1. We are going to reuse ProjectA so be sure to send in your ProjectA.java file from the previous extra
credit assignment before continuing to this one. Just include the Java source in the e-mail message
and make sure the subject is Project A.
2. Open your previous ProjectA
3. In your main before the loop, declare a 5-element array.
4. Inside the loop, as integers are entered, put them in consecutive elements of the array starting with index 0.
5. After the loop, add to your mean/average calculation the determination of the median. You may find it easier to create a method that returns the median.
6. When calculating the median, be careful that you not use array.length as it is always 5. You will instead want to use the count you are maintaining for calculating the mean to determine the elements you have put into the array.
7. Some helpful things you should know:
a. There is a utility that will sort an array of integers: search for sorting static arrays in java.
b. You will somehow need to handle not all elements of the array getting filled. Note that the
Java sort utility can help with this.
c. To determine if a number is even or odd, use the modulo operator in Java which is %. It
returns the remainder when dividing two numbers. For example, 4 % 2 divides evenly so
there is no remainder where 5 % 2 has a remainder of 1.
d. As we all know from the Weight project, if we divide two integers, we only get the integer
portion of the division and not the fractional part. For example, 5/2 is 2.5 but we only get 2
and the .5 Java throws away. Note that for an array with 5 filled elements (an odd number),
the index of the median index is 2 which happens to be the result of the integer division of the
number of filled elements divided by 2.
e. For an even number of elements like 4, integer dividing by 2 gives you 2 or the upper index of
the two median numbers you need to average. Therefore, that result minus 1 will give you the
lower index of the two numbers.
Thank you for your assistance.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
