Mobile Apps Development XCODE
Create a “Car” class with the following criteria:
Properties (5)
year – initialized by initializer
make – initialized by initializer
model – initialized by initializer
color – initialized by initializer
currentSpeed – initialized to 0 by initializer
Functions (5)
init(year: Int, make: String, model: String, color: String) – Initializer function that sets year make model color based on user input and sets currentSpeed to 0.
changeSpeed(to speed: Int) -> String– This function will change the property “currentSpeed” to whatever speed parameter is provided to it. NOTE: the function will not change the speed directly it must decide if the provided speed is greater or lesser than current speed and then call either “hitTheGas” if the car needs to speed up, or “pumpTheBreaks” if the car needs to slow down. The function should return a string message like: “The car sped up by _ miles”, “The car slowed down by _ miles”, “The car didn’t change its speed” or “The car stopped”.
hitTheGas(to speed: Int) -> Int – This function accepts an integer and changes the current speed to the speed that was passed to the function ONLY of the provided speed is GREATER than the current speed. If the speed provided is less than the current speed the function should not change the current speed.
pumpTheBreaks(to speed: Int ) -> Int - This function accepts an integer and changes the current speed to the speed that was passed to the function ONLY of the provided speed is LESS than the current speed. If the speed provided is greater than the current speed the function should not change the current speed.
stopTheCar() – This function changes the currientSpeed to 0
(30pts) Create a sub class “Batmobile” that inherits from the Car class
Properties will remain unchanged
Functions
Override the init() function so that it only accepts year as input the rest of the attributes should be automatically initialized as such:
make – Bat Automotive
model – Batmobile
color – Black
currentSpeed – 100
shootMachineGun() -> String – This is a function of the Batmobile that returns a string: “Bang Bang Bang”
(20pts) Create an array named “carCollection” and add 3 cars to it, 2 regular cars and 1 Batmobile. HINT: you must create Car variables to add to the array.
Extra Credit (10pts) Create a loop that iterates over the array and prints the makes and models of each car to the console:
Example console output:
Honda Accord
Bat Automotive Batmobile
Nissan Altima