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

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...
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 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...
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.
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...
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...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT