Scala Program: Car class with information display method
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics