Introduction to Object-Oriented Programming with SARL
Note If you don’t know how to solve an problem, or what is the function to be used, you could search on Internet for the answer using the API of the Java programming language. Indeed, since SARL is fully compatible with the Java API, you could use all the types or functions that are defined in this Java API.
1. Exercise 1
- Write a SARL program to create a class and display the package (or namespace) of that class.
Answer
class MyClass {
}
class Solution {
static def main {
println(typeof(MyClass).^package.name)
}
}
2. Exercise 2
- Write a SARL program to create an instance of a specified class and display the package (or namespace) of the said instance.
Answer
class MyClass {
}
class Solution {
static def main {
var obj = new MyClass
println(obj.class.^package.name)
}
}
3. Exercise 3
- Write a SARL program that imports the
abs() function from the java.lang.Math class using the import statement, and finds the absolute value of -155.
Answer
import static java.lang.Math.abs
class Solution {
static def main {
println(abs(-155))
}
}
4. Exercise 4
- Define a class that is repsenting a student, with her/her name and age as attributes, and the associated getter/setter functions.
Answer
class Student {
var age : int
var name : String
def getAge : int {
this.age
}
def setAgent(age : int) {
this.age = age
}
def getName : String {
this.name
}
def setName(name : String) {
this.name = name
}
}
5. Exercise 5
- Define a class that is repsenting a student, with her/her name and age as attributes, without implementing the getter/setter functions.
- The getter/setter functions must be automatically generatd by the SARL compiler.
Answer
import org.eclipse.xtend.lib.annotations.Accessors
class Student {
@Accessors
var age : int
@Accessors
var name : String
}
6. Exercise 6
- Write a SARL program to create two empty classes,
Student and Marks. Now create some instances and check whether they are instances of the said classes or not. Also, check whether the said classes are subclasses of the built-in object class or not.
Answer
class Student {
}
class Mark {
}
class Solution {
static def main {
var a = new Student
var b = new Mark
println("a is Student class = " + (a instanceof Student))
println("a is Mark class = " + (a instanceof Mark))
println("a is Object class = " + (a instanceof Object))
println("b is Student class = " + (b instanceof Student))
println("b is Mark class = " + (b instanceof Mark))
println("b is Object class = " + (b instanceof Object))
}
}
7. Exercise 7
- Write a SARL class named Student with two instances student1, student2 and assign values to the instances’ attributes. Print all the attributes of the student1, student2 instances with their values.
Answer
import org.eclipse.xtend.lib.annotations.Accessors
class Student {
@Accessors
var age : int
@Accessors
var name : String
}
class Solution {
static def main {
var student1 = new Student
student1.setAge(15)
student1.setName("First name")
var student2 = new Student
student2.age = 18
student2.name = "Second name"
println("Student1 age = " + student1.getAge)
println("Student1 name = " + student1.getName)
println("Student2 age = " + student2.age)
println("Student2 name = " + student2.name)
}
}
8. Exercise 8
- Define a class that is representing a student, with her/her name, age and marks as attributes, and the associated getter/setter functions.
- Define a second class named
Main that is creating an instance of the student class.
- How to simulate the addition of the method
s.means(), that is computing the mean of the marks of the student s without changing the code of the student class?
Answer
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<Float> = 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
}
}
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.
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.
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.
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.