Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Tiffany Chiu
- October 4, 2018
- 3.2: Functions */
- import java.util.Scanner;
- import java.text.DecimalFormat;
- class Main {
- public static void main(String[] args) {
- // new Scanner
- Scanner sc = new Scanner (System.in);
- Scanner ss = new Scanner (System.in);
- // new decimal format
- DecimalFormat d = new DecimalFormat (".##");
- /* Question 1
- noteToFreq: this program allows the user to input a note, and outputs the frequency. */
- System.out.println ("-------------------- \n \nQuestion 1 \n ");
- // Declare variables
- String note;
- double frequency;
- // User inputs a note
- System.out.print ("Enter a note: ");
- note = ss.nextLine ();
- note = note.toUpperCase();
- // The frequency is output
- frequency = noteToFreq(note); // calls for the noteToFreq function
- if (frequency > 1) {
- System.out.println (note + " has a frequency of " + d.format(frequency) + " Hz.");
- }
- else {
- System.out.println ("Invalid input.");
- }
- /*Question 2
- freqToNote: this program allows the user to input a frequency, and outputs the note. */
- System.out.println ("-------------------- \n \nQuestion 2 \n ");
- // User inputs a frequency
- System.out.print ("Enter a frequency (Hz): ");
- frequency = sc.nextDouble();
- // The note is output
- note = freqToNote(frequency); // calls for the freqToNote function
- if (frequency >= 260.63 && frequency <= 494.88) {
- System.out.println (note);
- }
- /* Question 3
- passChecker: this program takes a username and password from the user and outputs whether it was a
- "successful login" (i.e. if the password matched the corresponding username). */
- System.out.println ("-------------------- \n \nQuestion 3 \n ");
- // Declare variables and arrays
- String user, pass;
- String userList[] = new String[] {"Tiffany", "325318301", "@tiffanyc_"};
- String passList[] = new String[] {"Chiu", "tdsb", "abcd123"};
- // User inputs username and password
- System.out.print ("Username: ");
- user = ss.nextLine();
- System.out.print ("Password: ");
- pass = ss.nextLine();
- // Output
- System.out.println (passChecker (user, pass, userList, passList));
- /* Question 4
- palindromeChecker(): this program checks whether a string is a palindrome. */
- System.out.println ("-------------------- \n \nQuestion 4 \n ");
- // Declare variables
- boolean palindrome;
- String phrase;
- // User input
- System.out.print ("Enter a word or phrase: ");
- phrase = ss.nextLine ();
- palindrome = palindromeChecker(phrase);
- System.out.println (palindrome);
- }
- // Question 1 noteToFreq
- public static double noteToFreq(String note) { // This function reads a note and outputs its frequency
- // Declare variables
- double noteA, noteB, noteC, noteD, noteE, noteF, noteG, freq;
- int octave;
- /* Initialize; Assign frequency values for notes A - G in the first octave, because frequencies in higher octaves are
- power functions of the lowest frequency of each note. */
- noteC = 16.352;
- noteD = 18.354;
- noteE = 20.602;
- noteF = 21.827;
- noteG = 24.50;
- noteA = 27.50;
- noteB = 30.8675;
- if (note.length()==2) { // does not run the following code unless the length of the string is 2
- note = note.toUpperCase ();
- freq = 0;
- octave = (Integer.parseInt((note.substring(1)))); // Separates the octave value from the note as an integer value
- if (octave<=8 && octave>=0) { // does not run the following code if the value is not between 0 and 8
- 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
- freq = ((noteC)*Math.pow(2,octave));
- }
- 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
- freq = ((noteD)*Math.pow(2,octave));
- }
- 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
- freq = ((noteE)*Math.pow(2,octave));
- }
- 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
- freq = ((noteF)*Math.pow(2,octave));
- }
- 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
- freq = ((noteG)*Math.pow(2,octave));
- }
- 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
- freq = ((noteA)*Math.pow(2,octave));
- }
- 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
- freq = ((noteB)*Math.pow(2,octave));
- }
- else {
- System.out.println ("Invalid note"); // If all statements are false, the note is invalid
- } }
- return freq;
- }
- return -1;
- }
- // Question 2 freqToNote
- public static String freqToNote(double freq) { // This function reads a frequency and outputs the note (with a tolerance of +/- 1.0 Hz)
- // Declare variables
- double noteA, noteB, noteC, noteD, noteE, noteF, noteG;
- String note;
- // Initialize
- noteC = 261.63;
- noteD = 293.66;
- noteE = 329.63;
- noteF = 349.23;
- noteG = 392.00;
- noteA = 440.00;
- noteB = 493.88;
- note = "";
- if (freq <= (noteC + 1.0) && freq >= (noteC - 1.0)) { // If the frequency is within +/- 1 Hz of that of C4, the note is C4
- note = "C4";
- }
- 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
- note = "D4";
- }
- 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
- note = "E4";
- }
- 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
- note = "F4";
- }
- 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
- note = "G4";
- }
- 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
- note = "A4";
- }
- 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
- note = "B4";
- }
- else { // If all statements are false, the note is not within the 5th octave
- System.out.println ("That frequency does not correspond to a known note.");
- }
- return note;
- }
- // Question 3 passChecker
- public static boolean passChecker(String user, String pass, String userList[], String passList[]) {
- // Declare variables
- boolean login = true;
- if (pass.equals ("")) {
- login = true;
- }
- else {
- for (int i = 0; i < userList.length; i++) {
- if (user.equals(userList[i]) && pass.equals(passList[i])) {
- login = true;
- }
- else {
- login = false;
- }
- }
- }
- return login;
- }
- // Question 4 palindromeChecker
- public static boolean palindromeChecker(String phrase) {
- // Declare variables
- String phraseBackwards;
- boolean palindrome;
- int phraseLength;
- char [] phraseLetters;
- phraseBackwards = "";
- phrase = phrase.toLowerCase();
- phrase = phrase.replaceAll ("\\W", "");
- System.out.print (phrase);
- phraseLetters = phrase.toCharArray ();
- phraseLength = phrase.length() - 1;
- for (int u = phraseLength; u >= 0; u--)
- {
- phraseBackwards += (phrase.charAt (u));
- }
- if (phraseBackwards.equals(phrase)) {
- palindrome = true;
- }
- else {
- palindrome = false;
- }
- return palindrome;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement