Acorn is a simple language for executing sums and products of numbers.
For example, executing the app:
object TestAcorn extends App {
var
exp: Expression = Sum(Number(42), Product(Number(3.14), Number(2.71)))
println("the
value of " + exp + " = " + exp.execute)
exp = Product(Number(2),
Product(Number(3), Number(5)))
println("the value of " + exp
+ " = " + exp.execute)
}
Produces the output:
the value of (+ 42.0 (* 3.14 2.71)) = 47.85
the value of (* 2.0 (* 3.0 5.0)) = 30.0
Here's a class diagram showing the design of Acorn:

Notes:
· In UML names of abstract classes and operations are italicized.
· Sum, Number, and Product are not abstract and therefore must implement execute.
· The association from Sum to Expression indicates that Sum has two private fields of type Expression named operator1 and operator2.
· In the test harness we see that sums and products can be assigned to exp, a variable of type Expression. This is an example of polymorphism: one object can have many types.
All expressions are instances of subclasses of the abstract Expression class:
abstract class Expression {
def
execute: Double
}
There are three subclasses of Expression, each with a companion object containing an apply method:
class Sum(val operand1:
Expression, val operand2: Expression) extends
Expression {
def
execute = operand1.execute + operand2.execute
override
def toString = "(+ " +
operand1 + " " + operand2 + ")"
}
object Sum {
def
apply(operand1: Expression, operand2: Expression) =
new Sum(operand1, operand2)
}