Homework Challenge

NOTE: The following three problems are OPTIONAL. All points earned will be applied to your homework average for the course in a manner deemed appropriate by the instructor.

1. Factorial [Savitch, Chapter 11, Problem 3] (5 points)

One of the most common examples of recursion is one algorithm to calculate the factorial of an integer. The notation n! is used for the factorial of the integer n and is defined as follows:

0! is equal to 1
1! is equal to 1
2! is equal to 2*1 = 2
3! is equal to 3*2*1 = 6
4! is equal to 4*3*2*1 = 24

           .
           .
           .
     
n! is equal to n*(n-1)*(n-2)*...*3*2*1

An alternative way to describe the calculation of n! is the recursive formula n*(n-1)! plus a stopping case of 0! being defined as 1. Write a static method that implements this recursive formula for factorial. Place the method in a test program that allows the user to compute n! (with an invocation of your static method). Your program should repeat the calcualtion for additonal inputted values of n unitl the user says she/he wants to end the program.

2. Two Dimensional Array (5 points)

Write a program that performs the following tasks (in order):

  1. Create and intitialize a 256 x 256 two dimensional array of integers whose values are random integers in the range 0 to 255 (inclusive).

  2. Print out the first 8x8 block of integers in the two dimensional array to the screen, in other words, print out the integers in rows 0 through 7 for columns 0 through 7 (be sure to preserve the row/column structure when printing the values to the screen, and delimit the numbers by tabs by using the \t escape character).

  3. Using nested for loops, iterate through the two dimensional array and first compute the average (mean) value of the elements in the current row, and then set the value of each element in the same row to the calculated mean. Once complete, all the elements in the first row [0] of the two dimensional array should have the same value, which is the average of the original values in the first row; All of the values in the second row [1] should be the average of the original values in the second row, and so forth till the last row [255].

  4. Repeat step 2.

A sample output might look like:

	212     78      85      6       250     247     102     115
	244     15      207     80      77      203     81      200
	247     92      60      128     8       105     23      232
	50      77      172     231     84      73      155     234
	2       159     118     185     222     7       135     77
	197     178     52      12      213     100     69      239
	224     139     195     223     239     67      5       23
	186     211     68      16      100     172     188     108

	------------------------------------------------------------

	128     128     128     128     128     128     128     128
	127     127     127     127     127     127     127     127
	134     134     134     134     134     134     134     134
	129     129     129     129     129     129     129     129
	129     129     129     129     129     129     129     129
	118     118     118     118     118     118     118     118
	134     134     134     134     134     134     134     134
	132     132     132     132     132     132     132     132
     

3. RightArrow / LeftArrow classes [Savitch, Chapter 7, Problem 2] (10 points)

In this exercise, you will defeine two derived classes of the class Figure in Display 7.7/page 484. Your two classes will be called RightArrow and LeftArrow. These classes will be like the classes Triangle and Box, but they will draw left- and right-pointing arrows that look like the following, which is a right-pointing arrow:

                   *
                   * *
                   *   *
        ************     *
                   *   *
                   * *
                   *
   
The size of the arrow is determined by two numbers, one for the size of the "tail", which is 12 in the preceding example, and one for the base of the arrow head, which is 7 in the preceding example. (If the size of the base is an even number, then the tail will not be exactly in the middle of the arrow head. That is OK.) Write a test program for each class that tests all the methods in the class. You can assume that the base of the arrow is 3 or more.