Question

In: Computer Science

IN JAVA your software company has been requested to provide a prototype program performing the simple...

IN JAVA

your software company has been requested to provide a prototype program performing the simple statistical evaluations. You will need to implement a main program that reads the values into the array (or you could write another method to read them in), then call each of the various methods that you will convert from your previous programming project. The following methods comprise the StatPackage.

The function specifications are as follows:

Your StatPackage will accept up to 500 values as input.

Numerical values will be printed with two significant digits to the right of the decimal point.

The program will contain the following functions:

static double calcMean (int scores [], count) calculates the mean value for the values stored in the array scores mean is essentially the same as the average.

static double calcMedian (int scores [], count) calculates the median value for the values stored in the array scores median is the is middle value of the stored values.

static double calcVariance (int scores [], count) calculates the variance value for the values stored in the array scores Variance is determined by the formula:

            the sum of the square of the values / count
                                - square of the sum of the values / square of the count
                    

static double calcStdDev (double variance) calculates the standard deviation value for the values stored in the array scores. Standard deviation is the square root of the variance.

Solutions

Expert Solution

import java.io.*;
import java.util.Arrays;

public class StatPackage {

   public static double calcMean(int[] scores, int count) {
       return sum(scores) / (double) count;
   }

   public static double calcMedian(int[] scores, int count) {
       int[] sorted = sort(Arrays.copyOf(scores, count));
       int n = count/2;
       if(count % 2 == 1) {
           //length is odd
           return (double) sorted[n];
       }
       //else we average the two middle values
       return (sorted[n-1]+sorted[n])/ (double) 2;
   }

   public static double calcVariance(int[] scores, int count) {
       int a = 0;
       int b = 0;
       for(int i = 0; i < count; i++) {
           a += scores[i] * scores[i];
       }
       int sum = sum(scores);
       b = sum*sum;
       return ((double)a)/count - ((double) b)/(count * count);
   }

   public static double calcStdDev(int[] scores, int count) {
       return Math.sqrt(calcVariance(scores, count));
   }

   public static void main(String[] args) throws IOException {
       BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
       String line;
       int[] arr = new int[500];
       int i = 0;
       while((line = stdin.readLine()) != null && i < arr.length) {
           arr[i] = Integer.parseInt(line);
           i++;
       }
       if(i != arr.length) {
           arr = Arrays.copyOf(arr, i);
       }
       System.out.printf("Mean: %.02f%n", calcMean(arr, arr.length));
       System.out.printf("Median: %.02f%n", calcMedian(arr, arr.length));
       System.out.printf("Variance: %.02f%n", calcVariance(arr, arr.length));
       System.out.printf("Standard Deviation: %.02f%n", calcStdDev(arr, arr.length));
   }


   //
   //Utilities
   //

   //Merge-sort that doesn't mutate the original
   //As it copies twice every recursion, it is not very time/space efficient
   //First wrote something like this for AP CS
   private static int[] sort(int[] arr) {
       int[] res = Arrays.copyOf(arr, arr.length);
       if(res.length == 0) {
           return new int[0];
       }
       if(res.length == 1) {
           return res;
       }
       if(res.length == 2) {
           if(res[0] > res[1]) {
               swap(res, 0, 1);
               return res;
           }
           return res;
       }
       int n = arr.length / 2;
       int[] left = sort(Arrays.copyOfRange(res, 0, n));
       int[] right = sort(Arrays.copyOfRange(res, n, res.length));
       int i = 0, j = 0;
       while(i < left.length && j < right.length) {
           if(left[i] < right[j]) {
               res[i+j] = left[i];
               i++;
           } else {
               res[i+j] = right[j];
               j++;
           }
       }
       if(i+j != res.length) {
           if(i == left.length) {
               for(; j < right.length; j++) {
                   res[i+j] = right[j];
               }
           } else if(j == right.length) {
               for(; i < left.length; i++) {
                   res[i+j] = left[i];
               }
           }
       }
       return res;
   }

   //Swaps two values in-place
   private static void swap(int[] arr, int a, int b) {
       int temp = arr[a];
       arr[a] = arr[b];
       arr[b] = temp;
   }

   private static String toString(int[] arr) {
       String str = "";
       for(int i : arr) {
           str += i + " ";
       }
       return str;
   }

   //Sum doesn't even need a count variable because the array will init to 0;
   private static int sum(int[] arr) {
       int sum = 0;
       for(int i : arr) {
           sum += i;
       }
       return sum;
   }

   private static String toString(double[] arr) {
       String str = "";
       for(double d : arr) {
           str += String.format("%.02f ", d);
       }
       return str;
   }
}


Related Solutions

You are tasked to code a software prototype of this feature in Java. The system will...
You are tasked to code a software prototype of this feature in Java. The system will be provided the course code, course name, CRN, the total number of students, course credit hours.   The system should calculate and display the following statistics: Total student credit hours, which is the total number of students times course credit hours. FTE for the course, which is the total student credit hours divided by 40. Total amount of tuition generated in USD from the course....
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
Suppose you are requested, as an IT project manager, to develop software applications to provide solutions...
Suppose you are requested, as an IT project manager, to develop software applications to provide solutions to certain issues at your workplace, place of study, or personal uses. In this assignment, you need to design a new software system that will solve these issues. In order to design that system, you need to identify, business requirements (functional and nonfunctional), identify an appropriate process model, and conduct a feasibility study. You also need to discuss ethical and professional issues related to...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Write a simple java program that produces any text art ("ASCII art") picture. Your program can...
Write a simple java program that produces any text art ("ASCII art") picture. Your program can produce any picture you like, with the following restrictions: • The picture should consist of between 3 and 200 lines of output, with no more than 100 characters per line. • The code must use at least one for loop or static method but should not contain infinite loops.
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use of interfaces. The program should not take up more than half a page of size A4.
The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
Your software company was invited to provide a proposal for a company in Australia. You currently...
Your software company was invited to provide a proposal for a company in Australia. You currently have the cost in US dollars and need to convert the prices to the Australian dollar. Write a 2-part program using Ruby, Java®, or Python. Part 1: Write a function to gather the following costs from the user: Travel Cost: $9,800 Hotel Cost: $3,500 Rental Car Cost: $1,600 Labor Cost: $15,500 Part 2: Write a function to convert the costs from United States dollar...
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT