Some basic tasks

There are a few basic things which is good to know when you solve exercises in this course:

Downloading exercise projects

The programming assignments for an exercise rounds are collected in one zip archive and can be downloaded from the corresponding A+ section

When you start a round, download the package, unpack the archive, and import the project in Code as described in Workflow.

Warning

Some unzipping utilities (e.g. on Windows) will create an extra folder with the same name as the zip file. As there is already a folder in the archive make sure you import the right one (see Workflow).

The files associated with an exercise is located in a directory called a<xy>-<project>, where <xy> is the number of the assignment in the round and <project> is the name of the assignment project. If you look in the subdirectory src/main and src/test you will find the Scala files associated with the assignment task and test respectively.

The tasks are marked by comments and ??? in the Scala file. Read the assignment instructions in A+ and comments in the source code for hints and descriptions of your tasks.

Note

Each exercise is will give you a problem description, your tasks, and often links to helpful material and hints. It pays to read all of this carefully.

Submitting your solutions for grading

When you are happy with your solution to an exercise check which Scala files are required for submission to A+ add them to the corresponding field on A+ and click ‘Submit’. (Tip: open a system file manager from Code by right-clicking on a file tab and selecting Open Containing Folder / Reveal / Reveal in Finder [depends on your OS], then simply drag-n-drop the file to the web page.)

A+ submission view. There's a file selector at the bottom with button "Browse...".

Select the required file for upload and click ‘Submit’.

Note

Most exercises will award some points for partial solutions, so it can be worth submitting your work even if you have not solved all tasks in an exercise before the deadline.

Warning

Note that A+ gives you a limited number of submissions (usually 10) for each exercise. So do not rely on the grader to test your solutions. This is what unit tests are for. Only submit when you want to be graded on your work.

Starting a Scala REPL

While developing your solution it is sometimes handy to be able to test things in the interactive sbt Scala REPL. In IntelliJ you can use the built-in Scala REPL, or the sbt console. If you use VS Code, you need to have sbt installed on your computer, and start a REPL through its console.

Opening a REPL in IntelliJ

There are a couple of different ways to get a Scala REPL in IntelliJ:

Alternative 1: Using the IntelliJ Scala REPL

If you only wish to test out some Scala code, you can open up a Scala REPL inside IntelliJ from the menu ToolsScala REPL… (or by pressing Ctrl+Shift+D).

Note that while you can execute Scala code in this REPL, it is not always well formatted, and this REPL is not by default aware of the exercise structure. To use code in the exercises, or any of the other sub-modules in a round, you will need to configure the REPL Run Configuration:

  1. In the REPL tool bar, click the three dots: , then select Modify Run Configuration

  2. In the dialog that appears, under Use classpath and SDK of module: select the exercise or module you would like to be able to import int he REPL. Then click OK or Apply

  3. Restart the REPL – and remember to import functions or modules that you need.

Modifying the Scala REPL run configuration and setting the classpath.

Making REPL aware of sub-module code: 1. Modify Run Configuration, 2. Select module, 3. Restart REPL.

Alternative 2: Opening REPL through the sbt shell

The built-in sbt shell can be used to execute code and run tests through commands, rather than through the IDEA user interface. It also allows you to start a Scala REPL called a console aware of the code in an exercise sub-project. You do this by a command on the form ~<module>/console, where <module> is the name of the sub-project. For example, if you wanted to start a REPL to import code from the sequences sub-project in round 1, you would write ~sequences/console, or if you wanted to play with the tinylog examples in round 3, you would use ~tinylog/console instead (followed by the appropriate import in the REPL, e.g. import tinylog.*).

Starting a REPL aware of tinylog through the sbt shell by ``~tinylog/console``.

Starting a Scala Console through the sbt shell: 1. Open the sbt shell tab, 2. execute ~<module>/console where <module> is the name of a sub-project. Fore example tinylog.

Opening a REPL in a system terminal or VS Code

  • Open a Terminal – In VS Code, go to View → Terminal. This will open up a system terminal in your project. Here you can give system commands just as from a normal terminal window.

Warning

In VS Code Do not use the popup menu from one of the project files, this will start a terminal in the specific sub directory of that file, and the sbt console may not work properly.

  • Basic console: To start a basic Scala console you can give the command sbt "~console"

  • Alternatively you can start a console aware of your assignment code: Take the name of the assignment or library you want to be able to include and put it before the console part. For example to get a console aware of the files in the assignment “pascal” (in the folder a02-pascal). Give the command sbt "~pascal/console". (For assignments you always leave out the ‘axy-‘part.)

  • This will start a Scala console using the sbt build tool, and additionally tell sbt to watch the assignment files for changes in case you edit them (this is what the ~ part of the command does). Note that this uses a separate build mechanism from Code, so the first time you run it you may have to wait a few seconds for the files to compile.

    VS-code exercise view with the terminal open and the command sbt "~ pascal/console" written to it. The quotation marks are a part of the command.

    Giving the command sbt "~ pascal/console" in a Code terminal tab.

Note

If the standard short-cuts pasting clipboard text is not working in the VS Code terminal try using Shift+Insert.

  • You should now see the familiar scala> prompt. Now you have a REPL ready to use, why not test it with some basic Scala? If you started with the name of an assignment can also import code from packages in that assignment, if needed.

    Scala REPL running in VS-code terminal.

    Interactive Scala in the sbt console.

  • Use Ctrl+d to quit the console, or recompile (if there are saved changes to your source files this will trigger a recompile, otherwise it will quit the console).

Note

  • If you happen to only give the command sbt by mistake, you will enter the sbt build system (the prompt starts with sbt). That’s not a problem, just enter rest of the command there instead to start the Scala console.

  • The console will not start if you have a compilation error in your code.

  • If the terminal gives an error such as command not found: sbt then you need to check that sbt is properly installed and available in your system path.