Scala Object Oriented Programming : Exercises, Practice, Solutions
Scala Object Oriented Programming Exercises [ 15 exercises with solution ]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Scala program that creates a class called Person with properties like name, age and country. Implement methods to get and set properties.
Click me to see the sample solution
2. Write a Scala program that creates a subclass Student that extends the Person class. Add a property called grade and implement methods to get and set it.
Click me to see the sample solution
3. Write a Scala program that creates an object MathUtils with a static method factorial that calculates the factorial of a given number.
Click me to see the sample solution
4. Write a Scala program that creates an abstract class Shape with an abstract method area. Implement subclasses Rectangle and Circle that override the area method.
Click me to see the sample solution
5. Write a Scala program that creates a class BankAccount with properties accountNumber and balance. Implement methods to deposit and withdraw money from the account.
Click me to see the sample solution
6. Write a Scala program that creates a class Employee with properties like name, age, and designation. Implement a method to display employee details.
Click me to see the sample solution
7. Write a Scala program that creates a class Car with properties like make, model, and year. Implement a method to display car information.
Click me to see the sample solution
8. Write a Scala program that creates a class Animal with properties name and sound. Implement a method makeSound that prints the animal's sound.
Click me to see the sample solution
9. Write a Scala program that creates a class Triangle with properties side1, side2, and side3. Implement a method isEquilateral to check if the triangle is equilateral.
Click me to see the sample solution
10. Write a Scala program that creates a trait Resizable with a method resize that changes the size of an object. Implement a class Rectangle that extends the Resizable trait.
Click me to see the sample solution
11. Write a Scala program that creates an object MathConstants with constants for mathematical calculations such as PI and E.
Click me to see the sample solution
12. Write a Scala program that creates a class Point with properties x and y coordinates. Use a destructuring declaration to extract the coordinates.
Click me to see the sample solution
13. Write a Scala program that creates an enum class Color with values for different colors. Use the enum class to represent an object's color.
Click me to see the sample solution
14. Write a Scala program that creates a class ContactInfo with properties name, email, and address. Create a class Customer that includes a ContactInfo object.
Click me to see the sample solution
15. Write a Scala program that creates a class Shape with a nested class Dimensions to store the dimensions of a shape.
Click me to see the sample solution
More ...
Scala Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/scala-exercises/oop/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics