Hand in format: Hand in your solution for the bonus tasks via Moodle. For non-code related bonus tasks, use a single PDF file. For code related bonus tasks, hand in a zip file (no other archive format) containing the complete and compilable project including your local repository. You can update your solution arbitrarily often until the submission deadline. We grade the submission uploaded last.
Hint: All members of an exercise group should be able to view and edit the same submission.
Attention: This exercise sheet contains 7 bonus tasks.
Submission deadline: 03.11.2023, 10:00
Discussion in the small groups: Week starting with 06.11.2023
Publishing date of a solution: 08.11.2023
The goal of this exercise sheet is to learn Scala and its development tools. First, have a look at the overview1 about the differences between Java and Scala. Then, choose an IDE of your liking and set it up to be used with Scala. We recommend to use IntelliJ2.
Using your chosen IDE, create a Scala 3 project based on the Simple Build Tool (SBT)3 You can, as an alternative, use the Scala 3 Giter8 Template4 to create the project via SBT on the command line.
Use “arithmetic” as the name for the project.
Hint: You may consider hosting your repository using the TU-GitLab.
- Initialize your newly created repository by adding a
.gitignore
file suitable for your chosen IDE and SBT to the index usinggit-add
7.
Hint: You may use the suggested .gitignore.
- Add the Scala project files to the index and commit the changes to your local repository using
git commit
8 with a suitable commit message. Prefix your commit message with “init: ”.
In this task, we create the structures to model arithmetic expressions.
- Create an
enum
9 “ArithmeticExpression” within thepackage
“arithmetic”.
You must support at least the expressions shown in the following table.
Name | Use | #Operands | Attribute type | Example |
---|---|---|---|---|
Num | Whole numbers | 1 | Int | 42 |
Minus | Negative | 1 | ArithmeticExpression | -42 |
Plus | Addition | 2 | ArithmeticExpression | 2 + 4 |
Mult | Multiplication | 2 | ArithmeticExpression | 2 * 4 |
Div | Division | 2 | ArithmeticExpression | 2 / 4 |
- Commit the changes to your local repository. Prefix your commit message with “structure: ”.
Add ScalaTest10 to your SBT project and commit the changes using the commit message “ScalaTest”.
Hint:
In ScalaTest it is possible to write tests using many different styles11. The style “FunSuite” is similar to the Unit tests known from Java.
- Create a companion object12 for your enum introduced in Task 3.
- To be able to yield a value from an arithmetic expression add a method
def evaluate(expression: ArithmeticExpression): Double = ???
to the companion object and implement it.
- Arithmetic expressions should have a way to bring them into a better readable format. Add a method
def pretty(expression: ArithmeticExpression): String = ???
to the companion object and implement it. The pretty method should bring the given arithmetic expression into a human-readable format in infix notation.
Hint: You can use string interpolation13 to construct the formatted strings.
- Add at least one test per method for
evaluate
andpretty
, to check the correct behavior of the methods.
Hint: You can compare Double
and String
values in your assertions using actual == expected
. Keep in mind, that
it is usually not advisable to compare floating point values using strict equality. You may consider using Scalactic14.
- Commit the changes to your local repository. Prefix your commit message with “functionality: ”.
Hint: Implement the methods evaluate and pretty using pattern matching15 and recursion.
In this task we want to add support for a new kind of arithmetic expression.
- Add at least the following new case to your
enum
from Task 3.
Name | Use | #Operands | Attribute type | Example |
---|---|---|---|---|
Pow | Exponentiation | 2 | ArithmeticExpression | 24 |
- Make sure that your code compiles without any warnings after adding the new case by updating all affected methods.
For simplicity, we assume exponents evaluate to whole numbers, and you can use
doubleValue.toInt
to convert a double to an integer value.
Hint: You may use the facts that xy = x·xy-1 for y > 0 and xy = (1/x)·xy+1 for y < 0 in your recursive calls using pattern guards16.
- Add at least one test per method
pretty
andevaluate
to check the behavior of the added case. - Commit the changes to your local repository. Prefix the commit message with “pow: ”.
In this task we want to extend the already implemented functionality to be applicable to multiple arithmetic expressions.
- Add the following method to the companion object introduced in Task 5:
def evaluate(expressions: List[ArithmeticExpression]): List[Double] = ???
Implement this method, s.t. it returns the evaluations of each expression from the given list.
Hint: Scala provides many built-in operations on collections1718
- Add the following method to the companion object introduced in Task 5:
def showResults(expressions: List[ArithmeticExpression]): String = ???
Implement this method, s.t. it returns the given arithmetic expressions in a human-readable format in infix notation. The result must have one “expression = value” pair per line and maintain the order of the given expressions. The result must not end with a newline (\n).
Hint: You may want to preprocess the data s.t. you get tuples of readable expressions and evaluated values, before constructing the result string.
- Add at least one test per method
evaluate
andshowResults
to check the behavior of the methods. - Commit your changes to your local repository. Prefix the commit message with “lists: ”.
Footnotes
-
https://docs.scala-lang.org/scala3/book/scala-for-java-devs.html ↩
-
https://docs.scala-lang.org/getting-started/intellij-track/getting-started-with-scala-in-intellij.html ↩
-
https://docs.scala-lang.org/scala3/book/types-adts-gadts.html#algebraic-datatypes-adts ↩
-
https://docs.scala-lang.org/overviews/scala-book/companion-objects.html ↩
-
https://docs.scala-lang.org/scala3/book/string-interpolation.html ↩
-
https://docs.scala-lang.org/tour/pattern-matching.html#pattern-guards ↩
-
https://scala-lang.org/api/3.x/scala/collection/immutable/List.html ↩
-
https://docs.scala-lang.org/scala3/book/collections-methods.html ↩