The purpose of this assignment is to introduce you to programming with input and output, including text, sound, graphics, and images.


  1. Display RGB-encoded images.  There are lots of different encodings for image files; StdPicture handles JPEG, PNG, GIF, BMP, and TIFF depending on the extension of the filename input to StdPicture.read(). (All of these are RGB encodings, but others, such as CMYK, are more suitable for printing.)

    In this part of the assignment, you will write a program Display.java that loads and displays (with StdPicture) images described as a lists of RGB values in the following format.

    Input format. An RGB-encoded image is described by the following sequence:

    The diagram below shows this structure in the file nassau-hall.txt.

    Here are some sample executions:

    ~/Desktop/io> javac-introcs Display.java
    
    ~/Desktop/io> java-introcs Display < nassau-hall.txt 
    
    Nassau Hall
    
    ~/Desktop/io> java-introcs Display < johnson-arch.txt 
    
    Johnson Arch
    


  2. Dataset anonymization.  As computer scientists (and the tools they build) become increasingly relied upon to make sensitive decisions (like college admissions or mortgage rates), it's important to learn techniques to mitigate privacy leaks. A basic rule of thumb is to not give decision-makers more information than is strictly needed to make the decision.

    In this problem, you will implement a basic dataset anonymization technique. Write a program Anonymize.java which, given in a dataset (in StdIn) and a feature name as a command-line argument, writes the dataset with the feature removed (to StdOut).

    Input format. You will process a dataset in CSV format, with \(n\) entries, where each entry consists of one value for each of \(m\) features. (You may think of this as a table with \(n\) rows and \(m\) columns.) It is described in the following sequence of lines (i.e., strings separated by the newline character \n):

    Notice that the format does not give the values of \(m\) or \(n\). You may find the String method split() useful.

    The diagram below shows this structure in the file fruits.csv.

    Here are some sample executions:

    ~/Desktop/io> javac-introcs Anonymize.java
    
    ~/Desktop/io> java-introcs Anonymize taste < fruits.csv 
    name,color
    banana,yellow
    strawberry,red
    orange,orange
    pineapple,yellow
    
    ~/Desktop/io> java-introcs Anonymize price < fruits.csv 
    name,color,taste
    banana,yellow,sweet
    strawberry,red,sour
    orange,orange,sweet
    pineapple,yellow,sour
    
    ~/Desktop/io> java-introcs Anonymize SSN < patients_small.csv 
    BIRTHDATE,FIRST,LAST,RACE,GENDER,HEALTHCARE_EXPENSES
    1980-12-04,Doyle,Johns,white,M,1008922.16
    2011-09-03,Timmy,Pfannerstil,white,M,253391.99
    2011-04-25,Monica,Nikolaus,white,F,136549.01
    1949-01-31,Michele,Murphy,hawaiian,F,2015686.52
    1969-10-08,Ellen,Parker,black,F,1670546.89
    1982-08-28,Cruz,Okuneva,white,F,1564013.09
    1959-03-23,Mika,Daugherty,white,F,2002214.73
    2010-12-15,Silvia,Pichardo,other,F,34871.86
    1983-09-27,Olin,Shanahan,white,M,963576.68
    
    ~/Desktop/io> java-introcs Anonymize SSN < patients_small.csv |  java-introcs Anonymize BIRTHDATE
    FIRST,LAST,RACE,GENDER,HEALTHCARE_EXPENSES
    Doyle,Johns,white,M,1008922.16
    Timmy,Pfannerstil,white,M,253391.99
    Monica,Nikolaus,white,F,136549.01
    Michele,Murphy,hawaiian,F,2015686.52
    Ellen,Parker,black,F,1670546.89
    Cruz,Okuneva,white,F,1564013.09
    Mika,Daugherty,white,F,2002214.73
    Silvia,Pichardo,other,F,34871.86
    Olin,Shanahan,white,M,963576.68
    


  3. Transposing melodies.  Is it possible to create a complex piece of music using only a single simple melody? This is the basic idea behind a fugue, a compositional technique made famous by Johann Sebastian Bach. In a fugue, we hear (essentially) the same melody superimposed on itself, but in different tones and offsets.

    We'll learn how to play a fugue in the next part of the assignment. As a first step, let's start by transposing a melody by a given number of semitones. For example, here is Twinkle Twinkle Little Star starting at middle C (60):

    Transposing MIDI notes up by 7 semitones yields the same melody transposed by a fifth, starting at middle G (67):

    Transposing by 12 semitones yields the same melody an octave above:

    Write a program Transpose.java that takes 3 command-line arguments (an integer offset, a string that names an instrument and an integer tempo) and plays the melody described in standard input transposed by the provided offset. You will use StdMidi to play; recall that StdMidi.playNote() takes in an int and a double argument, corresponding to a MIDI note and its duration. See StdMidi for how to select an instrument and set the tempo.

    Input format. You will read (from StdIn) a melody described in the following sequence:

    The diagram below shows this structure in the file twinkle.txt.

    Your program must be able to play melodies with at least 3 instruments: acoustic grand piano (corresponding to the input string piano), violin (corresponding to violin) and one more instrument of your choice. Here are some sample executions:

    ~/Desktop/io> javac-introcs Transpose.java
    
    ~/Desktop/io> java-introcs Transpose 0 piano 100 < twinkle.txt 
    
    
    
    ~/Desktop/io> java-introcs Transpose -24 piano 100 < twinkle.txt 
    
    
    
    ~/Desktop/io> java-introcs Transpose 12 violin 180 < twinkle.txt 
    
    
    

    Optional: Submit a text file named MyMelody.txt with any melody of your choice in the file format described above. If you authorize, we'll play it in lecture or precept!

  4. Optional bonus: Fugues.  Equipped with the ability of transposing melodies, we are now ready to play a basic fugue. A fugue consists of a number of voices drawn from a small number of overlapping melodies (shifted in frequency as well as time). We will consider 3-voice fugues with two melodies: the subject and the answer.

    Write a program Fugue.java that takes 2 command-line arguments (a string and an integer, corresponding to the name of an instrument and a tempo) and plays a 3-voice fugue whose subject and answer are given in standard input. The first voice plays the subject, the second plays the answer (transposed and delayed), and the third plays the subject again (also transposed and delayed, by possibly different amounts).

    Input format. A 3-voice fugue is described by the following sequence:

    The diagram below shows this structure in the file twinkle-octaves.txt.

    Your program must play the entirety of the first voice and only the notes of the second and third that are played within the duration of the first. (In the example above, this corresponds to every note of every voice because the delays are all 0.) The first voice is neither transposed nor delayed, and recall that StdMidi.playNote() can only play one note at a time; in order to play overlapping melodies, you must use StdMidi.noteOn(), StdMidi.pause() and StdMidi.noteOff().

    Here are some sample executions:

    ~/Desktop/io> javac-introcs Fugue.java
    
    ~/Desktop/io> java-introcs Fugue piano 100 < twinkle-octaves.txt 
    
    
    
    ~/Desktop/io> java-introcs Fugue piano 120 < 846.txt 
    
    
    
    ~/Desktop/io> java-introcs Fugue violin 100 < contrapunctus.txt 
    
    
    

    Optional: Submit a text file named MyFugue.txt with any pair of melodies of your choice, describing a fugue in the file format described above. If you authorize, we'll play it in lecture or precept!


    Submission. Submit the Java files Display.java, Anonymize.java and Transpose.java; optionally, submit Fugue.java, MyMelody.txt and MyFugue.txt. Also submit a readme.txt file and answer the questions. Do not use Java features that have not yet been introduced in the course (such as functions).


    Grading.
    File Points
    Display.java 10
    Anonymize.java 10
    Transpose.java 10
    readme.txt 10
    Total 40

    This assignment was developed by Sebastian Caldas and Marcel Dall'Agnol.
    Copyright © 2024.
    Credits.