Environment Setup
Before you get started, you will need to set up everything you need to write code here at Banno.
After setting up your environment, try your hand at a simple Hello World program in Scala and push it up to a new Iteration1 internal git repo. As you read through the book for these iterations, be sure to use the REPL to evaluate as much of the code you see as possible. (Type scala into the command line.) This will give you a host of information. You can see compile errors come up in real time, tab complete to see what functions are available on any object, or use :load to pull in any code you have written to test manually. Also when you are using SBT, you can type sbt console within an SBT project to enter the REPL, loading the SBT project files into the REPL session. The REPL is a great way for you to experiment with your code. The REPL wants to be your friend! If you let it, it will get you through many issues.
Scala Basics
This covers chapters 1-3 in the O’Reilly book.
Be sure to push every project you work on up to the internal git. You should be updating your repo at every step.
Get used to working in Scala’s REPL-use it to do 5 operations (arithmetic operations and variable assignment are fine, or try using a command from the :help list) and put the results in a text file. (You can use :save) Push this up to Git with your first project.
Define an addition function- this simply needs to take in two integers and return the sum. Keep this function around, as you will be using it later.
Complete the following class for 2D Vectors. You must override the toString method to print the vector in (x,y) notation, as well as implement methods for vector addition, subtraction, scalar multiplication, and calculating vector magnitude. Click here for a quick reminder on these vector operations.
class Vec2D(x: Int, y: Int) {
def + //Your function for adding two Vec2D's
def - //Your function for subtracting two Vec2D's
def * //Your function for scalar multiplication
}
Push it up to the internal Git- this is an extremely important example to understand, so make comments on any lines you don’t fully understand to run through with your mentor.
Write a for expression that iterates over a list of strings. This must return back a list of Ints the same size as the incoming list of Strings, where each element of the new list is the length of the given string. We want a few special rules, however:
- If the string is an empty string, we want to return -1 as the length.
- If the string is “null” we want to return 0.
Try using a pattern match over your strings to accomplish this. Note that all of these requirements should be a part of the for expression (for { … } yield { … }). This can be confusing, so be sure to ask your mentor if you have questions. Create a main method to test this before pushing it up to git.
Milestone Project
The Milestone project is a project that you will be building upon on at the end of each iteration in this module. The first part of the Milestone project is described below.
Assume there exists a search engine which takes in a term and returns a list of possible uses of that term, with a brief description of each result. Create a case class for a user of this search engine. Each User contains:
- A username
- A password
- A collection of searches the user has executed in the past
A Search contains:
- The search string
- A collection of results
Finally, a Result contains:
- A name for the result
- A description for the result
To view the output, build a collection of at least five users, then iterate over that collection and print the results to the console.