The purpose of this assignment is to give you practice using objects and data types. The first exercise focuses on the String data type; the second exercise involves the Color and Picture data types.


  1. Huntington’s disease detector. Huntington’s disease is an inherited and fatal neurological disorder. Although there is currently no cure, in 1993 scientists discovered a very accurate genetic test. The gene that causes Huntington’s disease is located on chromosome 4 and has a variable number of (consecutive) repeats of the CAG trinucleotide. The normal range of CAG repeats is between 10 and 35. Individuals with Huntington’s disease have between 36 and 180 repeats. Doctors can use a PCR-based DNA test; count the maximum number of repeats; and use the following table to generate a diagnosis:

    repeats diagnosis
    0–9 not human
    10–35 normal
    36–39 high risk
      40–180     Huntington’s  
    181– not human

    Write a program Huntingtons.java to analyze a DNA string for Huntington’s disease and produce a diagnosis. To do so, implement the following public API:

    public class Huntingtons {
    
        // Returns the maximum number of consecutive repeats of CAG in the DNA string.
        public static int maxRepeats(String dna)
    
        // Returns a copy of s, with all whitespace (spaces, tabs, and newlines) removed.
        public static String removeWhitespace(String s)
    
        // Returns one of these diagnoses corresponding to the maximum number of repeats:
        // "not human", "normal", "high risk", or "Huntington's".
        public static String diagnose(int maxRepeats)
    
        // Sample client (see below).
        public static void main(String[] args)
    
    }
    
    Here is some more information about the required behavior:

    Here are a few sample executions:

    ~/Desktop/oop1> cat repeats4.txt
    TTTTTTTTTT TTTTTTTTTT TTTTTTTTCAGCAGCAGCAG TTTCAGCAGT TTTTTTTTTT
    TTTTTTTTTT TTTTTTTTTT TTTTTTTTTTTTTCAGTTTT TTTTTTTTTT T
    
    ~/Desktop/oop1> java-introcs Huntingtons repeats4.txt
    max repeats = 4
    not human
    
    ~/Desktop/oop1> cat repeats64.txt
    TTTTTTTTTT TTTTTTTTTT TTTTTTTTTT TTTTTTTTTT TTTTTTTTTT TTCAGCAGCA
    GCAGCAGCAG CAGCAGCAGC AGCAGCAGCA GCAGCAGCAG CAGCAGCAGC AGCAGCAGCA
    GCAGCAGCAG CAGCAGCAGC AGCAGCAGCA GCAGCAGCAG CAGCAGCAGC AGCAGCAGCA
    GCAGCAGCAG CAGCAGCAGC AGCAGCAGCA GCAGCAGCAG CAGCAGCAGC AGCAGCAGCA
    GCAGTTTTTT TTTTTTTTTT TTTTTTTTTT TTTTTTTTTT TTTTTTTTTT TTTTTTTTTT
    
    ~/Desktop/oop1> java-introcs Huntingtons repeats64.txt
    max repeats = 64
    Huntington's
    
    ~/Desktop/oop1> java-introcs Huntingtons chromosome4-hd.txt
    max repeats = 79
    Huntington's
    
    ~/Desktop/oop1> java-introcs Huntingtons chromosome4-healthy.txt
    max repeats = 19
    no Huntington's
    


  2. Kernel filter. Write an image-processing library KernelFilter.java that applies various kernel filters to images, such as Gaussian blur, sharpen, Laplacian, emboss, and motion blur. A kernel filter modifies the pixels in an image by replacing each pixel with a linear combination of its neighboring pixels. The matrix that characterizes the linear combination is known as the kernel.

    More specifically, to apply a kernel filter to a grayscale image, perform the following operation for each pixel p:

    To apply a kernel filter to a color image, perform the above operation to the red, green, and blue components of each pixel p separately, and combine the results.

    The following table describes the kernel filters that you will implement and illustrates the results using a classic test image (baboon.png):

    image filter kernel result
    identity \( \left [ \begin{matrix} 0 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 0 \end{matrix} \right ] \)
    Gaussian blur \( \displaystyle \frac{1}{16} \;\; \left [ \begin{matrix} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \end{matrix} \right ] \)
    sharpen \( \left [ \begin{matrix} 0 & -1 & 0 \\ -1 & 5 & -1 \\ 0 & -1 & 0 \end{matrix} \right ] \)
    Laplacian \( \left [ \begin{matrix} -1 & -1 & -1 \\ -1 & 8 & -1 \\ -1 & -1 & -1 \end{matrix} \right ] \)
    emboss \( \left [ \begin{matrix} -2 & -1 & 0 \\ -1 & 1 & 1 \\ 0 & 1 & 2 \end{matrix} \right ] \)
    motion blur \( \displaystyle \frac{1}{9} \;\; \left [ \begin{matrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{matrix} \right ] \)

    To do so, implement the following public API:

    public class KernelFilter {
    
        // Returns a new picture that applies the identity filter to the given picture.
        public static Picture identity(Picture picture)
    
        // Returns a new picture that applies a Gaussian blur filter to the given picture.
        public static Picture gaussian(Picture picture)
    
        // Returns a new picture that applies a sharpen filter to the given picture.
        public static Picture sharpen(Picture picture)
    
        // Returns a new picture that applies an Laplacian filter to the given picture.
        public static Picture laplacian(Picture picture)
    
        // Returns a new picture that applies an emboss filter to the given picture.
        public static Picture emboss(Picture picture)
    
        // Returns a new picture that applies a motion blur filter to the given picture.
        public static Picture motionBlur(Picture picture)
    
        // Test client (ungraded).
        public static void main(String[] args)
    
    }
    
    Here is some more information about the required behavior:

    Kernel filters are widely used for image processing in applications such as Photoshop or GIMP. They are also used in convolutional neural networks to extract features from an image and in digital cameras to remove camera shake.


    Submission. Submit a .zip file containing Huntingtons.java and KernelFilter.java. Use only Java features that have already been introduced in the course (e.g., String, In, Color, and Picture, but not regular expressions).


    This assignment was developed by Kevin Wayne.
    Copyright © 2019.