Assignments‎ > ‎

Scala programming, Part 1

DUE: February 1, 2012 by 10am (submission on Blackboard)

This homework asks you to write several Scala programs. The problems are of more or less increasing complexity, so you will probably be best off solving them in order.

If you aren’t sure what the definitions of concepts like factorial are, try first searching on the internet for definitions (Wikipedia is often a great resource for this sort of thing), but then don’t be shy to ask if you still aren’t sure what you need to do.

The tutorials that were assigned should be very helpful for you. You can also check out the other resources on the course links page.

Tip: Getting started with programming can be frustrating, especially the first time you do it. The problems aren't hard, but things just don't do what you want them to do or think they should do. So, don't hesitate to ask for help -- you aren't being dumb, you are just getting used to how things work, and will start to build problem solving skills as you go that will allow you to be more independent.

Tip: use the Scala REPL to try out code and see what happens immediately. This will make things go much faster as you come up with solutions.

Setting up the code

Your starting point is a set of stub programs that you can download from the course website in the file These are incomplete implementations that handle a few things for you, and have comments embedded in them to help you solve the problems.

You should download the stub zip file, unzip it, and then rename the directory from hw1-stubs to be <lastname>_<firstname>_hw1, as follows (but using your name rather than mine):

$ unzip
   creating: hw1-stubs/
  inflating: hw1-stubs/addRange.scala 
  inflating: hw1-stubs/calculator.scala 
  inflating: hw1-stubs/countries.scala 
  inflating: hw1-stubs/factorial.scala 
  inflating: hw1-stubs/variables.scala 
$ mv hw1-stubs baldridge_jason_hw1

You can now enter that directory and solve the homework problems by editing and running the appropriate files in it.

Submitting your solutions

Your submission will be a zipped up file with your implementations for each problem in it. Submit it on Blackboard as <lastname>_<firstname>

IMPORTANT: Do this by going to the parent directory (if you are in your working directory, you can do this with the command "cd ..") and running the following command:

$ zip -r baldridge_jason_hw1
  adding: baldridge_jason_hw1/ (stored 0%)
  adding: baldridge_jason_hw1/countries.scala (deflated 45%)
  adding: baldridge_jason_hw1/variables.scala (deflated 47%)
  adding: baldridge_jason_hw1/calculator.scala (deflated 52%)
  adding: baldridge_jason_hw1/factorial.scala (deflated 44%)
  adding: baldridge_jason_hw1/addRange.scala (deflated 44%)

Obviously, you should use your name instead of mine here.

NOTE: If you do not submit things in this way, I'll ask you to resubmit and take 3 points off (out of 100) from your score. (It may seem like a little thing, but having solutions submitted in this way saves me a lot of time because I use automatic scripts that check the correctness of your code, and it requires having things structured this way.)

  • Tip: Make sure your programs run as requested in each problem and that the output is consistent with the example output I give. This makes it much easier for me to check whether your solution is correct based on the output. Keep in mind that I will be providing new, novel inputs to your programs and these should be handled correctly.

  • 1. Working with variables.
    For this problem, you will write a program that takes two numbers on the command line and manipulates them in various ways. Look at the Scala stub program variables.scala. It takes two arguments from the command line and initializes two integer (Int) variables called num1 and num2 with their values. You will perform operations and comparisons on these variables for this problem.

    (a) Write code that adds the numbers together and prints the result in the format 2 + 5 = 7.

    (b) Write code that multiplies the numbers together and prints the result in the format 2 * 5 = 10.

    (c) Write an if statement that determines which of the numbers is larger and sets the variable smaller to be the smaller one and the variable larger to be the larger one. (If they are equal, let num1 be the “smaller” value.) Use pairs (Tuples with two elements) when doing this. Print out the smaller value and the larger value, e.g.: Smaller: 2 and Larger: 5.

    (d) Write one line of code that adds the numbers together, multiples that result by the smaller of the two numbers, and then prints the result in the format (2 + 5) * 2 = 14.

    Here’s an example of how your program should run:

    > scala variables.scala 2 5
    2 + 5 = 7
    2 * 5 = 10
    Smaller: 2
    Larger: 5
    (2 + 5) * 2 = 14

    2. A simple calculator.
    Write a program that takes two numbers and the name of an arithmetical operation on the command line, performs that operation on the two integers and prints the result. You need to handle the operations plus, minus, times, and div. Your program should work as follows:

    $ scala calculator.scala 2 plus 5
    2 plus 5 = 7.0
    $ scala calculator.scala 7 minus 3
    7 minus 3 = 4.0
    $ scala calculator.scala 2 times 9
    2 times 9 = 18.0
    $ scala calculator.scala 7 div 3
    7 div 3 = 2.3333333333333335

    Hint: you will need to convert the numerical arguments, which are Strings, into Ints. Also, you’ll need to convert one of the integers in a division to Double.

    Look at the Scala stub program calculator.scala, using the comments as guidance.

    3. Adding numbers in a range.
    Write a program that takes two numbers on the command line, adds all the numbers between
    them (including the two numbers themselves), and then prints the numbers which are being
    added and the result. The program should run as follows:

    > scala addRange.scala 2 4
    2 + 3 + 4 = 9

    The first number must be smaller than the second. Your program should print out a warning and
    exit if the second is smaller, as follows:

    > scala addRange.scala 4 2
    Please make sure that the first number is smaller than the second.

    Hint: use System.exit(0) to stop executing the program.

    Look at the Scala stub program, using the comments as guidance.

    4. Factorials.
    It just wouldn’t be an introductory programming homework without this one. Write a program that takes a number n on the command line and computes its factorial “n!”.

    The program should run as follows:

    $ scala factorial.scala 3
    3! = 6

    The number given must be a positive integer greater than or equal to zero. If it isn’t print out a
    warning and exit, as follows:

    $ scala factorial.scala -1
    Please supply a number greater than or equal to 0.

    Note that while any positive integer is a valid argument to factorial, anything above 20 is likely to return 0 on your machine -- this is a limitation on the size of the numbers that can be represented in 64 bit machines. It’s fine to test your program with numbers less than 12.

    Look at the Scala stub program factorial.scala, using the comments as guidance.

    5. Basic use of lists.
    For this problem, you will write a program that takes any number of country names as command line arguments and does various things with them, as described below.

    Look at the Scala stub program countries.scala. There are two lists already defined in the program, one containing the names of North American countries and the other containing the names of South American countries. Also, there are some print statements like println(“Part (a)’’) – make sure not to delete those.

    Be sure to look at the example output given below before starting on the problem – it will help explain the output that is expected from each part.

    (a) Write code that creates a sorted list called countries from the arguments on the command line and then prints them.

    Hint: use the sorted method on lists.

    (b) Write code that checks whether each country in countries is in South America, North America, or is a country we know nothing about (unknown), and prints out their status in reverse alphabetical order.

    Hint: use the contains and reverse methods on lists.

    (c) Print out the number of unknown countries, making sure to respect English agreement (e.g. was 1 country vs were 2 countries).

    Here’s an example of how your program should work on some example input:

    $ scala countries.scala Brazil Scotland USA IndiaPart (a)
    Considering: Brazil India Scotland USA

    Part (b)
    USA: North America
    Scotland: ???
    India: ???
    Brazil: South America

    Part (c)
    There were 2 unknown countries.
    Jason Baldridge,
    Jan 17, 2012, 8:18 PM