Exercises
Exercise 1A
Create a Car class with the fields: mark and color. Give the Car the behavoir drive() and brake() with the state speed. Create a @main function Instantiate a Auto as a red Tesla as myCar. Let myCar drive and brake and print the speed.
class Car:
var mark: String = ""
var color: String = ""
var speed = 0
def drive() = speed += 10
def brake() = speed -= 10
@main
def main(): Unit =
val myCar = new Car()
myCar.mark = "Tesla"
myCar.color = "red"
myCar.drive()
println(s"speed: ${myCar.speed}")
Exercise 1B
Create hisCar a gray Suzuki.
Let it drive three times and print the speed
Exercise 2
Create a constructor on the fields: mark and color
Change the main function to use the constructor
Exercise 3
Make the Car immutable by changing var by val
Change de main function if needed.
exercise 4
Make the speed private. Generate the toString method Change de main function .
exercise 5
Create a new class Truck, inherited from the Car class.
Give the Truck an extra field freight.
Give the Truck a constructor met de fields mark and color
In de main function create a blue Volvo with the variable name: truck1 En give truck1 a freight of 1000.
Exercise 6
Override the function drive() and brake() with a slower drive and brake speed
Exercise 7
Create RaceAuto inherited from Car. Give it a field topSpeed
Instantiate a red Ferrari as secondCar
exercise 8
Create a trait Vehicle met de functions: drive() and brake() make the Car clas implementing this trait.
Exercise 9
Create a Bicycle class from the Vehicle trait.
Instantiate a gray VanMook as myBike