Homework 6 Csc211.

Due Saturday March 14 (11:59pm)


Problem 1 (50 Points) A movie theater has decided to donate a percentage of the gross amount generated from a movie. The percentage to donate, the number of tickets sold and ticket price for adult and child tickets are stored in a file. Write a program that computes the gross amount and outputs.

The format of the file will be: 10 200 10.50 220 5.25 Using the above example 10 percent would be donated. 200 adult tickets were sold at $10.50, 220 child @ $5.50.

Turn in TotalTicketSales.java. Upload to COL using the link hw6a.


Problem 2 (40 Points)
Add the following methods to ArrayTool.java

First run the program and study the methods already present.

public static double standardDeviation(int [] array ) // Returns the standard deviation of the integers. // See explanation about standard deviation at the end of this document public static int max(int [] array) // Returns the maximum value in the array // Note: 1) You cannot assume the array is sorted. 2) You cannot sort the array. You have to traverse the array and find the maximum. public static boolean isIt(int [] array, int target) // returns true if the target is an element of the array // else false public static int difference(int [] array) // returns the difference between the largest and smallest value in the array // HINT: use the mix and max methods

Use the main method included to test your methods.

Turn in ArrayTool.java. Upload to COL using the link hw6b.


Extra Credit
(2% added to your overall average)

Add the following methods to ArrayTool.java

public static int[] merge(int [] array1, int [] array2) // Returns a reference to a new array with the values // from the first array and second array merged into one. // Note: 1) Both arrays being passed in will be sorted 2) You cannot copy the first array into the new array then start copying the second array and resort. You must follow the pseudo code below. public static void reverse(int [] array) // reverses the the elements of the array. // EX // if the array is 2 4 6 8 // then after calling the reverse method the array is 8 6 4 2

Standard Deviation

For more info on the standard deviation Go Here

The standard deviation is the square root of the variance. To compute the variance... 1) Find the mean of the array (Remember you have a method to do this) 2) For each integer in the array, subtract the mean from it then square that result. (remember the square of X is X * X ). Sum up all the squared results. 3) Find the average of these squares. Divide the total of the squares by the number of elements in the array. This is the variance. EX/ --------------- Let data be a reference to | 5 | 10 | 15 | --------------- - mean is 10 - sum of the differences squared is: 50 50 = (5-10)*(5-10) + (10-10)*(10-10) + (15-10)*(15-10) -variance is 50/3 = 16.6666 (Note dividing by the number of integers minus 1 gives a better indicator ) variance is 50/2 = 25 The Math class has a static method to find the square root of a number. So the Standard deviation is. Math.sqrt(variance) = 5

Merge algorithm

Write a method that takes as input two arrays (assume integers) sorted in non decreasing order, call them A1 and A2, and returns a third sorted array that contains a copy of each value from A1 and A2.

Example if

 A1 is    1 5 7 10  and
 A2 is    2 4 8 11
 Your method must return a reference to the array

 1 2 4 5 7 8 10 11
 
A solution using Pseudo code merge( A1, A2 ) A1Index = 0 A2Index = 0 Create an array A3 big enough to hold all values A3Index = 0 while A1Index < A1Length and A2Index < A2Length if A1[A1Index] < A2[A2Index] A3[A3Index] = A1[A1Index] Increment both A3Index and A1Index else A3[A3Inxex] = A2[A2Index] Increment both A3Index and A2Index while A1Index < A1Length A3[A3Index] = A1[A1Index] Increment both A3Index and A1Index while A2Index < A2Length A3[A3Index] = A2[A2Index] Increment both A3Index and A2Index return Array