import java.util.List
import org.eclipse.xtend.lib.annotations.Accessors
class Student {
@Accessors
var age : int
@Accessors
var name : String
@Accessors
val marks : List = newArrayList
}
class Main {
static def main : void {
var s = new Student
println(s.means)
}
static def means(student : Student) : float {
var mean = 0f
if (!student.marks.empty) {
for (mark : student.marks) {
mean += mark
}
mean /= student.marks.size
}
return mean
}
}
</code></pre>
</div>
## 9. Exercise 9
* Write a SARL class to convert an integer to a Roman numeral.
Answer
class IntRomanConverter {
static val coefs = #[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]
static val symbols = #[ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ]
static def convert(num : int) : String {
var acc = num
var roman = ""
var i = 0
while (acc > 0) {
for (x : 0..<(acc / coefs.get(i))) {
roman += symbols.get(i)
acc -= coefs.get(i)
}
i++
}
return roman
}
}
class Main {
static def main {
println(IntRomanConverter::convert(1))
println(IntRomanConverter::convert(1994))
}
}
## 10. Exercise 10
* Write a SARL class to convert a Roman numeral to an integer.
Answer
class RomanIntConverter {
static val coefs = #{'I' -> 1, 'V' -> 5, 'X' -> 10, 'L' -> 50, 'C' -> 100, 'D' -> 500, 'M' -> 1000}
static def convert(roman : String) : int {
var num = 0
for (i : 0..<roman.length) {
if (i > 0 && coefs.get(roman.charAt(i)) > coefs.get(roman.charAt(i - 1))) {
num += coefs.get(roman.charAt(i)) - 2 * coefs.get(roman.charAt(i - 1))
} else {
num += coefs.get(roman.charAt(i))
}
}
return num
}
}
class Main {
static def main {
println(RomanIntConverter::convert("I"))
println(RomanIntConverter::convert("MCMXCIV"))
}
}
## 11. Exercise 11
* Write a SARL class, named `Vector` that is representing a 2D vector of values `(x, y)`. Implements the function that enables to sum two vectors, named `add()`.
* Input Vectors: `(x=124, y=45)` and `(x=-456, y=78)`
* Output Vector: `(x=-332, y=123)`
Answer
import org.eclipse.xtend.lib.annotations.Accessors
class Vector {
@Accessors
var x : double
@Accessors
var y : double
new (x : double, y : double) {
this.x = x
this.y = y
}
new {
x = 0
y = 0
}
def add(v : Vector) : Vector {
new Vector(this.x + v.x, this.y + v.y)
}
def toString : String {
"(x=" + x + ", y=" + y + ")"
}
}
class Main {
static def main {
var a = new Vector(124, 45)
var b = new Vector(-456, 78)
var c = a.add(b)
println(c)
var d = b.add(a)
println(d)
}
}
## 12. Exercise 12
* Update the class, named `Vector`, with the definition of the operation `+` that sums two vectors. Use this operator to sum two vectors instead of calling the `add()` function.
* Input Vectors: `(x=124, y=45)` and `(x=-456, y=78)`
* Output Vector: `(x=-332, y=123)`
Answer
import org.eclipse.xtend.lib.annotations.Accessors
class Vector {
@Accessors
var x : double
@Accessors
var y : double
new (x : double, y : double) {
this.x = x
this.y = y
}
new {
x = 0
y = 0
}
def operator_plus(v : Vector) : Vector {
new Vector(this.x + v.x, this.y + v.y)
}
def toString : String {
"(x=" + x + ", y=" + y + ")"
}
}
class Main {
static def main {
var a = new Vector(124, 45)
var b = new Vector(-456, 78)
var c = a + b
println(c)
var d = b + a
println(d)
}
}
## 13. Version Specification
* Specification: SARL General-purpose Agent-Oriented Programming Language ("Specification")
* Version: 0.15
* Status: Stable Release
* Release: 2025-09-11
## 14. Legal Notice
> Copyright © 2014-2025 [SARL.io, the Original Authors and Main Authors](http://www.sarl.io/about/index.html).
>
> Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0;
> you may not use this file except in compliance with CC-BY-SA-4.0.
> You may obtain a copy of [CC-BY-4.0](https://creativecommons.org/licenses/by-sa/4.0/deed.en).
>
> Examples of SARL code are licensed under the Apache License, Version 2.0;
> you may not use this file except in compliance with the Apache License.
> You may obtain a copy of the [Apache License](http://www.apache.org/licenses/LICENSE-2.0).
>
> You are free to reproduce the content of this page on copyleft websites such as Wikipedia.
Generated with the translator docs.generator 0.15.1.