Skip to content

Where is the car?

  • Project
  • 2 hints
  • 6 of 6 in this chapter

Your first project

This is a project section. These operate a bit differently than lessons or quizzes, as they allow you to use what you have learned. We will give a few rules for you to follow, and you will program accordingly. Don't worry, there's no penalty or problem if you mess up. It's all about learning. At the end of this project, you can see the answer equivalent.

What will I do?

This project will involve some simple Physics.

Kinematics

A physics concept that is commonly applied to the real world is Kinematics.

This is concept is mainly based on one equation:

Text
current_position = initial_position + speed * time + acceleration/2 * time<sup>2</sup>
x = x 0 + v 0 t + a t 2 / 2 x = x_0 + v_0t + at^2/2

The Challenge

Your challenge is to write a program that can solve for the current position of the car. We have given you all the other variables.

  • Determine how far (the current position) away a car is 5 seconds after it runs a red light
  • The car starts at a position of 0 (the red light) with a speed of 20 meters per second
  • The car accelerates at 2 meters per second squared

Hints

Hint 1What is the current position?

The current position is what you are solving for. :)

Hint 2How do I square a number?

Squaring a number is multiplying it by itself. For example, 3<sup>2</sup> is 9.

Solution

Swift
var initial_position = 0
var initial_speed = 20
var acceleration = 2
var time = 5

var current_positon = (initial_position) + (initial_speed*time) + ((acceleration*time*time)/2)

print("The car is currently \(current_positon) meters away from the traffic lights!")

Give the project a try first.