Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.35 KB | None | 0 0
  1. /*  Tiffany Chiu
  2.     October 4, 2018
  3.     3.2: Functions */
  4. import java.util.Scanner;
  5. import java.text.DecimalFormat;
  6. class Main {
  7.   public static void main(String[] args) {
  8.    
  9.     // new Scanner
  10.     Scanner sc = new Scanner (System.in);
  11.     Scanner ss = new Scanner (System.in);
  12.    
  13.     // new decimal format
  14.     DecimalFormat d = new DecimalFormat (".##");
  15.    
  16. /* Question 1
  17. noteToFreq: this program allows the user to input a note, and outputs the frequency. */
  18.  
  19.  
  20.    
  21.     System.out.println ("-------------------- \n \nQuestion 1 \n ");
  22.    
  23.       // Declare variables
  24.     String note;
  25.     double frequency;
  26.      
  27.       // User inputs a note
  28.     System.out.print ("Enter a note: ");
  29.     note = ss.nextLine ();
  30.     note = note.toUpperCase();
  31.      
  32.       // The frequency is output
  33.     frequency = noteToFreq(note);   // calls for the noteToFreq function
  34.    
  35.     if (frequency > 1) {
  36.     System.out.println (note + " has a frequency of " + d.format(frequency) + " Hz.");
  37.     }
  38.     else {
  39.       System.out.println ("Invalid input.");
  40.     }
  41.    
  42. /*Question 2
  43. freqToNote: this program allows the user to input a frequency, and outputs the note. */
  44.    
  45.     System.out.println ("-------------------- \n \nQuestion 2 \n ");
  46.      
  47.       // User inputs a frequency
  48.     System.out.print ("Enter a frequency (Hz): ");
  49.     frequency = sc.nextDouble();
  50.      
  51.       // The note is output
  52.     note = freqToNote(frequency);   // calls for the freqToNote function
  53.      
  54.     if (frequency >= 260.63 && frequency <= 494.88) {
  55.       System.out.println (note);
  56.     }
  57.  
  58. /* Question 3
  59. passChecker: this program takes a username and password from the user and outputs whether it was a
  60. "successful login" (i.e. if the password matched the corresponding username). */
  61.  
  62.   System.out.println ("-------------------- \n \nQuestion 3 \n ");
  63.  
  64.     // Declare variables and arrays
  65.   String user, pass;
  66.   String userList[] = new String[] {"Tiffany", "325318301", "@tiffanyc_"};
  67.   String passList[] = new String[] {"Chiu", "tdsb", "abcd123"};
  68.  
  69.     // User inputs username and password
  70.   System.out.print ("Username: ");
  71.   user = ss.nextLine();
  72.   System.out.print ("Password: ");
  73.   pass = ss.nextLine();
  74.  
  75.     // Output
  76.   System.out.println (passChecker (user, pass, userList, passList));
  77.    
  78. /* Question 4
  79. palindromeChecker(): this program checks whether a string is a palindrome. */
  80.  
  81.   System.out.println ("-------------------- \n \nQuestion 4 \n ");
  82.  
  83.     // Declare variables
  84.   boolean palindrome;
  85.   String phrase;
  86.  
  87.     // User input
  88.   System.out.print ("Enter a word or phrase: ");
  89.   phrase = ss.nextLine ();
  90.  
  91.   palindrome = palindromeChecker(phrase);
  92.  
  93.   System.out.println (palindrome);
  94.  
  95.  
  96.  
  97.   }
  98.  
  99.  
  100.   // Question 1 noteToFreq
  101.   public static double noteToFreq(String note) {  // This function reads a note and outputs its frequency
  102.    
  103.     // Declare variables
  104.     double noteA, noteB, noteC, noteD, noteE, noteF, noteG, freq;
  105.     int octave;
  106.    
  107.     /* Initialize; Assign frequency values for notes A - G in the first octave, because frequencies in higher octaves are
  108.     power functions of the lowest frequency of each note. */
  109.     noteC = 16.352;
  110.     noteD = 18.354;
  111.     noteE = 20.602;
  112.     noteF = 21.827;
  113.     noteG = 24.50;
  114.     noteA = 27.50;
  115.     noteB = 30.8675;
  116.    
  117.     if (note.length()==2) {   // does not run the following code unless the length of the string is 2
  118.      
  119.     note = note.toUpperCase ();
  120.     freq = 0;
  121.    
  122.     octave = (Integer.parseInt((note.substring(1))));     // Separates the octave value from the note as an integer value
  123.      
  124.       if (octave<=8 && octave>=0) {     // does not run the following code if the value is not between 0 and 8
  125.      
  126.       if (note.substring (0,1).equals ("C")) {      // If the note is C (regardless of octave), the frequency is equal to the frequency of C0 times 2 to the power of the octave
  127.         freq = ((noteC)*Math.pow(2,octave));
  128.       }
  129.       else if (note.substring (0,1).equals ("D")) {   // If the note is D, the frequency is the frequency of D0 times 2 to the power of the octave
  130.         freq = ((noteD)*Math.pow(2,octave));
  131.       }
  132.       else if (note.substring (0,1).equals ("E")) {   // If the note is E, the frequency is the frequency of E0 times 2 to the power of the octave
  133.         freq = ((noteE)*Math.pow(2,octave));
  134.       }
  135.       else if (note.substring (0,1).equals ("F")) {   // If the note is F, the frequency is the frequency of F0 times 2 to the power of the octave
  136.         freq = ((noteF)*Math.pow(2,octave));
  137.       }
  138.       else if (note.substring (0,1).equals ("G")) {   // If the note is G, the frequency is the frequency of G0 times 2 to the power of the octave
  139.         freq = ((noteG)*Math.pow(2,octave));
  140.       }
  141.       else if (note.substring (0,1).equals ("A")) {   // If the note is A, the frequency is the frequency of A0 times 2 to the power of the octave
  142.         freq = ((noteA)*Math.pow(2,octave));
  143.       }
  144.       else if (note.substring (0,1).equals ("B")) {   // If the note is B, the frequency is the frequency of B0 times 2 to the power of the octave
  145.         freq = ((noteB)*Math.pow(2,octave));
  146.       }
  147.       else {
  148.         System.out.println ("Invalid note");    // If all statements are false, the note is invalid
  149.       } }
  150.    
  151.       return freq;
  152.     }
  153.     return -1;
  154.   }
  155.    
  156.   // Question 2 freqToNote
  157.   public static String freqToNote(double freq) {  // This function reads a frequency and outputs the note (with a tolerance of +/- 1.0 Hz)
  158.    
  159.     // Declare variables
  160.     double noteA, noteB, noteC, noteD, noteE, noteF, noteG;
  161.     String note;
  162.    
  163.     // Initialize
  164.     noteC = 261.63;
  165.     noteD = 293.66;
  166.     noteE = 329.63;
  167.     noteF = 349.23;
  168.     noteG = 392.00;
  169.     noteA = 440.00;
  170.     noteB = 493.88;
  171.    
  172.     note = "";
  173.    
  174.     if (freq <= (noteC + 1.0) && freq >= (noteC - 1.0)) {   // If the frequency is within +/- 1 Hz of that of C4, the note is C4
  175.       note = "C4";
  176.     }
  177.     else if (freq <= (noteD + 1.0) && freq >= (noteD - 1.0)) {    // If the frequency is within +/- 1 Hz of that of D4, the note is D4
  178.       note = "D4";
  179.     }
  180.     else if (freq <= (noteE + 1.0) && freq >= (noteE - 1.0)) {    // If the frequency is within +/- 1 Hz of that of E4, the note is E4
  181.       note = "E4";
  182.     }
  183.     else if (freq <= (noteF + 1.0) && freq >= (noteF - 1.0)) {    // If the frequency is within +/- 1 Hz of that of F4, the note is F4
  184.       note = "F4";
  185.     }
  186.     else if (freq <= (noteG + 1.0) && freq >= (noteG - 1.0)) {    // If the frequency is within +/- 1 Hz of that of G4, the note is G4
  187.       note = "G4";
  188.     }
  189.     else if (freq <= (noteA + 1.0) && freq >= (noteA - 1.0)) {    // If the frequency is within +/- 1 Hz of that of A4, the note is A4
  190.       note = "A4";
  191.     }
  192.     else if (freq <= (noteB + 1.0) && freq >= (noteB - 1.0)) {    // If the frequency is within +/- 1 Hz of that of B4, the note is B4
  193.       note = "B4";
  194.     }
  195.     else {          // If all statements are false, the note is not within the 5th octave
  196.       System.out.println ("That frequency does not correspond to a known note.");  
  197.     }
  198.    
  199.     return note;
  200.    
  201.   }
  202.  
  203.   // Question 3 passChecker
  204.   public static boolean passChecker(String user, String pass, String userList[], String passList[]) {
  205.    
  206.     // Declare variables
  207.     boolean login = true;
  208.    
  209.     if (pass.equals ("")) {
  210.       login = true;
  211.     }
  212.     else {
  213.    
  214.       for (int i = 0; i < userList.length; i++) {
  215.           if (user.equals(userList[i]) && pass.equals(passList[i])) {
  216.             login = true;
  217.           }
  218.           else {
  219.             login = false;
  220.           }
  221.       }
  222.     }
  223.     return login;
  224.   }
  225.  
  226.   // Question 4 palindromeChecker
  227.   public static boolean palindromeChecker(String phrase) {
  228.    
  229.     // Declare variables
  230.     String phraseBackwards;
  231.     boolean palindrome;
  232.     int phraseLength;
  233.     char [] phraseLetters;
  234.    
  235.     phraseBackwards = "";
  236.     phrase = phrase.toLowerCase();
  237.     phrase = phrase.replaceAll ("\\W", "");
  238.     System.out.print (phrase);
  239.     phraseLetters = phrase.toCharArray ();
  240.     phraseLength = phrase.length() - 1;
  241.  
  242.   for (int u = phraseLength; u >= 0; u--)
  243.   {
  244.     phraseBackwards += (phrase.charAt (u));
  245.   }
  246.    
  247.    if (phraseBackwards.equals(phrase)) {
  248.      palindrome = true;
  249.    }
  250.    else {
  251.      palindrome = false;
  252.    }
  253.     return palindrome;
  254.   }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement