Fork me on GitHub

Introduction to Object-Oriented Classes with SARL - Answers

1. Exercise 1

class MyClass {
}
class Solution {
	def main(args : String*) {
		println(typeof(MyClass).^package.name)
	}
}

2. Exercise 2

class MyClass {
}
class Solution {
	def main(args : String*) {
		var obj = new MyClass
		println(obj.class.^package.name)
	}
}

3. Exercise 3

import static java.lang.Math.abs
class Solution {
	def main(args : String*) {
		println(abs(-155))
	}
}

4. Exercise 4

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

import org.eclipse.xtend.lib.annotations.Accessors
class Student {
	@Accessors
	var age : int
	@Accessors
	var name : String
}

6. Exercise 6

class Student {
}
class Mark {
}
class Solution {
	def main(args : String*) {
		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

import org.eclipse.xtend.lib.annotations.Accessors
class Student {
	@Accessors
	var age : int
	@Accessors
	var name : String
}
class Solution {
	def main(args : String*) {
		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

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 {
	def main(args : String*) {
		var s = new Student
		println(s.means)
	}
	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

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 {
	def main(args : String*) {
		println(IntRomanConverter::convert(1))
		println(IntRomanConverter::convert(1994))
	}
}

10. Exercise 10

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 {
	def main(args : String*) {
		println(RomanIntConverter::convert("I"))
		println(RomanIntConverter::convert("MCMXCIV"))
	}
}

11. Exercise 11

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 {
	def main(args : String*) {
		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

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 {
	def main(args : String*) {
		var a = new Vector(124, 45)
		var b = new Vector(-456, 78)
		var c = a + b
		println(c)
		var d = b + a
		println(d)
	}
}

Copyright © 2014-2024 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.14.0.