In: Computer Science
This program is to be written in Java Language. Thank you
A College has conducted a student survey. Students were asked to rate their satisfaction with remote learning courses. Students rated their satisfaction on a scale of 1 to 5 (1 = "I hate it", 5 = "I love it"). The student responses have been recorded in a file called "StudentResponses.txt". Each line of the file contains one student response.
Program 1 You are to write a program that reads and analyzes the survey responses. Your program should read the contents of the file and place the responses into an ArrayList. Your program should write a report to a file. The file should contain the number of students who responded to the survey, along with the mean, median, and standard deviation of the responses.
Program 2 Read the file "StudentResponses.txt" and count the number of occurances of the integers from 1 to 5. Use these counters to calculate the mean, median, and standard deviation.
Your program should use functions to calculate the mean, median and standard deviation. Each function should have an ArrayList as a parameter, and should return a double. Each function including the main should have a clearly stated algorithm.
Short Summary:
Implemented the program as per requirement
Attached source code and sample output
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
//This class reads a file and calculates mean,median and standard deviation
public class StudentsSurvey {
//Main function
public static void main(String[] args) {
//Declare array of size as per
requirement
int[] initialArray=new
int[200];
try {
//Read file
using scanner
Scanner sc=new
Scanner(new File("C:\\Users\\asus\\StudentResponses.txt"));
int i=0;
while(sc.hasNext())
{
initialArray[i]=sc.nextInt();
i++;
}
int
arrayLength=i;
//Create an
array with number of elements size
int[] array=new
int[arrayLength];
for(int
j=0;j<arrayLength;j++)
array[j]=initialArray[j];
sc.close();
//Write to a
file
FileWriter
obj=new FileWriter(new
File("C:\\Users\\asus\\StudentResponseStatistics.txt"));
obj.write("No
Of students responded to the survey:"+arrayLength+"\n");
System.out.println("No Of students responded to the
survey:"+arrayLength);
//Mean
obj.write("Mean:"+String.valueOf(mean(array))+"\n");
//Median
obj.write("Median:"+String.valueOf(median(array))+"\n");
//Standard
Deviation
obj.write("Standard
Deviation:"+String.valueOf(standardDeviation(array))+"\n");
obj.flush();
obj.close();
} catch (FileNotFoundException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
//This method calculates mean of given array
private static double mean(int[] array)
{
double mean;
int sum, i;
int n = array.length;
sum = 0;
//Calculate sum
for(i = 0; i < n; i++) {
sum+=array[i];
}
mean=(double)sum/n;
return mean;
}
//This method calculates median of given array
private static double median(int[] array)
{
int n = array.length;
// sort the array
Arrays.sort(array);
if (n % 2 != 0)
return
(double)array[n / 2]; // even case
return (double)(array[(n - 1) /
2] + array[n / 2]) / 2.0;
}
//This method calculates standardDeviation of given
array
private static double standardDeviation(int[]
array)
{
double sum = 0.0, standardDeviation
= 0.0;
int n = array.length;
//Calculate sum
for(double num : array) {
sum +=
num;
}
double mean = sum/n;
for(double num: array) {
standardDeviation += Math.pow(num - mean, 2);
}
return Math.sqrt(standardDeviation/n);
}
}
Code Screenshot:
Output:
IP File:
OP File:
**************Please do upvote to appreciate our time. Thank you!******************