Modify a text file: $/q3.java
$/q3.java
class Main { static String title= "CST-112: Quiz #3: "; static String author= "Thomas Jefferson"; // ++++ CHANGE author TO YOUR NAME public static void main(String[] args) { System.out.println( title + author ); int many=12; int[] a; a= new int[many]; // Values int[] aa= new int[many]; // Squares of the original values. // ++++ CHANGE array names (tho & jeff) to YOUR NAME! double[] tho= new double[many]; // Deviance from mean: a[i]-mu double[] jeff= new double[many]; // Square of deviance: d[i]^2 // [1a.] Change the String author to contain your own name. // Output the number of vowels [AEIOUaeiou] in the String author. int numvowels=0; // ++++ ADD YOUR CODE HERE System.out.println( "[1a.] Number of vowels in String author: " + numvowels ); ////////////////////////////////////////////////////////////////// fillI( a, many ); // Fill a[] array with random values. // System.out.print( "Original values: "); showI( a, many ); // Print the array a[]. // [2a.] Add one to every odd value in the array a[]. // ++++ ADD YOUR CODE HERE // [2b.] Determine the "range" of values in the a[] array (lowest to highest); print it. // ++++ ADD YOUR CODE HERE // [2c.] Exchange the highest and lowest values within the array. // (i.e. "swap" the two elements!) // ++++ ADD YOUR CODE HERE // [2d.] Fill the int array, aa[], with the squares of the original values, in a[]. // ++++ ADD YOUR CODE HERE // Print both arrays. System.out.print( "Modified values: "); showI( a, many ); System.out.print( "Array of squares: "); showI( aa, many ); ////////////////////////////////////////////////////////////////// // [3a.] Calculate the mean average of the values (using your sumI() method); // assign it to a variable named "mu" and print it. // ++++ ADD YOUR CODE OWN CODE BELOW, in the sumI() function. double mu= (double) sumI(a, many) / many; System.out.printf( "[3a.] mu: %6.2f\n", mu ); // [3b.] Count how many values are smaller than mu & how many are larger than mu; print these results. int smaller=0, larger=0; // ++++ ADD YOUR CODE HERE if (smaller>0) say( "[3b.] Number of values smaller than mu: " + smaller ); if (larger>0) say( "[3b.] Number of values larger than mu: " + larger ); // [3c.] Fill the array of "deviations" by subtracting mu from each value. // ++++ ADD YOUR CODE HERE prompt( "[3c.] Deviations:"); showD( tho, many ); // [3d.] Compute the sum of the "deviations"; print it. // ++++ ADD YOUR CODE OWN CODE BELOW, in the sumD() function. double sumd= sumD( tho, many ); System.out.println( "[3d.] sumd: " + sumd ); // [3e.] Fill the double[] dd array with the squares of the deviations. // ++++ ADD YOUR CODE HERE System.out.print( "[3e.] Dev-squared:"); showD( jeff, many ); ////////////////////////////////////////////////////////////////// //// Variance and sigma (standard-deviation). //// double variance=0, sigma=0; // [4a.] Calculate variance = mean average of jeff[]. // (Mean of squares of deviations). // ++++ ADD YOUR CODE HERE System.out.println( "[4a.] variance: " + variance ); // [4b.] Calculate sigma = square-root of the variance. // ++++ ADD YOUR CODE HERE System.out.println( "[4b.] sigma: " + sigma ); ////////////////////////////////////////////////////////////////// //// "Shortcut method" //// double ss=0; double vv2=0; double shortcut=0; // [5a.] Find the mean of aa[] ("array of squares"); // store this total in a variable named "ss" ("sum of the squares"). // ++++ ADD YOUR CODE HERE System.out.println( "[5a.] Mean average of the squares in aa[]: " + ss ); // [5b.] Subtract the square of mu from ss; store the result in vv2. // ++++ ADD YOUR CODE HERE System.out.println( "[5b.] ss - mu^2: " + vv2 ); // [5c.] Set shortcut equal to the square-root of vv2. System.out.println( "[5c.] shortcut: " + shortcut ); // [5d.] Check whether or not sigma2 and sigma2 are equal; if not, then print the percentage difference between them. double difference= shortcut - sigma; double pct= difference / sigma; if (shortcut == sigma) { System.out.println( "[5d.] shortcut == sigma" ); } else { System.out.println( "[5d.] Percentage difference is: " + pct +"%" ); } } ////////////////////////////////////////////////////////////////// // Sum an array of ints. // static double sumI( int a[], int m ) { int total=0; // ++++ ADD YOUR CODE HERE return total; } // Sum an array of doubles. // static double sumD( double d[], int m ) { double total=0; // ++++ ADD YOUR CODE HERE return total; } //// THESE FUNCTIONS ARE SUPPLIED WITH THE SAMPLE CODE //// // (No changes are needed below.) // Fill array with original values. // static void fillI( int[] a, int m ) { for (int i=0; i