Question

In: Computer Science

(JAVA) Implement a ‘runnable’ Class called “NumericAnalyzer”. Here’s the functional behavior that must be implemented. NumericAnalyzer...

(JAVA)

Implement a ‘runnable’ Class called “NumericAnalyzer”. Here’s the functional behavior that must be implemented.

  • NumericAnalyzer will accept a list of 1 or more numbers as command line arguments. These numeric values do not need to be ordered (although you’ll need to display these values sorted ascendingly). NOTE: Don’t prompt the user for input – this is an exercise passing values to your program via the command line!
  • Error checking: if the user fails to pass in parameters, the program will display an error message (of your choice) and exit early.
  • The main() method’s String [] args argument values must be converted and assigned to a numeric/integer array.
  • Your program will display the following information about the numbers provided by the user:
    1. This list of numbers provided by the user sorted ascendingly.
    2. The size of number list (the number of numbers!)
    3. The average or mean value.
    4. The median - the middle value of the set of numbers sorted. (Simple Algorithm: index = the length of the array divided by 2).
    5. The min value.
    6. The max value.
    7. The sum
    8. The range: the difference between the max and min
    9. Variance: Subtract the mean from each value in the list. This gives you a measure of the distance of each value from the mean. Square each of these distances (and they’ll all be positive values), add all of the squares together, and divide that sum by the number of values (that is, take the average of these squared values) .
    10. Standard Deviation: is simply the square root of the variance.

Solutions

Expert Solution

Please find the below well-commented code:

Code text:

import java.util.*;
import java.lang.*;

class NumericAnalyzer {
   void sortedList(int[] list){                                   //sorts the list
       Arrays.sort(list);
       System.out.print("User list sorted ascendingly: ");
       for(int i = 0;i < list.length; i++)
           System.out.print(list[i] + " ");                       //print the elements of list
       System.out.print("\n");
   }
  
   void sizeOfTheList(int[] list){                                   //to print the size of the list
       System.out.println("The Size of the List:"+list.length);
   }
  
   void averageOrMean(int[] list){                                   //prints average of the list
       System.out.print("Average Value of the List:");
       int sum=0;
       for(int i = 0;i < list.length; i++)
           sum=sum+list[i];                                       //calculate the sum
       System.out.println((sum+0.0)/list.length);                   //print the median
   }
  
   void median(int[] list){
       Arrays.sort(list);
       System.out.print("The Median of the List:");              
       System.out.println(list[list.length/2]);                   //Prints median
   }
  
   void min(int[] list){                                           //min element
       Arrays.sort(list);
       System.out.println("Minimum value of the list: " + list[0]);  
   }
  
   void max(int[] list){                                           //max element
       Arrays.sort(list);
       System.out.println("Minimum value of the list: " + list[list.length-1]);
   }
  
   void sum(int[] list){                                           //print sum of the list
       int sum=0;
       for(int i = 0;i < list.length; i++)
           sum=sum+list[i];
       System.out.println("Sum of list is : " + sum);
   }
  
   void range(int[] list){                                           //print the range
       Arrays.sort(list);
       System.out.println("The range : " + (list[list.length] - list[0]));
   }
  
   void variance(int[] list){                                       //function to print the variance
       Arrays.sort(list);
       int sum=0,squaredSum = 0;
       for(int i = 0;i < list.length; i++)
           sum=sum+list[i];
      
       int mean = sum/list.length;
      
       for(int i = 0;i < list.length;i++){
           list[i] = list[i] - mean;
           list[i] = list[i]*list[i];
           squaredSum = squaredSum + list[i];                       //calculate the squared sum
       }
      
       System.out.println("The variance : "+ ((squaredSum+(0.0))/list.length));   //print the variance
   }
  
   void standardDeviation(int[] list){                               //function to print the variance
       Arrays.sort(list);
       int sum=0,squaredSum = 0;
       for(int i = 0;i < list.length; i++)                          
           sum=sum+list[i];
      
       int mean = sum/list.length;
      
       for(int i = 0;i < list.length;i++){
           list[i] = list[i] - mean;
           list[i] = list[i]*list[i];
           squaredSum = squaredSum + list[i];
       }
       double variance = (squaredSum+0.0)/list.length;                  
      
       System.out.println("Standard Deviation: " + Math.sqrt(variance));   //print the squared sum
   }
}


public class NumericAnalyzerMain{
   public static void main(String [] args){
       if(args.length == 0){
           System.out.println("ERROR****Please pass in some arguments\n");   //error if no command line args
           System.exit(1);
       }
      
       int [] list = new int[args.length];
      
       try{                                                               //will raise error if command line args is not
           for(int i = 0; i < args.length; i++){                           //integer
               list[i] = Integer.parseInt(args[i]);                  
           }
       }
       catch (NumberFormatException nfe) {
System.out.println("The argument must be an integer/numeric.");
System.exit(1);
}
      
       NumericAnalyzer numericAnalyzer = new NumericAnalyzer();           //instance of NumericAnalyzer
      
       //call the individual methods
      
       numericAnalyzer.sortedList(list);
      
       numericAnalyzer.sizeOfTheList(list);
      
       numericAnalyzer.averageOrMean(list);
      
       numericAnalyzer.median(list);
      
       numericAnalyzer.min(list);
      
       numericAnalyzer.max(list);
      
       numericAnalyzer.sum(list);
      
       numericAnalyzer.variance(list);
      
       numericAnalyzer.standardDeviation(list);
   }
}

Output:


Related Solutions

JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K and n o Create an overloaded constructor to initialize the variables into any positive integers such that n > k > 0o Create a method that calculates the value of binomial coefficient, C(n,k) , using the following rule: ▪ For an array of size (n+1) X (k+1) ▪ Array [n][k] = Array[n-1][ k-1]+ Array[n-1] [k]▪ Array[n][0]= Array[n][n] = 1 ▪ Hence, C(n,k) = array...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of       ...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of        * the comparator is to compare the alphabetic order of integers. With Q3,        * the order of Integer elements will be sort according to the order of        * digit Unicode.        * For example, the value of {11,3,31,2} will return {11,2,3,31}        * NOTE: You don't need to compare each digit one by one. Just compare them System.out.println("Q3...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
To ensure that an OS remains fully functional, you must implement a preventive maintenance plan. A...
To ensure that an OS remains fully functional, you must implement a preventive maintenance plan. A preventive maintenance plan provides many benefits to users and organizations such as decreased downtime, improved performance, improved reliability, and lower repair costs. In a minimum of 175 words, describe at least 3 Windows tools or utilities researched on the Internet that you should include and perform in a Preventive Maintenance Plan.
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT