Tuesday, November 8, 2011

Exam #2 programming problems

I strongly suggest that each student develop a Java solution to the following problems in preparation for the coding portion of the first exam. While I do not promise that the practical part of the exam will be *exactly* the same as any one of these problems, the actual test question will at least be very similar to several of these practice problems. More than likely, there will be five program options on the test and you'll need to complete four of the five.

  1. Complete exercise 7.2, p252. The class name must be SumMajorDiagonal. You must write it as a command line application. This problem requires that you write three methods: one for inputting the elements of the matrix, one for printing the matrix, and one for summing the elements of the major diagonal. The major diagonal is the diagonal that runs from the upper left corner to the lower right corner of the matrix. This means that the row and column indicies are the same for each element in the major diagonal (e.g., [0][0], [1][1], [2][2], etc). You may fill your matrix with randomly generated numbers (0-9 is always nice and tidy).

    These are the headers:

       public static int sumMajorDiagonal(int[][] matrix)
       public static int[][] generateMatrix()
       public static void printMatrix(int[][] matrix)


    Sample Output
    Here is a screenshot of my code running.

    Sum Major Diagonal output


  2. Complete exercise 6.10, p229. The class name must be SmallestElement. You must write it as a command line application. This problem requires that you write a method for determining the location of the smallest value in an array. You may randomly fill the array or request user input. This is the header:

       public static int indexOfSmallest(int[] array)

    Your output must indicate both the array location and the actual value.

    Sample Output
    Here is a screenshot of my code running.

    SmallestElement output


  3. Write a program that declares a multidimensional array to hold a set of squares. The first row will hold the side measurements; the second row will hold the calculated perimeters; and the third row will hold the calculated areas.

    You will need to write four methods. These are the headers:

       public static void populateMatrix(int[][] matrix)
       public static void areas(int[][] matrix)
       public static void perimeters(int[][] matrix)
       public static void printMatrix(int[][] matrix)


    populateMatrix fills in the side length values and then calls areas and perimeters to calculate and fill the appropriate spots in the matrix.

    Sample Output
    Here is a screenshot of my code running.

    Square Calculations output


  4. A matrix or multidimensional array is an arrangement of data in rows and columns. Write a console application named SwitchRowsCols.java. This class will have two methods; the first one to create a new matrix with the rows and columns switched and the second to print. These are the headers:

       public static char[][] switchRowsAndCols(char[][] matrix)
       public static void printMatrix(char[][] matrix)


    In main, you will declare a rectangular (i.e., not square) matrix of character data. In switchRowsAndCols, you will create a new matrix, using the row and column lengths of the original matrix. Fill the new matrix with the data from the original matrix but switch the row and column data.

    Sample Output
    Here is a screenshot of my code running.

    Switch Rows and Columns output


  5. Complete exercise 6.14, p230. Instead of having the user enter a series of numbers, create an array with the values 48, 112, 32, 144, 80. You will need to write two methods. These are the headers:

       public static int computeGCD(int... numbers)
       public static void display(int[] list, int gcd)


    You might want to first sort the numbers array, placing the smallest value in the first position of the array..

    Sample Output
    Here is a screenshot of my code running.

    Compute GCD of Array output


  6. Write a GUI program that populates an array with ten randomly generated values between 0 and 99. Your code must calculate the average of the values in the array and then count how many of them are strictly greater than the average. You will need four methods: one to create and populate the array; one to computer the average; one to count how many elements are higher than the average; and another to display the output. These are the headers:

       public static int[] createArray()
       public static double computeAverage(int[] array)
       public static int countAboveAverage(int[] array, double avg)
       public static void displayResults(double avg, int count)


    Note that while you are using an int array, the average you calculate may not be a whole number, hence computeAverage returns a double. You will also need to make use of the StringBuilder in order to build a tidy string for your JOptionPane's output.

    Sample Output
    Here are screenshots of three runs of my code.



    Above Average output


  7. Complete exercise 6.15 on p230. For this problem, you will need to write three methods: one to create and fill the array with random numbers one to detect and eliminate duplicate values; and one to print arrays. These are the headers:

       public static int[] createArray()
       public static int[] eliminateDuplicates(int[] numbers)
       public static void printArray(int[] array)


    Instead of requesting user input, just randomly fill the array. eliminateDuplicates returns a shorter array containing only unique values. The simplest approach involves sorting the elements of the larger array before shipping it off to eliminateDuplicates.

    Sample Output
    Here is a screenshot of my code running.

    Random No Duplicates output


  8. The mode of a set of numbers is that value which occurs most often in the set. Since more than one number can occur the same number of times, there can be multiple modes in a data set. Refine the code from the previous problem to detect and display the mode(s) of your randomly generated numbers. These are the headers:

       public static int[] createArray()
       public static int[][] createHistogram(int[] numbers)
       public static int[] findModes(int[][] hist)
       public static void printArray(int[] numbers)


    This problem is considerably more complex than simply eliminating duplicate values but is not any more complicated than the WeeklyHours lab assignment.

    Sample Output
    Here is a screenshot of my code running.

    Find Modes output (single mode)

    Find Modes output (multiple modes)


  9. The median of an ordered set of numbers is the center value. If an array has an odd number of elements, the value at array position length/2 is the median. If the array has an even number of elements, the median is the average of the two center elements; that is, the average of the elements at (length/2) - 1 and length/2.

    Write a program that requests the user input a series of integers and then determines the median of the set. You will need to write four methods: one to get the size of the set from the user; one to collect the integers of the set from the user; one to determine the median; and one to print the array. These are the headers:

       public static int getSetSize(Scanner input)
       public static int[] getData(Scanner input, int n)
       public static double getMedian(int[] list)
       public static void printArray(int[] array)


    Sample Output
    Here are screenshots of two runs of my code.

    Find Median output (even set size)

    Find Median output (odd set size)


  10. Write a program that creates a multidimensional array. The columns will be n, sqrt(n), n-squared, and n-cubed. You will need to write three methods: the first will set the values for the n column; the second will fill the other three columns of the matrix; and the third will print the matrix. These are the headers:

       public static void setNs(double[][] grid)
       public static void computeValues(double[][] grid)
       public static void printMatrix(double[][] grid)


    printf is really handy for formatting your output. You can make a double look like an int by using the format string "%2.0f", by the way.

    Sample Output
    Here is a screenshot of my code running.

    Pow and Sqrt output