Lesson 11 – Strings with Finch

Required Java Concepts

String manipulation, Switch statements

Teaching Materials

Get Access

In this lesson, you will teach your Finch to sing! Your Finch will play a song represented by a string. The string will be composed of notes – acceptable characters are A, a, B, b, C, c, D, d, E, e, F, f, G, and g. An example song might be “CCCdEedefG.” You will use the Finch’s buzzer to generate the correct musical note for each letter. Uppercase characters represent long notes, and lowercase letters are short notes.

You will be working on a single program for this entire lesson, but it is broken down into exercises so that you can work on one step at a time.

Exercise 1:

Start your program by writing a method to check to whether a string is a valid song.

  • The method should accept a String parameter that represents a song.
  • Define a string named validNotes that contains all the acceptable characters.
  • Check each character of the song to see if it is in validNotes.
  • If any character in the song is not in validNotes, then the song is not valid, and the method should return false. If all the characters are valid, then the method should return true.

Exercise 2:

Write the user interaction portion of the program.

  • Ask the user to enter a song as a string.
  • Be sure to give the user instructions so that they understand what input is acceptable.
  • If the song is valid, tell the user that the Finch will now play it. Otherwise, tell the user that the song is not valid.

Exercise 3:

Write a method to play a single note. This function should take a single character as input. Since you have already checked your user input, you may assume that the character is valid.

  • If the letter is uppercase, the length of the note should be 1.2 seconds. If the letter is lowercase, the length of the note should be 0.6 seconds.
  • Determine the frequency for the letter. Use the following mapping of characters to frequencies:
    • Aa – 880
    • Bb – 988
    • Cc – 523
    • Dd – 587
    • Ee – 659
    • Ff – 698
    • Gg – 784
  • Once you have determined the duration and the frequency, play the note with the Finch’s buzzer.

Exercise 4:

Write a method to play a song, and then test your program! Remember, you should only play a song if it is valid. Here are some songs that you can use to test your program:

  • ccggaaGffeeddCggffeeDggffeeDccggaaGffeeddC
  • bagabbbaaabdDbagabbbbaabaG
  • CCCdEedefG
  • cdeccdecefGefG

Exercise 5:

When a note is played, you should also light up the Finch’s beak; each note of a different frequency should have a unique color, though notes with the same frequency but different durations (like ‘A’ and ‘a’) can have the same color.