Overview: The purpose of this assignment is to analyze a set of measurements in
ID: 3854500 • Letter: O
Question
Overview: The purpose of this assignment is to analyze a set of measurements in terms of center, variability, and relative standing. This should be accomplished through writing a Java code that implements the required functionality. Requirements: Write a Java code that implements the following flow of functionalities: 1. It receives a number of measurements for a continuous quantitative variable. Your program should allow the user to enter a number of measurements whose maximum number is predefined. 2. For the set of measurements, your program should calculate the following numerical measures to describe its center: a. Mean. b. Median. c. Mode. 3. For the set of measurements, your program should calculate the following numerical measures to describe its variability: a. Variance b. Standard deviation. 4. Your program should check that the standard deviation has been successfully calculated through using the estimated relation between the standard deviation and the range.Explanation / Answer
//Solving the first 4 out of 6 Subparts of the given question following the predefined minimum protocol.
// Solution: Analyse a set of measurements
import java.io.*;
class Measurements
{
public static void main(String args[]) throws IOException
{
//Range for continuous quantitative variables
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Minimum and Maximum Value of the variables: ");
int minimum = Integer.parseInt(br.readLine());
int maximum = Integer.parseInt(br.readLine());
//Number of measurements in the set
System.out.println("Enter the total number of measurements: ");
int number = Integer.parseInt(br.readLine());
//Accepting the set of measurements from the user
double[] set = new double[number];
System.out.println("Range is from "+minimum+" to "+maximum);
System.out.println("Enter the set of measurements within the range: ");
int i;
for(i=0;i<number;i++)
{
set[i] = Double.parseDouble(br.readLine());
}
//Calculating numerical measures to describe its center
// 1. Mean
double sum=0.0;
for(i=0;i<number;i++)
{
sum = sum + set[i];
}
double mean = sum/number;
// 2. Median
//Sorting the elements
double temp = 0.0; int j;
for(i=0; i<number; i++)
{
for(j=1; j<(number-i); j++)
{
if(set[j-1]>set[j])
{
//Swapping
temp = set[j-1];
set[j-1] = set[j];
set[j] = temp;
}
}
}
double median=0;
int mid = number/2;
if (number%2 == 1)
median = set[mid];
else
median = (set[mid-1] + set[mid])/2;
//3. Mode:
double mod=0, countMax=0;
for (i=0;i<number;i++)
{
int count=0;
for (j=i+1;j<number;j++)
{
if (set[j] == set[i])
count++;
}
if (count>countMax)
{
countMax=count;
mod=set[i];
}
}
double mode = mod;
//Variance
temp = 0;
for(i=0;i<number;i++)
{
temp = temp + (set[i]-mean)*(set[i]-mean);
}
double variance = temp/(number-1);
//Standard Deviation:
double sd = Math.sqrt(variance);
System.out.println("Mean: " +mean+ " Median: "+median+ " Mode: "+mode+ " Variance: "+variance+ " Standard Deviation: "+sd);
//Check Standard deviation with relation : Sd <= range/2
double min = set[0];
double max = set[number-1];
double check = max - min;
double range = check/2;
if(sd<=range)
System.out.println("Standard deviation successfully calculated");
}
}
//Save this in a file named "Measurements.java"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.