Scala Program: Car class with information display method
Scala OOP Exercise-7 with Solution
Write a Scala program that creates a class Car with properties like make, model, and year. Implement a method to display car information.
Sample Solution:
Scala Code:
class Car(val make: String, val model: String, val year: Int) {
def displayInfo(): Unit = {
println("Car Information:")
println(s"Make: $make")
println(s"Model: $model")
println(s"Year: $year")
}
}
object CarApp {
def main(args: Array[String]): Unit = {
val car = new Car("Mercedes-Benz", "Mercedes GLS", 2023)
car.displayInfo()
}
}
Sample Output:
Car Information: Make: Mercedes-Benz Model: Mercedes GLS Year: 2023
Scala Code Editor :
Previous: Employee class with details display method.
Next: Animal class with make sound method.
What is the difficulty level of this exercise?
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/scala-oop-exercise-7.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics