goals do
goal "Understand the difference between an integer and a float"
goal "Perform basic arithmetic operations"
end
step do
message "In programming, we like to distinguish between different kinds of
numbers. Here, we'll talk about integers (whole numbers) and floats
(decimals)."
irb <<-TRYME
1.class
1.0.class
TRYME
message "You'll notice that `1.class` and `1.0.class` are actually 2 different
kinds of objects."
message "With that in mind, let's see how that plays into performing
arithmetic operations."
end
step do
irb <<-TRYME
1 + 2
1.0 + 2.0
1 + 2.0
TRYME
message "Here, you'll notice that performing an operation on 2 integers will
produce an integer and operating on 2 floats will produce a float, but mixing
an integer with a float would actually produce a float."
end
step do
irb <<-TRYME
2 - 1
2.0 - 1.0
2.0 - 1
TRYME
message "Just like addition, we can perform subtraction on integers and floats interchangeably."
end
step do
irb <<-TRYME
2 * 3
2.0 * 3
2.0 * 3.0
TRYME
message "Likewise, we can perform multiplication on floats and integers."
end
step do
irb <<-TRYME
4 / 2
8 / 2
6 / 4
6 / 4.0
TRYME
message "Division can be performed similarly. Notice the difference in the results of the last two lines. When dividing two integers, ruby will perform mathematical truncation, where the expected result 1.5 becomes 1"
end
step do
irb <<-TRYME
4 / 0
TRYME
message "Uh oh! Notice how we did not get a result here. The ruby interpreter raised an error. These errors tell the programmer that they tried to do something that the interpreter does not like or cannot use. Each error has a classification and a message. In this case, we get a `ZeroDivisionError` with the message: divided by 0"
end
challenge do
message "Write a calculator that performs addition. Your program should prompt the user to enter two numbers and output the sum."
message 'Here is a sample of how the program might run. The `>` has been used below to indicate that the user should input the value proceeding it.'
source_code <<-'CONTENTS'
What is the first number?
> 2
What is the second number?
> 4
The sum is 6
CONTENTS
end
next_step "booleans"