This course has already ended.
You cannot submit this assignment

You need to sign in and enrol to submit exercises.

Assignment 21

3 points

The following code plays the internationally popular melody of Frère Jacques. Try it out.

val frereJacques = "f-g-a-f-f-g-a-f-a-hb->c---<a-hb->c---cdc<hba-f->cdc<hba-f-f-c-f---f-c-f---"
play(frereJacques)

Frère Jacques is commonly performed as a repeating canon using multiple voices or instruments: each instrument begins the melody at a somewhat different time so that the performances of the instruments overlap each other. You can form a string that represents such a canon by calling the effect-free function canon. The function is defined as follows.

  • As its first parameter, it expects a string that contains a melody.

  • As its second parameter, it expects a reference to a buffer that contains integers. It interprets the integers as the numbers of instruments.

  • As its third and last parameter, it expects an integer that represents the delay that each instrument initially “waits” after the previous instrument has started playing.

  • The function returns a string that represents a canon of the given melody played by the given instruments. (The resulting string is in a format compatible with play.)

Here’s a nearly finished piece of code that uses canon:

val frereJacques = "f-g-a-f-f-g-a-f-a-hb->c---<a-hb->c---cdc<hba-f->cdc<hba-f-f-c-f---f-c-f---"
val instruments = Buffer(4, 1, 74, 19)
play(???)

What should replace the three question marks so that the code plays a canon using the melody and instruments indicated by the variables, and a delay of 8?

Experiment in the REPL. In the field below, write the expression that calls canon as requested. (Please enter just the expression that goes where the question marks are, not the entire play command or the return value of the function.) Use the variables defined above.

Feel free to play around with canon.

3 points

Let’s do one more exercise where we pass a buffer reference as a parameter.

Experiment with the function censor, also located in package o1. Call the function and pass in these two parameters:

  1. this piece of text that needs censoring: "Oh, my goodness! What was that, for Pete's sake? Jeepers, where'd you get those!? For the love of Pete!"

  2. a reference to a Buffer with three naughty words: "goodness", "Pete", and "Jeeper".

What does the censor function return, given this input?

Here’s one way to find out: Create a variable that refers to the buffer. Then call the censor function. As you call it, pass in the text as a literal and use the variable you just defined to refer to the buffer of naughty words.

The code in the preceding assignment should be of assistance.

Enter all the input strings exactly as written above. You may wish to copy and paste the returned string below so that you get it just right.

1 point

Chapter 1.4 introduced a way of embedding values in a string using an s prefix and the dollar sign. Let’s take a quick look at how that technique combines with function calls.

The following three code fragments should each print out a number and its square root. Which of the three work? Take a guess and experiment in the REPL as needed.

(We’re assuming that import scala.math.* is in force.)

val number = 123.4
val root = sqrt(number)
println(s"The square root of $number is $root.")
val number = 123.4
println(s"The square root of $number is ${sqrt(number)}.")
val number = 123.4
println(s"The square root of $number is $sqrt(number).")

Which of the following is correct?

Posting submission...

Earned points

0 / 7

Exercise info

Assignment category
Graded
Your submissions
0 / 5
Deadline
Wednesday, 14 September 2022, 18:00
Late submission deadline
Saturday, 15 April 2023, 12:00 (-100%)
Group size
1-2
Total number of submitters
995