CS 329E Assignment 2


Assignment 2: Command-Line Application



Project Description

For this assignment you will create a Command-Line racing game using Swift. This will provide you further experience in Swift as well as introduce object-oriented concepts that will be critical for future app development.

Basic Requirements

To create this racing game, you will:
  1. Create a class called Automobile and define the following private properties: make, model, and speed, and static property currentTrack.
  2. Create an array racetracks of race track names. You can name them whatever you'd like, but there should be at least 3.
  3. Define methods:
    1. init takes arguments for each property. Initializes all properties to their passed-in value.
    2. pickTrack is a class-level method which randomly selects a race track from racetracks and assigns it to currentTrack.
    3. get and set instance methods for each private property.
    4. increaseSpeed is an instance method that increases the vehicle's speed by a random number between 3 and 12 (mph).
    5. description is an instance method which returns a string "Make: [make], Model: [model], Speed: [speed]"
  4. In main.swift, create 3 Automobile objects of the following make and model respectively: Honda Accord, Toyota Corolla, MINI Cooper. Their speeds will be initialized to 0.
  5. Select a track using pickTrack. At the start of the race print "Race on [Racetrack] is beginning."
  6. Call increaseSpeed on each vehicle for ten "rounds" (imagine this is a race to see who can go farthest in 10 seconds).
  7. During each round, print out "[Make] [Model] is in the lead." based on which vehicle has currently covered the most distance. If there is a tie for first place, print "Two or more cars are in the lead." To do this calculation, assume each round is 1 second and that changes in speed are instantaneous between rounds. Note that you may want to create additional helper methods and/or structures to help you calculate and/or track car position based on speed. It is not sufficient to check the final speed to determine the race's winner -- your answer will not necessarily be correct based on distance traveled.
  8. During each round, also call description for each car, so we can see each car's current speed.
  9. After the race finishes, call each vehicle's description method followed by a line declaring the winner: "[Make] [Model] won the race on [Racetrack]!" or in the case of a tie, "There was a tie!".

Getting Started

To create a command-line application:
  1. Open Xcode and got to File -> New -> Project
  2. Select OSX Command Line Tool project (shown below)
  3. Set Product Name to yourid_assignment2 and Organization Identifier to cs329e. Your Team should automatically be set to your Developer ID, and the language should be set to Swift by default.
  4. main.swift is created automatically, but you will need to create a separate file for the Automobile class.

What to turn in

You will submit a zip of the project folder, yourid_assignment2, that contains the files main.swift, automobile.swift and yourid_assignment2.xcodeproj via Canvas.


Last modified: 01/21/20 by Sarah Abraham theshark@cs.utexas.edu