JAVE HELP Write a Java class to represent a musical note usi
JAVE HELP:
Write a Java class to represent a musical note using a simplified version of the conventions of standard Western music.
An instance of your class should contain the following instance variables:
pitch: a value representing the frequency of the sound a note makes; this will be represented in three ways for
the purposes of methods, but just use one version for data representation. The representations are:
o a String consisting of
a letter (A through G) and
a number (0 through 8, representing the complete octaves on a piano keyboard) and
an optional # symbol representing sharp: most notes are not sharps, which is why the symbol is
optional, but your program must account for it
o An int representing the MIDI value of a note’s pitch, ranging 21 through 108 – (A0 – C8) - see
illustrations on next page)
o A double value representing the pitch’s frequency in Hz – the range is 27.5 to 4166.01 but there’s a
twist; each of 12 tones is repeated in the 8+ octaves we’ll use. The frequency of note A4 is 440.0 Hz; the
next note up (A4#) is 466.16, which is 440 x 21/12; the next is B at 493.88, which is 440 x 22/12; the pattern
continues as shown in the table below:
Note position in chromatic scale (i) frequency (first note’s frequency x 22/12
A 0 440.0 (440.0 x 20
)
A# 1 466.16
B 2 493.88
C 3 523.25
C# 4 554.37
D 5 587.33
D# 6 622.25
E 7 659.26
F 8 698.46
F# 9 739.99
G 10 783.99
G# 11 830.61
A 12 880.0 (440.0 x 21
)
• duration: a value representing the amount of time the note should sound; the standard values in Western music
include the whole note (longest duration) which can be subdivided into the half, quarter, eighth, and sixteenth
notes (duration relative to whole note should be obvious from the names).
Methods for the Note class include:
• constructors: a default constructor that sets the pitch to middle C (the middle key on the piano), the duration to
quarter and constructors that allow variations in pitch and/or duration
• basic accessor (getter) and mutator (setter) methods;
- the mutator methods setPitch and setDuration, and are responsible for ensuring that illegal values
cannot be set; two versions of the setPitch method are required, so that both representations,
described above, can be used
- the accessor methods should include all aspects of pitch: getPitchSymbol (e.g. G3), getPitchMIDIvalue
(e.g. 55) and getPitchFrequency (e.g. 196.00)
• a method that changes a note’s pitch by a specific (int) amount; for example, if the current note is middle C (C4,
or 60, or 261.63 Hz), an argument to this method should be able to raise or lower the pitch as follows:
- changePitch(3) would raise the pitch from C4 to D4#
- changePitch(-2) would lower the pitch from C4 to A3
This method should also do bounds-checking – an exception should be thrown if the argument would place a
note’s value out of range.
For this project, I will provide the tester code based on the interface you will be given; for subsequent projects, your
own tester code will be required.
public interface MusicNote
{
/**
* Sets note pitch using a String containing a letter and a number
* @param p is a valid letter-number combination
* where the letter is in the range A..G (upper or lowercase)
* and the number is in the range 1..7
* postcondition: pitch is set to specified value
* @throws: IllegalArgumentException
*/
public void setPitch(String p);
/**
* Sets note pitch using a whole number value
* @param midi is a valid MIDI value for the notes in the range
* A1 - C8
* postcondition: pitch is set to specified value
* @throws: IllegalArgumentException
*/
public void setPitch(int midi);
/**
* Sets note duration to specified value
* @param name is a char from the set {w, h, q, e, s}
* representing whole, half, quarter, eighth and sixteenth
* notes respectively
* postcondition: duration is set to specified value
* @throws: IllegalArgumentException
*/
public void setDuration(char name);
/**
* Changes pitch up or down by specified factor
* @param factor is an int value representing the
* number of pitches between the current pitch and the desired
* pitch; factor must fall within the possible range of values
* such that pitch cannot be set above C8 or below A0
* postcondition: pitch is changed by specified factor;
* if factor is positive, pitch will be higher and if negative,
* pitch will be lower
* @throws IllegalArgumentException
*/
public void changePitch(int factor);
/**
* Returns duration of note
* @return char value from the set {w, h, q, e, s}
* representing whole, half, quarter, eighth and sixteenth
* notes respectively
*/
public char getDuration ();
/**
* Returns note\'s pitch symbol
* @return String version of pitch: e.g. C4 for middle C
*/
public String getPitchSymbol();
/**
* Returns note\'s MIDI value
* @return an int version of pitch: valid MIDI value
*/
public int getPitchMIDIvalue();
/**
* Returns note\'s frequency in Hertz
* @return pitch represented by double value in Hertz
*/
public double getPitchFrequency();
}
**********Tester Class***************
// for both Scanner & Random
import org.jfugue.*;
// for sound, if desired; requires non-standard library available
// for download from the class website
/**
* Test class for MusicNote.
* Website to get ajar file to play music notes
* http://www.jfugue.org/4/download.html
*/
public class NoteTester {
public static void main (String [] args) {
MusicNote note = new Note();
// Automatic tests
runAutoTests(note);
// Interactive tests
runUserTests(note);
}
public static void runAutoTests(MusicNote n) {
System.out.println(\"******* AUTO TESTS*******\");
getNoteInfo(n);
System.out.println(\"*******Testing setPitch(String)*******\");
System.out.println(\"Changing pitch to G3\");
n.setPitch(\"G3\");
getNoteInfo(n);
// Optional: plays note as sound
playNote(n);
System.out.println(\"Attempting to change to Z1\");
try {
n.setPitch(\"Z1\");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
System.out.println(\"*******Testing setPitch(int)*******\");
System.out.println(\"Changing pitch to MIDI value 71\");
n.setPitch(71);
getNoteInfo(n);
// Optional: plays note as sound
playNote(n);
System.out.println(\"Attempting to change to -5\");
try {
n.setPitch(-5);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
getNoteInfo(n);
// Optional: plays note as sound
playNote(n);
System.out.println(\"*******Testing setDuration(char)*******\");
System.out.println(\"Changing duration to w\");
n.setDuration(\'w\');
getNoteInfo(n);
// Optional: plays note as sound
playNote(n);
System.out.println(\"Attempting to change to x\");
try {
n.setDuration(\'x\');
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
System.out.println(\"*******Testing changePitch(int)*******\");
System.out.println(\"Changing pitch by 5\");
n.changePitch(5);
getNoteInfo(n);
// Optional: plays note as sound
playNote(n);
System.out.println(\"Attempting to change by 700\");
try {
n.changePitch(700);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
System.out.println(\"*******Testing changePitch(int)*******\");
System.out.println(\"Changing pitch by -3\");
n.changePitch(-3);
getNoteInfo(n);
// Optional: plays note as sound
playNote(n);
System.out.println(\"Attempting to change by -2000\");
try {
n.changePitch(-2000);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
public static void getNoteInfo(MusicNote n) {
System.out.println(\"n=\" + n);
System.out.println(\"Pitch symbol is \" + n.getPitchSymbol());
System.out.println(\"Pitch MIDI value is \" + n.getPitchMIDIvalue());
System.out.println(\"Pitch frequency is \" + n.getPitchFrequency());
System.out.println(\"Note duration is \" + n.getDuration());
}
// Optional: uses code from jfugue library
public static void playNote(MusicNote n) {
Player piano = new Player();
String note = n.getPitchSymbol();
char dur = n.getDuration();
if (dur==\'s\')
dur = \'i\'; // jfugue uses \'i\' for sixteenth instead of \'s\'
note = note + dur;
Pattern phrase = new Pattern(note);
piano.play(phrase);
}
public static void runUserTests(MusicNote n) {
Scanner kb = new Scanner(System.in);
System.out.println(\"Current note information:\");
getNoteInfo(n);
boolean done = false;
while (!done) {
System.out.println(\"Choose an option:\");
System.out.println(\"A. Change to specific pitch\");
System.out.println(\"B. Change duration\");
System.out.println(\"C. Set pitch higher\");
System.out.println(\"D. Set pitch lower\");
System.out.println(\"E. See note information\");
System.out.println(\"F. Hear note\");
System.out.println(\"G. Quit\");
System.out.print(\"Your choice: \");
String input = kb.nextLine();
if(input.equalsIgnoreCase(\"a\"))
n = changeP(kb, n);
else if(input.equalsIgnoreCase(\"b\"))
n = changeD(kb, n);
else if(input.equalsIgnoreCase(\"c\"))
n = higher(kb, n);
else if(input.equalsIgnoreCase(\"d\"))
n = lower(kb, n);
else if(input.equalsIgnoreCase(\"e\"))
getNoteInfo(n);
else if(input.equalsIgnoreCase(\"f\"))
playNote(n);
else if(input.equalsIgnoreCase(\"g\")) {
System.out.println(\"Program ending\");
done = true;
}
else
System.out.println(input + \" not a valid option. Try again.\");
}
}
public static MusicNote changeP(Scanner kb, MusicNote n) {
String input;
MusicNote m = new Note();
m.setPitch(n.getPitchSymbol());
m.setDuration(n.getDuration());
System.out.println(\"Choose one option:\");
System.out.println(\"1) Change pitch with symbol\");
System.out.println(\"2) Change pitch with MIDI value\");
System.out.print(\"Enter 1 or 2: \");
String choice = kb.nextLine();
if(choice.equals(\"1\")) {
System.out.print(\"Enter new symbol: \");
input = kb.nextLine();
try {
m.setPitch(input);
System.out.println(\"New note information\");
getNoteInfo(m);
}
catch (IllegalArgumentException e) {
System.out.println(input + \" was not valid. No change to note.\");
}
}
else if (choice.equals(\"2\")) {
System.out.print(\"Enter new MIDI value: \");
input = kb.nextLine();
try {
int mValue = Integer.parseInt(input);
m.setPitch(mValue);
System.out.println(\"New note information\");
getNoteInfo(m);
}
catch (NumberFormatException e) {
System.out.println(input + \" is not a number. No change to note.\");
}
catch (IllegalArgumentException e) {
System.out.println(input +
\" was not a valid MIDI value. No change to note\");
}
}
return m;
}
public static MusicNote changeD(Scanner kb, MusicNote n) {
String input;
MusicNote m = new Note();
m.setPitch(n.getPitchSymbol());
m.setDuration(n.getDuration());
System.out.print(\"Enter letter for duration (w, h, q, e, s): \");
input = kb.nextLine();
try {
m.setDuration(input.charAt(0));
System.out.println(\"New note information\");
getNoteInfo(m);
} catch (IllegalArgumentException e){
System.out.println(input +
\" was not a valid duration. No change to note.\");
}
return m;
}
public static MusicNote higher(Scanner kb, MusicNote n) {
System.out.print(\"Enter positive number for pitch change: \");
String input = kb.nextLine();
Note nn = new Note (n.getPitchMIDIvalue(), n.getDuration());
try {
int change = Integer.parseInt(input);
if (change < 0)
change = Math.abs(change);
nn.changePitch(change);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println(\"Note unchanged\");
}
return nn;
}
public static MusicNote lower(Scanner kb, MusicNote n) {
System.out.print(\"Enter negative number for pitch change: \");
String input = kb.nextLine();
Note nn = new Note (n.getPitchMIDIvalue(), n.getDuration());
try {
int change = Integer.parseInt(input);
if (change > 0)
change = -change;
nn.changePitch(change);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println(\"Note unchanged\");
}
return nn;
}
}
Solution
public class Note 02 { 03 private int length; 04 private int frequency; 05 06 /** 07 Constructor 08 @param len The length of the Note. 09 @param freq The frequency of the Note. 10 */ 11 12 public Note(int len, int freq) 13 { 14 length = len; 15 frequency = freq; 16 } 17 18 /** 19 The setLength method stores a value in the 20 length field. 21 @param len The value to store in length. 22 */ 23 24 public void setLength(int len) 25 { 26 length = len; 27 } 28 29 /** 30 The setFrequency method stores a value in the 31 Frequency field. 32 @param freq The value to store in Frequency. 33 */ 34 35 public void setFrequency(int freq) 36 { 37 frequency = freq; 38 } 39 40 41 42 43 44 45 /** 46 The getLength method returns a Note 47 object\'s length. 48 @return The value in the length field. 49 */ 50 51 public double getLength() 52 { 53 54 } 55 56 /** 57 The getFrequency method returns a Note 58 object\'s Frequency . 59 @return The value in the Frequency field. 60 */ 61 62 public double getFrequency () 63 { 64 65 } 66 67 }




