Due: Wednesday, January 30, 2019
Skim the introduction in Programming in Scala (PiS). Read Chapters 1 and 2 in PiS.
Get set up to do Scala programming on your machine. Follow the instructions on this page to get a command line environment based on sbt. Although we will use an integrated development environment later in the course, we will start using just a console.
You should be able to use any operating system. You can also use the department Linux server, lemuria, if you would like (the software is already installed there). If you do not have an account on lemuria but would like one, let me know.
For this part of the course you can use any text editor that you would like. On Windows Notepad++ is a good, general purpose editor (although the default installtion doesn't include knowledge of Scala syntax).
What is the result of evaluating each of the following expressions? Show the value and the type. You can use the REPL to check your answer, but try to figure out what is going to happen first (or explain what does happen if it is different than your initial expectation).
{ val temp1 = 1 + 2 val temp2 = 1 - 2 temp1 / temp2 }
{ if (10 < 20) for (i <- 0 to 4) yield 2*i + 2 else for (i <- 0 to 4) yield 2*i + 1 }
{ def helper(n: Int): Int = 2*n - 1 val temp = helper(5) + helper(6) 2*temp - 1 }
Write the following methods. For now, don't worry about error handling. Note that the ??? notation is actual Scala. If it is evaluated it throws the NotImplemented exception. Of course, you should replace the ??? with actual code.
// Returns the sum of all integers from 1 to n using the formula n(n+1)/2. def sumNa(n: Int): Int = ??? // Returns the sum of all integers from 1 to n using recursion. def sumNb(n: Int): Int = ??? // Returns the smallest number that is >= startingAt and that divides n evenly. // Use recursion. def findFirstFactorFrom(startingAt: Int, n: Int): Int = ??? // Returns the smallest number greater than 1 that divides n evenly. // Use findFirstFactorFrom. def findFirstFactor(n: Int): Int = ??? // Returns true if n is prime, false otherwise. // Use findFirstFactor. def isPrime(n: Int): Boolean = ???
Submit a file to Moodle containing your answers to the questions above. Be sure to include your name in the file.