Fork me on GitHub

Introduction to Object-Oriented Classes 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.

Solution

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.

Solution

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.

Solution

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.

Solution

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.

Solution

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.

Solution

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.

Solution

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?

Solution

9. Exercise 9

Write a SARL class to convert an integer to a Roman numeral.

Solution

10. Exercise 10

Write a SARL class to convert a Roman numeral to an integer.

Solution

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().

Solution

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.

Solution

Copyright © 2014-2023 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-SNAPSHOT.