Advertisement
Sheero

NoteDataStructureClass

May 24th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. //Zehra Baig
  2. //CSC-236-C1
  3. //Lab 2-B
  4.  
  5. import java.text.DecimalFormat;
  6.  
  7. public class NoteDataStructureClass implements NoteADT
  8. {
  9.     //Fields
  10.     private int noteValue;
  11.     private String noteStrValue;
  12.     private String noteLength;
  13.     private String keyColor;
  14.     private double frequency;
  15.  
  16.     //Default Constructor
  17.     public NoteDataStructureClass()
  18.     {
  19.         this(-9, "Quarter");
  20.     }
  21.  
  22.     //Overloaded Constructor
  23.     public NoteDataStructureClass(int nv, String nl)
  24.     {
  25.         setValue(nv);
  26.         setLength(nl);
  27.     }
  28.  
  29.     //Copy Constructor
  30.     public NoteDataStructureClass(NoteDataStructureClass ndsc)
  31.     {
  32.         this.noteValue = ndsc.noteValue;
  33.         this.noteLength = ndsc.noteLength;
  34.     }
  35.  
  36.     //Sets numeric value of Note obj, calls other methods dependent on it
  37.     public void setValue(int nv)
  38.     {
  39.         noteValue = nv;
  40.         setStrValue();
  41.         setKeyColor();
  42.         setFrequency();
  43.     }
  44.  
  45.     //Sets length of Note obj
  46.     public void setLength(String nl)
  47.     {
  48.         noteLength = nl;
  49.     }
  50.    
  51.     //Sets String value of Note obj
  52.     public void setStrValue()
  53.     {
  54.         if(noteValue == 0 || Math.abs(noteValue) == 12 ||
  55.                 Math.abs(noteValue) == 24 || Math.abs(noteValue) == 36 ||
  56.                 noteValue == -48)
  57.         {
  58.             noteStrValue = "A";
  59.         }
  60.         else if(noteValue == 1 || noteValue == 13 ||
  61.                 noteValue == 25 || noteValue == 37 ||
  62.                 noteValue == -11 || noteValue == -23 ||
  63.                 noteValue == -35 || noteValue == -47)
  64.         {
  65.             noteStrValue = "A#";
  66.         }
  67.         else if(noteValue == 2 || noteValue == 14 ||
  68.                 noteValue == 26 || noteValue == 38 ||
  69.                 noteValue == -10 || noteValue == -22 ||
  70.                 noteValue == -34 || noteValue == -46)
  71.         {
  72.             noteStrValue = "B";
  73.         }
  74.         else if(noteValue == 3 || noteValue == 15 ||
  75.                 noteValue == 27 || noteValue == 39 ||
  76.                 noteValue == -9 || noteValue == -21 ||
  77.                 noteValue == -33 || noteValue == -45)
  78.  
  79.         {
  80.             noteStrValue = "C";
  81.         }
  82.         else if(noteValue == 4 || noteValue == 16 ||
  83.                 noteValue == 28 || noteValue == -8 ||
  84.                 noteValue == -20 || noteValue == -32 ||
  85.                 noteValue == -44)
  86.         {
  87.             noteStrValue = "C#";
  88.         }
  89.         else if(noteValue == 5 || noteValue == 17 ||
  90.                 noteValue == 29 || noteValue == -7 ||
  91.                 noteValue == -19 || noteValue == -31 ||
  92.                 noteValue == -43)
  93.         {
  94.             noteStrValue = "D";
  95.         }
  96.         else if(noteValue == 6 || noteValue == 18 ||
  97.                 noteValue == 30 || noteValue == -6 ||
  98.                 noteValue == -18 || noteValue == -30 ||
  99.                 noteValue == -42)
  100.         {
  101.             noteStrValue = "D#";
  102.         }
  103.         else if(noteValue == 7 || noteValue == 19 ||
  104.                 noteValue == 31 || noteValue == -5 ||
  105.                 noteValue == -17 || noteValue == -29 ||
  106.                 noteValue == -41)
  107.         {
  108.             noteStrValue = "E";
  109.         }
  110.         else if(noteValue == 8 || noteValue == 20 ||
  111.                 noteValue == 32 || noteValue == -4 ||
  112.                 noteValue == -16 || noteValue == -28 ||
  113.                 noteValue == -40)
  114.         {
  115.             noteStrValue = "F";
  116.         }
  117.         else if(noteValue == 9 || noteValue == 21 ||
  118.                 noteValue == 33 || noteValue == -3 ||
  119.                 noteValue == -15 || noteValue == -27 ||
  120.                 noteValue == - 39)
  121.         {
  122.             noteStrValue = "F#";
  123.         }
  124.         else if(noteValue == 10 || noteValue == 22 ||
  125.                 noteValue == 34 || noteValue == -2 ||
  126.                 noteValue == -14 || noteValue == -26 ||
  127.                 noteValue == -38)
  128.         {
  129.             noteStrValue = "G";
  130.         }
  131.         else if(noteValue == 11 || noteValue == 23 ||
  132.                 noteValue == 35 || noteValue == -1 ||
  133.                 noteValue == -13 || noteValue == -25 ||
  134.                 noteValue == -36)
  135.         {
  136.             noteStrValue = "G#";
  137.         }
  138.  
  139.         else
  140.         {
  141.             noteStrValue = "Out of scope";
  142.         }
  143.     }
  144.    
  145.     //Sets key color of Note obj
  146.     public void setKeyColor()
  147.     {
  148.         //All sharps have a length of 2, no need to check what each character is
  149.         if(noteStrValue.length() == 2)
  150.         {
  151.             keyColor = "Black key (sharp)";
  152.         }
  153.         else
  154.         {
  155.             keyColor = "White key (natural)";
  156.         }
  157.     }
  158.    
  159.     //Calculates frequency of note
  160.     public void setFrequency()
  161.     {
  162.         frequency = (440.0 * Math.pow(2, noteValue / 12.0));
  163.     }
  164.  
  165.     //Returns numeric value of Note obj
  166.     public int getValue()
  167.     {
  168.         return noteValue;
  169.     }
  170.  
  171.     //Returns length of Note obj
  172.     public String getLength()
  173.     {
  174.         return noteLength;
  175.     }
  176.  
  177.     //Returns String value of Note obj
  178.     public String getStrValue()
  179.     {
  180.         return noteStrValue;
  181.     }
  182.  
  183.     //Returns key color of Note obj
  184.     public String getKeyColor()
  185.     {
  186.         return keyColor;
  187.     }
  188.  
  189.     //Returns frequency of Note obj
  190.     public double getFrequency()
  191.     {
  192.         return frequency;
  193.     }
  194.    
  195.     //Overridden toString() method
  196.     public String toString()
  197.     {
  198.         //Rounding frequency to 6 decimal places
  199.         DecimalFormat freq = new DecimalFormat("#,###.######");
  200.         return "\nNote: " + this.getStrValue()
  201.              + "\nLength: " + this.getLength()
  202.              + "\nNumeric Value: " + this.getValue()
  203.              + "\n" + this.getKeyColor()
  204.              + "\nFrequency: " + freq.format(this.getFrequency()) + " hertz\n";
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement