COMP 1006 - Assignment 5

Due Friday March 1, 2013 at 9 p.m.

Submit a single zip file called userID.assignment5.zip to cuLearn with all of your solutions to the following problems. Submit early and submit often. Here, userID is your Carleton userID number.


Multidimensional Arrays: A basic review of arrays can be found here. More information about arrays and multidimensional arrays can be found here.
  1. Create a class called Matrix that stores and manipulates 2-dimensional matrices of integers. The class must have the following methods:

    public int get(int row, int column)
    // get the integer value in the given row and column 
    // (rows and columns start at zero)
    
    
    public void set(int row, int column, int value)
    // set the matrix element in the given row and column to the given value
    // (rows and columns start at zero)
    
    
    public void negate()
    // negates each element in the matrix
    // 
    
    public void add(Matrix m)
    // adds the matrix m to this
    
    
    public Matrix invert()
    // returns a new matrix by inverting the current matrix.
    // And by invert, I really mean compute the TRANSPOSE of the matrix
    // If the original matrix was 3x4 and given by
    // a b c d
    // e f g h
    // i j k l
    // then the inverse of this matrix is the 4x3 matrix
    // a e i
    // b f j
    // c g k
    // d h l
    
    
    public void printMatrix()
    // prints the elements of the matrix to the console  
    // prints elements, row by row, with data separated by a space
    // example: a 3x4 matrix when printed might look like 
    // 1 3 5 -1
    // 4 6 9 3
    // 2 38 3 5
    // don't worry about lining up the columns
    
    
    public Matrix(int rows, int columns)
    // Constructor for Matrix objects.
    // Create an empty matrix (filled with zeros) 
    // with dimension given by input parameters 
    
    
    
    Your Matrix class cannot have a main() function/method. Be sure to test your code with another driver class (that does have a main method).

    Submit your Matrix.java file for this problem (in your zip file).

  2. Create a class called Pixels. Your class should have the following method:

    // Input:  image - a 2-dimensional array of integers
    //                 with values in the range [0,255]
    //                 representing grayscale values for a pixel
    //         psize - an integer
    //                 the size of each new "pixel" after pixelation
    //
    // Output: a 2-dimensional array of integers that is the 
    //         "pixelated" version of the input array, where
    //         each consecutive psizeXpsize grid of pixels 
    //         is averaged together
    // Example: averaging each neighbouring 2x2 grid of 
    //          grayscale values.
    //
    //   int[][] img = { {2,4,31,31}, 
    //                   {3,3,21,41},
    //                   {1,2,10,20},
    //                   {3,2,20,30}};
    //
    //   calling Pixels.pixelate(img,2) returns the following array
    //                3,3,31,31
    //                3,3,31,31
    //                2,2,20,20
    //                2,2,20,20
    //
    //   calling Pixels.pixelate(img,1) returns a copy of img
    //
    public static int[][] pixelate(int[][] image, int psize)
    
    Use integer arithmetic for the averages. If the width or height of the input array is not a multiple of the new pixel size (psize) then the new "pixels" on the right-hand side and/or the bottom may be smaller than psizeXpsize. Just compute the average of whatever is inside this "pixel".

    Submit your Pixels.java file for this problem (in your zip file).


Strings: A basic review of Strings can be found here.
  1. Create a class called Words. The class should have the following method:

    public static boolean isPalindrome(String word)
    // Input : a string
    // Output: true if the input string is a palindrome*
    //         false otherwise
    //
    // * Recall that a palindrome is a sequence of characters
    //   that is the same when read forward and backwards.
    //
    // * For this question we will ignore whitespace
    //   when determining if a string is a palindrome or not.
    //
    // Examples: Words.isPalindrome("a") => true
    //           Words.isPalindrome("cat") => false    
    //           Words.isPalindrome("w o    w") => true    
    //           Words.isPalindrome("   a  ") => true    
    //           Words.isPalindrome("mom!") => false    
    
    Note that Problem 5 should be done at the same time as you work on Problem 3.

    Submit your Words.java file for this problem (in your zip file).

  2. Create a class called DNA. The class should have two methods:

    public static String compress(String dna)
    // input: a string consisting only of the letters G,A,T and C.
    //        (they may be upper, lower, or mixed case)
    // output: a string that represents a condensed version of
    //         the input string.  Each occurrence of a 
    //         non-repeated letter is copied to the new string. 
    //         Each occurrence of a sequence of a single letter that
    //         is repeated two or more times is replaced by
    //         that letter and number of occurrences in the sequence.
    //         All letters in the output should be capitalized.
    // Example: DNA.compress("GGCcCTtttTT") => "G2C3T6"
    //          DNA.compress("Cat") => "CAT"         
    
    public static String expand(String dna)
    // Input: a compressed sequence of DNA 
    //        (the same form as the output of compress())
    //        (the letters in the input may be any case)
    // Output: the long form sequence of DNA that the 
    //         input represented.
    //         All letters in the output should be capitalized.
    //         The function should accept input that is slightly
    //         incorrect as shown in the 3rd example below.
    // Examples: DNA.expand("G2T5") => "GGTTTTT"
    //           DNA.expand("cat") => "CAT"
    //           DNA.expand("ccca3") => "CCCAAA"
    
    For this question you will need to be able to access individual elements of a string, concatenate strings, convert (sub)strings to numbers and convert numbers to strings.

    Note that Problem 6 should be done at the same time as you work on Problem 4.

    Submit your DNA.java file for this problem (in your zip file).


Testing: For more information about testing see this document. (Coming soon!)
  1. Create a testing class called TestWords to test your Words class from Question 3.

    Submit your TestWords.java file for this problem (in your zip file).

  2. Create a testing class called TestDNA to test your DNA class from Question 3.

    Submit your TestDNA.java file for this problem (in your zip file).