Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Zehra Baig
- //CSC-236-C1
- //Lab 2-B
- import java.text.DecimalFormat;
- public class NoteDataStructureClass implements NoteADT
- {
- //Fields
- private int noteValue;
- private String noteStrValue;
- private String noteLength;
- private String keyColor;
- private double frequency;
- //Default Constructor
- public NoteDataStructureClass()
- {
- this(-9, "Quarter");
- }
- //Overloaded Constructor
- public NoteDataStructureClass(int nv, String nl)
- {
- setValue(nv);
- setLength(nl);
- }
- //Copy Constructor
- public NoteDataStructureClass(NoteDataStructureClass ndsc)
- {
- this.noteValue = ndsc.noteValue;
- this.noteLength = ndsc.noteLength;
- }
- //Sets numeric value of Note obj, calls other methods dependent on it
- public void setValue(int nv)
- {
- noteValue = nv;
- setStrValue();
- setKeyColor();
- setFrequency();
- }
- //Sets length of Note obj
- public void setLength(String nl)
- {
- noteLength = nl;
- }
- //Sets String value of Note obj
- public void setStrValue()
- {
- if(noteValue == 0 || Math.abs(noteValue) == 12 ||
- Math.abs(noteValue) == 24 || Math.abs(noteValue) == 36 ||
- noteValue == -48)
- {
- noteStrValue = "A";
- }
- else if(noteValue == 1 || noteValue == 13 ||
- noteValue == 25 || noteValue == 37 ||
- noteValue == -11 || noteValue == -23 ||
- noteValue == -35 || noteValue == -47)
- {
- noteStrValue = "A#";
- }
- else if(noteValue == 2 || noteValue == 14 ||
- noteValue == 26 || noteValue == 38 ||
- noteValue == -10 || noteValue == -22 ||
- noteValue == -34 || noteValue == -46)
- {
- noteStrValue = "B";
- }
- else if(noteValue == 3 || noteValue == 15 ||
- noteValue == 27 || noteValue == 39 ||
- noteValue == -9 || noteValue == -21 ||
- noteValue == -33 || noteValue == -45)
- {
- noteStrValue = "C";
- }
- else if(noteValue == 4 || noteValue == 16 ||
- noteValue == 28 || noteValue == -8 ||
- noteValue == -20 || noteValue == -32 ||
- noteValue == -44)
- {
- noteStrValue = "C#";
- }
- else if(noteValue == 5 || noteValue == 17 ||
- noteValue == 29 || noteValue == -7 ||
- noteValue == -19 || noteValue == -31 ||
- noteValue == -43)
- {
- noteStrValue = "D";
- }
- else if(noteValue == 6 || noteValue == 18 ||
- noteValue == 30 || noteValue == -6 ||
- noteValue == -18 || noteValue == -30 ||
- noteValue == -42)
- {
- noteStrValue = "D#";
- }
- else if(noteValue == 7 || noteValue == 19 ||
- noteValue == 31 || noteValue == -5 ||
- noteValue == -17 || noteValue == -29 ||
- noteValue == -41)
- {
- noteStrValue = "E";
- }
- else if(noteValue == 8 || noteValue == 20 ||
- noteValue == 32 || noteValue == -4 ||
- noteValue == -16 || noteValue == -28 ||
- noteValue == -40)
- {
- noteStrValue = "F";
- }
- else if(noteValue == 9 || noteValue == 21 ||
- noteValue == 33 || noteValue == -3 ||
- noteValue == -15 || noteValue == -27 ||
- noteValue == - 39)
- {
- noteStrValue = "F#";
- }
- else if(noteValue == 10 || noteValue == 22 ||
- noteValue == 34 || noteValue == -2 ||
- noteValue == -14 || noteValue == -26 ||
- noteValue == -38)
- {
- noteStrValue = "G";
- }
- else if(noteValue == 11 || noteValue == 23 ||
- noteValue == 35 || noteValue == -1 ||
- noteValue == -13 || noteValue == -25 ||
- noteValue == -36)
- {
- noteStrValue = "G#";
- }
- else
- {
- noteStrValue = "Out of scope";
- }
- }
- //Sets key color of Note obj
- public void setKeyColor()
- {
- //All sharps have a length of 2, no need to check what each character is
- if(noteStrValue.length() == 2)
- {
- keyColor = "Black key (sharp)";
- }
- else
- {
- keyColor = "White key (natural)";
- }
- }
- //Calculates frequency of note
- public void setFrequency()
- {
- frequency = (440.0 * Math.pow(2, noteValue / 12.0));
- }
- //Returns numeric value of Note obj
- public int getValue()
- {
- return noteValue;
- }
- //Returns length of Note obj
- public String getLength()
- {
- return noteLength;
- }
- //Returns String value of Note obj
- public String getStrValue()
- {
- return noteStrValue;
- }
- //Returns key color of Note obj
- public String getKeyColor()
- {
- return keyColor;
- }
- //Returns frequency of Note obj
- public double getFrequency()
- {
- return frequency;
- }
- //Overridden toString() method
- public String toString()
- {
- //Rounding frequency to 6 decimal places
- DecimalFormat freq = new DecimalFormat("#,###.######");
- return "\nNote: " + this.getStrValue()
- + "\nLength: " + this.getLength()
- + "\nNumeric Value: " + this.getValue()
- + "\n" + this.getKeyColor()
- + "\nFrequency: " + freq.format(this.getFrequency()) + " hertz\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement