Assignments‎ > ‎

### HW1a: Basic Scala programming

DUE: August 31, 2011 by 11am (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.

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

Your submission will be a zipped up file with your implementations for each problem in it. Submit it on Blackboard as <lastname>_<firstname>_hw1a.zip (tgz is fine too).

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 I am creating 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.
##### 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:

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:

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 addRange.py, 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.

ċ
hw1-stubs.zip
(3k)
Jason Baldridge,
Aug 24, 2011, 9:34 AM