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 Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
In Java, design and implement a class called Cat. Each Cat class will contain three private...
In Java, design and implement a class called Cat. Each Cat class will contain three private variables - an integer representing its speed, a double representing its meowing loudness, and a String representing its name. Using the Random class, the constructor should set the speed to a random integer from 0 to 9, the meowing loudness to a random double, and the name to anything you want; the constructor should take no parameters. Write “get” and “set” methods for each...
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...
Implement Java source code for a class called Temperature that does the following: a. Prompts the...
Implement Java source code for a class called Temperature that does the following: a. Prompts the user to input a temperature b. Prints “Too Hot!” when the input temperature is above 75 c. Prints “Too Cold!” when the input temperature is below 40 d. Prints “Just Right!” when the input temperature is 40 to 75 e. Be precise, include comments, prologue, etc. if needed.
in java Design and implement a class called Dog that contains instance data that represents the...
in java Design and implement a class called Dog that contains instance data that represents the dog’s name and age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in “person years” (seven times the dog’s age). Include a toString method that returns a one-line description of the dog. Create a Tester class called Kennel, whose main...
I am to complete the following for Java but getting compiler errors...Implement a base class called...
I am to complete the following for Java but getting compiler errors...Implement a base class called Student that contains a name and major. Implement the subclass GraduateStudent, which adds a property called stipend. Here is what I have for code with the compiler error following. public class GraduateStudent extends Student {       private double stipend;       public GraduateStudent(String name, String major, double stipend) {               super(name);        super(major);           this.stipend = stipend;...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators,...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT