Please provide the working source code in Swift macOS language. See the followin
ID: 3880714 • Letter: P
Question
Please provide the working source code in Swift macOS language. See the following instructions. Thank You
Description: Create and run an OSX Command Line application using Swift. Application specifics 1. In a separate file, define a class named Automobile 1. Define the following properties 1. Name:_make, Data type: string, Accessibility: private 2. Name:_model, Data type: string, Accessibility: private 3. Name:_numberOfDoors, Data type: integer, Accessibility: private 4. Name:_speed, Data type: integer, Accessibility: private 2. Define the following methods 1. An 'init method, with arguments for each property. Initialize all properties to their passed-in value 2. A class-level method named 'create', with arguments for each property. it's purpose is to create and return an instance of an Automobile object, using the passed in initial 3. Get and set instance methods for each private property, except the speed property, 4. A method named 'increaseSpeed', with one argument named 'speedChange' of 5. A method named decreaseSpeed', with one argument named 'speedChange' of type 6. A method named 'description' that will return the following string property values which should only have a get method integer type. Make sure the resulting speed is not outside the range of 0 to 150 integer. Make sure the resulting speed is not outside the range of 0 to 150 Make: , Model: won!!". Or, in the unusual event there is a tie, output "There was a tie!". The winner is determined by the greater speed 1. Example: Honda Accord won!! 3. 4. 2. At the global level, call the 'main' global-scope function The output should look like this, with speed values probably different when your program runs 3. Make: Maserati, Model: GranTurismo, NumDoors: 2, Speed: 67 Make: Honda, Model: Accord, NumDoors: 4, Speed: 128 Make: Tesla, Model: S 90, NumDoors: 2, Speed: 35 Honda Accord won!! 4. Build and run the app, and verify the output is correct. Random number generation code to include func randomValueBetween(min:UInt32, max:Ulnt32) ->UInt32 var randomValue:UInt32 = min + arc4random-uniform(UInt32(max-min + 1)) return random ValueExplanation / Answer
main.swift
-------------------------------------
import Foundation
func main() {
// create 3 Automobile objects
let a1 = Automobile.create(_make:"Maserati", _model:"GranTurismo", _numberOfDoors:2, _speed:0)
let a2 = Automobile.create(_make:"Honda", _model:"Accord", _numberOfDoors:4, _speed:0)
let a3 = Automobile.create(_make:"Tesla", _model:"S 90", _numberOfDoors:2, _speed:0)
// randomValueBetween returns a random int between min, max
func randomValueBetween(min:UInt32, max:UInt32) -> UInt32 {
let randomValue:UInt32 = min + arc4random_uniform(UInt32(max - min + 1))
return randomValue
}
// define a loop that iterates 10 times, calling the increaseSpeed method on each Automobile object
for _ in 1...10 {
a1.increaseSpeed(speedChange: Int(randomValueBetween(min: 0, max: 16)))
a3.increaseSpeed(speedChange: Int(randomValueBetween(min: 0, max: 16)))
a2.increaseSpeed(speedChange: Int(randomValueBetween(min: 0, max: 16)))
}
// call each object’s description method
print(a1.description())
print(a2.description())
print(a3.description())
// output results of race
let winner = max(a1.speed, a2.speed, a3.speed)
let speedSet:Set = [a1.speed, a2.speed, a3.speed]
if speedSet.count < 3 && !speedSet.contains(winner) {
print ("There was a tie!")
exit(0)
}
if a1.speed == winner {
print ("(a1.make) (a1.model) won!!")
}
if a2.speed == winner {
print ("(a2.make) (a2.model) won!!")
}
if a3.speed == winner {
print ("(a3.make) (a3.model) won!!")
}
}
main()
-------------------------------------------------------------------------
auto.swift
---------------------------------------
import Foundation
class Automobile {
// Properties
private var _make:String
private var _model:String
private var _numberOfDoors:Int
private var _speed:Int
// Methods
// initialize properties
init(_make:String, _model:String, _numberOfDoors:Int, _speed:Int) {
self._make = _make
self._model = _model
self._numberOfDoors = _numberOfDoors
self._speed = _speed
}
// create instance of an Automobile object
class func create(_make:String, _model:String, _numberOfDoors:Int, _speed:Int) -> Automobile {
return Automobile(_make: _make, _model: _model, _numberOfDoors: _numberOfDoors, _speed: _speed)
}
// get and set instance methods for each private property, except the speed property which only has get
var make:String {
get {return _make}
set(make) {_make = make}
}
var model:String {
get {return _model}
set(model) {_model = model}
}
var numberOfDoors:Int {
get {return _numberOfDoors}
set(numberOfDoors) {_numberOfDoors = numberOfDoors}
}
var speed:Int {
return _speed
}
// increaseSpeed, with argument ‘speedChange’
// check resulting speed is between 0, 150
func increaseSpeed(speedChange:Int) {
_speed = speed + speedChange
if speed > 150 {
_speed = 150
}
}
// decreaseSpeed, with argument ‘speedChange’
// check resulting speed is between 0, 150
func decreaseSpeed(speedChange:Int) {
_speed = speed - speedChange
if speed < 0 {
_speed = 0
}
}
// description returns the following string:
// Make: <make>, Model: <model>, NumDoors: <number-of-doors>, Speed: <speed>
func description() -> String {
return "Make: (_make), Model: (_model), NumDoors: (_numberOfDoors), Speed: (_speed)"
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.