Write a program that can be used to gather statistical data about the number of
ID: 3693898 • Letter: W
Question
Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should perform the following steps: A) Ask the user how many students were surveyed. An array of integers with this many elements should then be dynamically allocated. B) Allow the user to enter the number of movies each student saw into the array. C) Calculate and display the average, median, and mode of the values entered. (Use the functions to calculate the median and mode.)
Explanation / Answer
MeanMedianMode.java
package org.students;
import java.util.Scanner;
import java.util.Arrays;
public class MeanMedianMode {
public static void main(String[] args) {
int totMovies=0,length;
double mean=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the no of Students data you want to enter::");
int num=sc.nextInt();
int stdMovArr[]=new int[num];
for(int i=0;i<num;i++)
{
System.out.print("No of Movies see by Student "+(i+1)+" in a month::" );
stdMovArr[i]=sc.nextInt();
totMovies+=stdMovArr[i];
}
//Calculating Mean
mean=totMovies/num;
System.out.println("The Mean of the Array is::"+mean);
//Calling Methods
median(stdMovArr);
mode(stdMovArr);
}
//Method to Calculate Mode
private static void mode(int[] stdMovArr) {
int count2 = 0;
int count1 = 0;
int pupular1 =0;
int popular2 =0;
for (int i = 0; i < stdMovArr.length; i++)
{
pupular1 = stdMovArr[i];
count1 = 0; //see edit
for (int j = i + 1; j < stdMovArr.length; j++)
{
if (pupular1 == stdMovArr[j]) count1++;
}
if (count1 > count2)
{
popular2 = pupular1;
count2 = count1;
}
else if(count1 == count2)
{
popular2 = Math.min(popular2, pupular1);
}
}
System.out.println("The Mode of the Array is::"+ popular2);
}
//Method to calculate Median
private static void median(int[] stdMovArr) {
int length=0;
System.out.println("The Elements in the array before sorting are::");
for(int i=0;i<stdMovArr.length;i++)
{
System.out.print(stdMovArr[i]+" ");
}
//To sort the Array in the Ascending order
Arrays.sort(stdMovArr);
System.out.println(" ");
System.out.println("The Elements in the array After sorting are::");
for(int i=0;i<stdMovArr.length;i++)
{
System.out.print(stdMovArr[i]+" ");
}
System.out.println(" ");
length=stdMovArr.length;
if(length%2==0)
{
int val =length/2;
System.out.println("The Median is::"+stdMovArr[val-1]+" , "+stdMovArr[val]);
}
else
{
int val=(length+1)/2;
System.out.println("The Median is::"+stdMovArr[val-1]);
}
}
}
__________________________________________________________________________________________
output:
Enter the no of Students data you want to enter::9
No of Movies see by Student 1 in a month::3
No of Movies see by Student 2 in a month::4
No of Movies see by Student 3 in a month::5
No of Movies see by Student 4 in a month::6
No of Movies see by Student 5 in a month::7
No of Movies see by Student 6 in a month::7
No of Movies see by Student 7 in a month::7
No of Movies see by Student 8 in a month::8
No of Movies see by Student 9 in a month::9
The Mean of the Array is::6.0
The Elements in the array before sorting are::
3 4 5 6 7 7 7 8 9
The Elements in the array After sorting are::
3 4 5 6 7 7 7 8 9
The Median is::7
The Mode of the Array is::7
______________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.