Homework B -- Utility Methods and Statistics

Due: Nov 3 prior to 11:59:59 PM

In this homework you will

Setup

Do the usual thing .... go to your CS113 directory and create a HW2 directory therein. Put all of your work for this assignment into the CS113 directory.

Arrays and Methods

Part 2 of this week's lab had you do two tasks.
  1. Convert a set of command line inputs into an array of int
  2. Create a new array that is the reverse the array of int
This homework extends and builds on these tasks.

Convert args in a method

Rather than converting all of the command lines arguments into numbers in the main method, write a method to do this conversion. The method should have the following signature:
    public static int[] convertArgs(String[] args)
That is, given an array of Strings your method will convert each of the strings into an integer, and return an array of integers. For instance, suppose your program was named Utilities (and it is in the file Utilities.java) then given the command line java Utilities 1 1 2 3 5 8 13 your convertArgs function would receive an array
    ["1", "1", "2", "3", "5", "8", "13"]
and would return the array
    [1,1,2,3,5,8,13]
If the command line arguments contain a non-integer it is OK for your program to die.

Print Array

When you try to print the contents of an Array using java .. i.e. you have code like:
    int[] arr = new int[2];
    arr[0]=1;
    arr[1]=21;
    System.out.println(arr);
You do not get the contents of the array, rather you get something incomprehensible like [DA34B55398. This is the value of the pointer to the array, which had nothing to do with its contents. So write a method that takes an array and prints out its contents in a nice table. For instance, given arr as above, the table might look like:
    0 1
    2 21
Do not worry about getting things into nicely aligned columns.

The signature of this method should be

        public static void printArray(int[] nums)
    

Average

Write a method to compute the average of an array of integers. The signature of this method should be
    public static double computeAverage(int[] nums)
For example, given an array[1,1,2,3,5,8,13], this function should return 4.714285714285714 (the number of decimals you print may be different).

Max

Write a method to compute the average of an array of integers. For instance, given the data [1,13,2,8,3,5], the max is 13. Note that the max may appear anywhere in the array, not just at the end. You max finder must be able find a max from among any set of integers. For instance [-99000, -99001] should return -99000.

Standard Deviation

The formula for computing the sample standard deviation is
    sqrt( (sum( (x[i])-average(x))*(sum(x[i])-average(x))) / x.length-1 )
For instance, given the array [1,1,2,3,5,8,13] the calculation of standard deviation would look like
    aver = 4.7  /// use the average function to compute
    sqrt( ((1-aver)^2 + (1-aver)^2 + (2-aver)^2 + (3-aver)^2 + (5-aver)^2 + (8-aver)^2 + (13-aver)^2) / (7-1) )
    sqrt((13.79+13.79+7.36+2.93+0.08+10.79+68.65)/6)
    sqrt(19.57)
    4.42 
Your method should return the standard deviation -- as a double -- when given an array of ints. (In java, you can compute the square root using Math.sqrt(num). For instance Math.sqrt(25.0) will return 5.0.)

Lo res grapher

Finally make a lo-res horizontal bar graph of your data. The idea here is to print o a single line the number of X's given by the integer. So for the data [1,1,2,3,5,8,13] the graph would be
X 
X 
XX 
XXX 
XXXXX 
XXXXXXXX 
XXXXXXXXXXXXX 
You will most likely use a for loop within a for loop to so this. In Java you can print things on the same line using System.out.print. So, for instance
    System.out.print("hi");
    System.out.print(" there");
    System.out.println(" Clara");
    System.out.println(" and others");
would result in
    hi there Clara 
    and others

Putting it together

Finally, write a main that shows the operation of all of the methods you wrote. For instance:
    java Utilities 1 6 2 5 3 4
    The numbers entered are:
    0 1
    1 6
    2 2
    3 5
    4 3
    5 4
    The average is: 3.5
    The max is: 6
    The standard deviation is: 1.87
    X 
    XXXXXX 
    XX 
    XXXXX 
    XXX 
    XXXX 

Submitting

Create a readme

Use VSC to create another file in your HW2 directory. This file should be named "Readme". The contents of this file should follow this sample.

You should have at least 2 files in your HW2 directory: Utilities.java, and Readme. (You might also have .class files.)

Submit

If you did this work on your own computer

You will first need to copy the files from you own computer to a lab computer. To do so, yu can go into the lab as with HW2 or use ssh. Either way, you will need to create HW2 directory within your CS113 directory on the Unix machines. Recall, that this can be done with the following commands:
        cd
        cd CS113 
        mkdir HW2 
    
Once you have made the HW2 directory in Unix, open a terminal on you own computer and in that terminal use "cd" to navigate to the directory containing your work for this assignment. Assuming you use the same directory structure on your own computer and in the lab, this process can be accomplished with the following commands
        cd
        cd CS113
        cd HW2
    
Then use the scp command to copy each of the files you want to submit from your computer to the lab. For example:
        scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW2/Readme
    
As always, when you read "UNIX_NAME" put in your UNIX user name. Also, with each scp command you will need to enter your UNIX password.

Actually submit

Open a terminal in UNIX (again, you can use SSH to do so from your laptop) and execute the following Unix commands (assuming you put HW2 directory into a CS113 directory in your home directory).
        cd
        cd CS113
        /home/gtowell/bin/submit -c 113 -d HW2 -p 2
    
In response to the submit command you should see a series of messages ending with:
        
        Submitting archive...
        Submission complete! Submission timestamp is 2023-08-08-15-30-28-EDT.