In Java Programming 8th edition chapter 9 #2 b. How do I fix the following? (I u
ID: 3853163 • Letter: I
Question
In Java Programming 8th edition chapter 9 #2 b. How do I fix the following? (I used the solution provided and it runs but my professor says the following is still wrong, I don't understand. Please help!)
In MeanMedian2 program,
(2) Both mean and median should be double
(3) The program always receives 20 numbers, but there should be a way to accept any numbers less than 20. This requires a way to stop user input, and a count variable to count the number of input numbers.
(4) User input can be either odd or even numbers. Median calculation is different on these two cases. Also, the use of Arrays.sort() will be different too.
Explanation / Answer
import java.util.*;
class MeanMedian
{
public static void main(String s[])
{
int list[] = new int[20];
int t,x,n=0;
double mean=0, median=0;
Scanner sc= new Scanner(System.in);
System.out.println("Please enter max of 20 numbers or and NAN to exit");
try
{
for(int i=0;i<20;i++)
{
x=sc.nextInt();
list[i]=x;
n++;
}
}
catch(Exception e)
{
System.out.println("Thanks for the input");
}
for(int i=0;i<n;i++)
{
mean = mean+list[i];
}
mean=mean/n;
// Sort the list in aesc order
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(list[i]>list[j])
{
t=list[i];
list[i]=list[j];
list[j]=t;
}
}
}
int m=n/2;
if(n%2==0)
{
median= (list[m]+ list[m-1])/2;
}
else median= list[m];
System.out.println("Mean of the numbers is "+mean +" Median of the numbers is "+median);
for(int i=0;i<n;i++)
{
System.out.println(list[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.