CIT 591 Scala Warm-Up Exercises
Fall 2013, David Matuszek

  1. [Get started] Log in. Open a command-line window and enter scala to start the Scala REPL. Use the REPL to compute 2+2.

  2. [Reading and printing] Use readLine to ask the user for an integer, and save it in a val named number. Then use println to tell the user the value of half that number.
  3. [Use an if statement] Compare the number just entered to 100, then print "That's a big number" if the number is greater than 100, or "That's a small number" if the number is less than 100. (Remember that your test must be put inside parentheses.)

  4. [Use a while loop] Write a program to repeatedly do the following: (1) Ask for a number, and (2) print out a message that says what number you entered, and the square of that number (for example, 5 squared is 25). Quit when the number 0 is entered. (Remember that the readLine and println expressions must be inside curly braces, {}).

  5. [Use a for loop] Write a program to print out the numbers 1 through 25 and, for each number, print its square and its cube on the same line. (You can use + to combine strings and numbers into a single string.)

  6. [Write a program] In a text editor, write a program to compute and print the result of adding 2 and 2. Save this program (File -> Save or ctrl-S) on a file named test.scala, then run it by entering a :load command into the REPL. The :load command looks like this:
    :load complete_path_to_the_Scala_file

  7. [Edit a program] Edit your program to compute and print the result of 13 squared. (You can use 13 * 13, or 13 ** 2 to compute this.) Run the program by loading it in again.

  8. [Write a function] In your program, write a function named isEven that, given a single Int as a parameter, returns true if the remainder when divided by 2 is zero (the operator that gives you the remainder is %), and false otherwise. Reload the program and try calling the function with various numbers.