# Homework Solution: Note(String, double) :The main constructor in which the user provides the pitch name and…

Note(String, double) :The main constructor in which the user provides the pitch name and the duration. Note(int, double) :An alternate constructor using a MIDI value for the pitch. Note(String) :An alternate constructor using the (whole) string representation of a note. int toMidi(String) : Convert a pitch name into its MIDI value. (e.g. c#6 ! 73) (static) String toPitch(int) : Convert a MIDI value into its pitch name. (e.g. 41 ! f3) (static) String getPitch() : Returns the name of this note's pitch. int getMidiPitch() : Returns the MIDI value of this note's pitch. double getDuration() : Returns the duration of this note. Note stretch(double) : Return a new note with the same pitch but with the duration equal to the product of this note's duration with the argument. Note transpose(int) : Return a new note transposed higher (or lower, if the argument is neg- ative) by the given interval (number of MIDI values). String toString() : Returns a string of the form p x d" where p is the name of the pitch and d is the duration. boolean equals(Object) : Return true if the argument is a note with the same pitch and duration. int hashCode() : Returns an int associated with this Note. You should compute a unique integer eciently (without computing string hashcodes). public class Note { /* Static Constants */ public static final int DEFAULT_INTENSITY = 50; public static final int REST_PITCH = 128; // First illegal pitch, used for rests. private static final int PITCHES_PER_OCTAVE = 12; private static final String[] NOTE_LETTERS = {"c","c#","d","d#","e","f","f#","g","g#","a","a#","b"}; private static final double MIN_DURATION = 1.0/64, // One sixty-fourth MAX_DURATION = 8.0; // Eight whole notes /** Fields (Immutable) */ private final String pitch; private final int midiValue; private final double duration; /** * Instantiates a new note based on a string denoting note letter and octave. * * @param pitch the pitch (e.g. "f6") * @param duration the duration * @throws NullPointerException if pitch is null * @throws IllegalArgumentException if: *                1. The pitch parameter is malformed or out of range. *                2. The duration parameter is out of range. */ public Note(String pitch, double duration) { // TODO // Recommended: First implement toMidi(String). } /** * Instantiates a new note based on MIDI value. * * @param midiValue the MIDI value (e.g. 68) * @param duration the duration * @throws IllegalArgumentException if: *                1. The MIDI pitch parameter is out of range. *                2. The duration parameter is out of range. */ public Note(int midiValue, double duration) { // TODO // Recommended: First implement toPitch(int). } /** * Instantiates a new note from a String matching the format of Note's toString() method. * * @param note the string representation * * @throws IndexOutOfBoundsException if parameter isn't in correct format * @throws NumberFormatException if duration representation cannot be parsed as double * @throws IllegalArgumentException if the elements in the format are not permitted. */ public Note(String note) { this(note.split(" x ")[0], Double.parseDouble(note.split(" x ")[1])); } /** * Converts a pitch string to a MIDI value. * The pitch "rest" should return {@link #REST_PITCH}. * * @param pitch the pitch to convert * @throws NullPointerException if pitch is null * @throws IllegalArgumentException is the String is not a legal pitch * @return the MIDI value */ public static int toMidi(String pitch) { // TODO return -1; } /** * Converts a MIDI value to a pitch string. * The MIDI value 128 should return "rest". * * @param midiValue the MIDI value to convert * @throws IllegalArgumentException if the MIDI value is outside of legal range * @return the pitch string */ public static String toPitch(int midiValue) { // TODO return null; } /** * Gets the pitch string of this note. * * @return the pitch */ public String getPitch() { return pitch; } /** * Gets the MIDI value of this note. * * @return the MIDI value */ public int getMidiPitch() { return midiValue; } /** * Gets the duration of this note. * * @return the duration */ public double getDuration() { return duration; }   /** * Returns a new note with the same pitch, but with its duration multiplied by the parameter. * * @param factor the amount to scale by * @throws IllegalArgumentException if resultant duration is outside of valid range * @return the stretched note */ public Note stretch(double factor) { // TODO return null; } /** * Returns a (new) note with the same duration, but transposed by the given interval. * * @param interval the interval to transpose by * @throws IllegalArgumentException if note is transposed beyond valid bounds [c0, g10] * @return the transposed note */ public Note transpose(int interval) { // TODO return null; } /** * Returns a string representation of this Note. * It should follow the format found in songs/InMyLife.song, namely: *    For a Note with pitch "g#4" and duration 1.0625 -> "g#4 x 1.0625" * NB1: Identical spacing and format are important! * NB2: For a "rest" note, the same format must be used (including duration). * * @return the string representation */ @Override public String toString() { // TODO return null; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object o) { // TODO: Return equal if the argument is a Note and the midiValue and duration are equal return false; } @Override public int hashCode() { // TODO: Compute hash using pieces. (Don't take hash code of strings.) return -1; } }

public class Note { /* Static Constants */ public static final int DEFAULT_INTENSITY = 50;

Note(String, embrace) :The ocean committer in which the user provides the rock call and

the protraction.

Note(int, embrace) :An be-undetermined committer using a MIDI prize restraint the rock.

Note(String) :An be-undetermined committer using the (whole) string fidelity of a voice.

int toMidi(String) : Convert a rock call into its MIDI prize. (e.g. c#6 ! 73) (static)

String toPitch(int) : Convert a MIDI prize into its rock call. (e.g. 41 ! f3) (static)

String getPitch() : Repays the call of this voice’s rock.

int getMidiPitch() : Repays the MIDI prize of this voice’s rock.

embrace getDuration() : Repays the protraction of this voice.

Voice spread(double) : Repay a fantasticlightlight voice with the identical rock still with the protraction correspondent

to the effect of this voice’s protraction with the reasoning.

Voice reverse(int) : Repay a fantasticlightlight voice reversed loftier (or inferior, if the reasoning is neg-

ative) by the attached period (rate of MIDI prizes).

String toString() : Repays a string of the restraintm p x d” where p is the call of the rock and

d is the protraction.

boolean correspondents(Object) : Repay penny if the reasoning is a voice with the identical rock and

duration.

int hashCode() : Repays an int associated with this Voice. You should rate a unique

integer eciently (past computing string hashcodes).

public ardispose Voice {

/* Static Constants */

public static conclusive int DEFAULT_INTENSITY = 50;

public static conclusive int REST_PITCH = 128; // First unfair rock, used restraint rests.

private static conclusive int PITCHES_PER_OCTAVE = 12;

private static conclusive String[] NOTE_LETTERS = {“c”,”c#”,”d”,”d#”,”e”,”f”,”f#”,”g”,”g#”,”a”,”a#”,”b”};

private static conclusive embrace MIN_DURATION = 1.0/64, // One sixty-fourth

MAX_DURATION = 8.0; // Eight unimpaired voices

/** Fields (Immutable) */

private conclusive String rock;

private conclusive int midiValue;

private conclusive embrace protraction;

/**

* Instantiates a fantasticlightlight voice established on a string denoting voice voice and octave.

*

* @param rock the rock (e.g. “f6″)

* @param protraction the protraction

* @throws NullPointerException if rock is null

* @throws UnfairArgumentException if:

*                1. The rock parameter is malformed or quenched of dispose.

*                2. The protraction parameter is quenched of dispose.

*/

public Voice(String rock, embrace protraction) {

// TODO

// Recommended: First utensil toMidi(String).

}

/**

* Instantiates a fantasticlightlight voice established on MIDI prize.

*

* @param midiPrize the MIDI prize (e.g. 68)

* @param protraction the protraction

* @throws UnfairArgumentException if:

*                1. The MIDI rock parameter is quenched of dispose.

*                2. The protraction parameter is quenched of dispose.

*/

public Voice(int midiValue, embrace protraction) {

// TODO

// Recommended: First utensil toPitch(int).

}

/**

* Instantiates a fantasticlightlight voice from a String matching the restraintmat of Voice’s toString() regularity.

*

* @param voice the string fidelity

*

* @throws IndexOutOfBoundsException if parameter isn’t in amend restraintmat

* @throws RateFormatException if protraction fidelity canreferable be parsed as embrace

* @throws UnfairArgumentException if the elements in the restraintmat are referable clear.

*/

public Voice(String voice) {

this(note.split(” x “)[0], Embrace.parseDouble(note.split(” x “)[1]));

}

/**

* Converts a rock string to a MIDI prize.

* The rock “rest” should repay {@link #REST_PITCH}.

*

* @param rock the rock to convert

* @throws NullPointerException if rock is null

* @throws UnfairArgumentException is the String is referable a legitimate rock

* @repay the MIDI prize

*/

public static int toMidi(String rock) {

// TODO

repay -1;

}

/**

* Converts a MIDI prize to a rock string.

* The MIDI prize 128 should repay “rest”.

*

* @param midiPrize the MIDI prize to convert

* @throws UnfairArgumentException if the MIDI prize is quenchedside of legitimate dispose

* @repay the rock string

*/

public static String toPitch(int midiValue) {

// TODO

repay null;

}

/**

* Gets the rock string of this voice.

*

* @repay the rock

*/

public String getPitch() { repay rock; }

/**

* Gets the MIDI prize of this voice.

*

* @repay the MIDI prize

*/

public int getMidiPitch() { repay midiValue; }

/**

* Gets the protraction of this voice.

*

* @repay the protraction

*/

public embrace getDuration() { repay protraction; }

/**

* Repays a fantasticlightlight voice with the identical rock, still with its protraction multifarious by the parameter.

*

* @param element the correspondentity to flake by

* @throws UnfairArgumentException if unimportant protraction is quenchedside of sufficient dispose

*/

// TODO

repay null;

}

/**

* Repays a (new) voice with the identical protraction, still reversed by the attached period.

*

* @param period the period to reverse by

* @throws UnfairArgumentException if voice is reversed past sufficient limit [c0, g10]

* @repay the reversed voice

*/

public Voice reverse(int period) {

// TODO

repay null;

}

/**

* Repays a string fidelity of this Voice.

* It should thrive the restraintmat set in lays/InMyLife.song, callly:

*    Restraint a Voice with rock “g#4” and protraction 1.0625 -> “g#4 x 1.0625”

* NB1: Identical spacing and restraintmat are important!

* NB2: Restraint a “rest” voice, the identical restraintmat must be used (including protraction).

*

* @repay the string fidelity

*/

@Override

public String toString() {

// TODO

repay null;

}

* @see java.lang.Object#equals(java.lang.Object)

*/

@Override

public boolean correspondents(Object o) {

// TODO: Repay correspondent if the reasoning is a Voice and the midiPrize and protraction are correspondent

repay false;

}

@Override

public int hashCode() {

// TODO: Rate hash using pieces. (Don’t select hash jurisprudence of strings.)

repay -1;

}

}

## Expert Vindication

public ardispose Voice {

/* Static Constants */

public static conclusive int DEFAULT_INTENSITY = 50;

public static conclusive int REST_PITCH = 128; // First unfair rock, used restraint rests.

private static conclusive int PITCHES_PER_OCTAVE = 12;

private static conclusive String[] NOTE_LETTERS = {“c”,”c#”,”d”,”d#”,”e”,”f”,”f#”,”g”,”g#”,”a”,”a#”,”b”};

private static conclusive embrace MIN_DURATION = 1.0/64, // One sixty-fourth

MAX_DURATION = 8.0; // Eight unimpaired voices

/** Fields (Immutable) */

private conclusive String rock;

private conclusive int midiValue;

private conclusive embrace protraction;

/**

* Instantiates a fantasticlightlight voice established on a string denoting voice voice and octave.

*

* @param rock the rock (e.g. “f6″)

* @param protraction the protraction

* @throws NullPointerException if rock is null

* @throws UnfairArgumentException if:

*                1. The rock parameter is malformed or quenched of dispose.

*                2. The protraction parameter is quenched of dispose.

*/

public Voice(String rock, embrace protraction) {

// TODO

// Recommended: First utensil toMidi(String).

}

/**

* Instantiates a fantasticlightlight voice established on MIDI prize.

*

* @param midiPrize the MIDI prize (e.g. 68)

* @param protraction the protraction

* @throws UnfairArgumentException if:

*                1. The MIDI rock parameter is quenched of dispose.

*                2. The protraction parameter is quenched of dispose.

*/

public Voice(int midiValue, embrace protraction) {

// TODO

// Recommended: First utensil toPitch(int).

}

/**

* Instantiates a fantasticlightlight voice from a String matching the restraintmat of Voice’s toString() regularity.

*

* @param voice the string fidelity

*

* @throws IndexOutOfBoundsException if parameter isn’t in amend restraintmat

* @throws RateFormatException if protraction fidelity canreferable be parsed as embrace

* @throws UnfairArgumentException if the elements in the restraintmat are referable clear.

*/

public Voice(String voice) {

this(note.split(” x “)[0], Embrace.parseDouble(note.split(” x “)[1]));

}

/**

* Converts a rock string to a MIDI prize.

* The rock “rest” should repay {@link #REST_PITCH}.

*

* @param rock the rock to convert

* @throws NullPointerException if rock is null

* @throws UnfairArgumentException is the String is referable a legitimate rock

* @repay the MIDI prize

*/

public static int toMidi(String rock) {

// TODO

repay -1;

}

/**

* Converts a MIDI prize to a rock string.

* The MIDI prize 128 should repay “rest”.

*

* @param midiPrize the MIDI prize to convert

* @throws UnfairArgumentException if the MIDI prize is quenchedside of legitimate dispose

* @repay the rock string

*/

public static String toPitch(int midiValue) {

// TODO

repay null;

}

/**

* Gets the rock string of this voice.

*

* @repay the rock

*/

public String getPitch() { repay rock; }

/**

* Gets the MIDI prize of this voice.

*

* @repay the MIDI prize

*/

public int getMidiPitch() { repay midiValue; }

/**

* Gets the protraction of this voice.

*

* @repay the protraction

*/

public embrace getDuration() { repay protraction; }

/**

* Repays a fantasticlightlight voice with the identical rock, still with its protraction multifarious by the parameter.

*

* @param element the correspondentity to flake by

* @throws UnfairArgumentException if unimportant protraction is quenchedside of sufficient dispose

*/

// TODO

repay null;

}

/**

* Repays a (new) voice with the identical protraction, still reversed by the attached period.

*

* @param period the period to reverse by

* @throws UnfairArgumentException if voice is reversed past sufficient limit [c0, g10]

* @repay the reversed voice

*/

public Voice reverse(int period) {

// TODO

repay null;

}

/**

* Repays a string fidelity of this Voice.

* It should thrive the restraintmat set in lays/InMyLife.song, callly:

*    Restraint a Voice with rock “g#4” and protraction 1.0625 -> “g#4 x 1.0625”

* NB1: Identical spacing and restraintmat are important!

* NB2: Restraint a “rest” voice, the identical restraintmat must be used (including protraction).

*

* @repay the string fidelity

*/

@Override

public String toString() {

// TODO

repay null;

}

* @see java.lang.Object#equals(java.lang.Object)

*/

@Override

public boolean correspondents(Object o) {

// TODO: Repay correspondent if the reasoning is a Voice and the midiPrize and protraction are correspondent

repay false;

}

@Override

public int hashCode() {

// TODO: Rate hash using pieces. (Don’t select hash jurisprudence of strings.)

repay -1;

}

}