Purpose: This homework is to practice implementing and using user defined method
ID: 3704875 • Letter: P
Question
Purpose: This homework is to practice implementing and using user defined methods. Problem Description: Create a small mathematics library with the following functionality.
• calculate the average of an array of floats
• calculate the median of an array of floats
• calculate the range of values in an array of floats Write a Java program to do the following: 1. Given the following sets of exam scores, calculate the average, median and range of each one. {100.0, 59.0, 37.5, 85.3, 97.0, 75.0, 77.7, 37.0, 95.0} {10.0, 20.0, 30.0, 40.0, 50.0} Requirements: In addition to fulfilling the conditions described above your program must also …
1. Have the main class named ‘MathLibs’ 2. Have user defined methods to perform the bulleted actions listed above.
3. Operate without receiving any input from the user or a file. It should simply display information about the sets of data. These may be hardcoded into the program.
4. You are not allowed you use any existing math methods.
5. Your output should match up with the sample output provided below.
6. Your methods must have headers which describe the method, inputs and any return value.
7. Your implementation must be general such that if the input were changed, it would still function as expected.
Here is some starter code.
// YOUR FILE HEADER HERE public class MathLibs { public static void main(String[] args) {
double[] exam1Scores = {100.0, 59.0, 37.5, 85.3, 97.0, 75.0, 77.7, 37.0, 95.0};
double[] exam2Scores = {10.0, 20.0, 30.0, 40.0, 50.0};
System.out.println("Stats for exam 1...");
System.out.printf("Avg: %4.2f ", average(exam1Scores));
// System.out.printf("Standard Deviation: %4.2f ", stdDeviation(exam1Scores);
System.out.printf("Median: %4.2f ", median(exam1Scores));
System.out.printf("Range: %4.2f ", range(exam1Scores));
System.out.println(" Stats for exam 2...");
System.out.printf("Avg: %4.2f ",average(exam2Scores));
// System.out.printf("Standard Deviation: %4.2f ", stdDeviation(exam2Scores);
System.out.printf("Median: %4.2f ", median(exam2Scores));
System.out.printf("Range: %4.2f ", range(exam2Scores)); }
/*************************************************************
* median - Determine the median of a set of values.
* The median is middle most value of a sort list of values.
* If there is an even number of values then the average of
* the two middle most values should be returned.
* double[] data - an array of data, that may or may not be sorted
*/
public static double median(double[] data) {
// CODE HERE TO DETERMINE THE MEDIAN
return 0;
}
// YOUR HEADER HERE public static double average(double[] data) {
// CODE HERE TO DETERMINE THE AVERAGE
return 0;
}
// YOUR HEADER HERE public static double range(double[] data) {
// YOUR CODE HERE TO DETERMINE RANGE
return 0;
}
Explanation / Answer
MathLibs.java
public class MathLibs { public static void main(String[] args) {
double[] exam1Scores = {100.0, 59.0, 37.5, 85.3, 97.0, 75.0, 77.7, 37.0, 95.0};
double[] exam2Scores = {10.0, 20.0, 30.0, 40.0, 50.0};
System.out.println("Stats for exam 1...");
System.out.printf("Avg: %4.2f ", average(exam1Scores));
// System.out.printf("Standard Deviation: %4.2f ", stdDeviation(exam1Scores);
System.out.printf("Median: %4.2f ", median(exam1Scores));
System.out.printf("Range: %4.2f ", range(exam1Scores));
System.out.println(" Stats for exam 2...");
System.out.printf("Avg: %4.2f ",average(exam2Scores));
// System.out.printf("Standard Deviation: %4.2f ", stdDeviation(exam2Scores);
System.out.printf("Median: %4.2f ", median(exam2Scores));
System.out.printf("Range: %4.2f ", range(exam2Scores)); }
/*************************************************************
* median - Determine the median of a set of values.
* The median is middle most value of a sort list of values.
* If there is an even number of values then the average of
* the two middle most values should be returned.
* double[] data - an array of data, that may or may not be sorted
*/
public static double median(double[] data) {
if(data.length % 2 != 0){
return data[data.length/2];
}
else{
return (data[data.length/2] + data[(data.length-1)/2])/2;
}
}
public static double average(double[] data) {
// CODE HERE TO DETERMINE THE MEDIAN
double sum = 0;
for(int i=0;i<data.length;i++) {
sum+=data[i];
}
return sum/data.length;
}
// YOUR HEADER HERE
public static double range(double[] data) {
// YOUR CODE HERE TO DETERMINE RANGE
double min=data[0], max=data[0];
double total = 0;
for(int i=0;i<data.length;i++) {
if(max < data[i]) {
max = data[i];
}
if(min > data[i]) {
min = data[i];
}
}
return max-min;
}
}
Output:
Stats for exam 1...
Avg: 73.72
Median: 97.00
Range: 63.00
Stats for exam 2...
Avg: 30.00
Median: 30.00
Range: 40.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.