I want the solutions in Netbeans a)The mean of a list of numbers is its arithmet
ID: 3914060 • Letter: I
Question
I want the solutions in Netbeans
a)The mean of a list of numbers is its arithmetic average. The median of a list is its
middle value when the values are placed in order. For example, if a list contains 1,
4, 7, 8, and 10, then the mean is 6 and the median is 7. Write an application that
allows you to enter five integers and displays the values, their mean, and their
median. Save the file as MeanMedian.java.
b. Revise the MeanMedian class so that the user can enter any number of values up
to 20. If the list has an even number of values, the median is the numeric average
of the values in the two middle positions. Save the file as MeanMedian2.java.
Explanation / Answer
a)
import java.util.Scanner;
public class MeanMedian
{
public static void main(String[] args)
{
int n, sum = 0;
float avg;
Scanner inp = new Scanner(System.in);
int a[] = new int[5];
System.out.println("Enter all the elements:");
for(int i = 0; i < 5 ; i++)
{
a[i] = inp.nextInt();
sum = sum + a[i];
}
System.out.println("The elements in the list are :");
for(int i = 0; i < 5 ; i++)
{
System.out.println(a[i]);
}
avg = (float)sum / 5;
System.out.println("Mean:"+avg);
System.out.println("Median:"+a[2]);
}
}
b)
import java.util.Scanner;
public class MeanMedian
{
public static void main(String[] args)
{
int n, sum = 0;
float avg;
Scanner inp = new Scanner(System.in);
int a[] = new int[5];
System.out.println("Enter no. of elements you want in array:");
n = inp.nextInt();
System.out.println("Enter all the elements:");
for(int i = 0; i < n ; i++)
{
a[i] = inp.nextInt();
sum = sum + a[i];
}
System.out.println("The elements in the list are :");
for(int i = 0; i < n ; i++)
{
System.out.println(a[i]);
}
int mid = n/2;
avg = (float)sum / n;
System.out.println("Mean:"+avg);
System.out.println("Median:"+(float)((a[mid]+a[mid+1])/2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.