Start learning free

LESSON 50 OF 414 · CALCULATIONS - CHALLENGES

Your First Challenge in Scala: Sum of Two Numbers

Practice Read the full explanation

In this lesson we will introduce you to how challenges work and how to make the most out of them. Let's check your first coding challenge:


Sum of Two Numbers

You are given two numbers a and b and you should return their sum.

Examples:

Input: a = 4, b = 9

Output: 13

Explanation:
The sum of a and b is 4 + 9, which is 13.

This is the challenge statement. It explains what the challenge asks us to do and provides some examples to make sure we understand the statement.

Each challenge gives us some input and asks us to write some code that successfully takes that input and uses it to produce some desired output.

In this case, the input is two numbers a and b and the output is their sum.


The class:

If you look at the code editor, you will see that everything is wrapped up inside a class:

class Solution {

}

You don't need to know anything about classes at this point, we will study them in future lessons. Just leave that code untouched.


The method:

Inside this class, we will always be provided with a method. Inside this method is where we should write our solution.

In our case the method is:

def sum(a: Int, b: Int): Int = {
    // Your solution goes here!
}

A method is just a function inside a class. Don't dwell on this, we will study methods in future lessons.


Parameters and return:

This method's parameters will always match the input described in the challenge's statement. In our case, the parameters are a and b, the two numbers we are given.

The method should return a value which represents the desired output described in the challenge's statement. In our case, it should return a + b.


Assignment:

Follow the Coding Tutorial and let's solve our first coding challenge: Sum of Two Numbers


STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · CALCULATIONS - CHALLENGES

    Your First Challenge in Scala, explained

    Problem overview

    This lesson explains how challenges work, using a tiny one: given two numbers a and b, return their sum. For a = 4 and b = 9 the answer is 13.

    Every challenge has the same shape: some input goes in, and your code must produce the matching output. The statement describes both and gives examples to confirm you understood. Here the input is two numbers and the output is one number.

    How a challenge is laid out

    The class

    The editor starts with a class:

    class Solution {
    
    }
    

    A class groups code together. You don't need to know more about classes yet; leave this wrapper as it is and write inside it.

    The method

    Inside the class there is a method, and that is where your solution goes. A method is a function that lives inside a class. In this challenge it is:

    def sum(a: Int, b: Int): Int = {
        // Your solution goes here!
    }
    

    Read the header from left to right:

    • def starts a function or method definition.
    • sum is its name. Keep the names you are given, because the lesson's tests call the method by that name.
    • (a: Int, b: Int) lists the parameters. Each one is written as a name, a colon and a type. Int is a 32-bit whole number.
    • The : Int after the parentheses is the return type: the method must produce an Int.
    • The = and the braces introduce the body, where your code goes.

    Parameters and return

    The parameters always match the input in the statement. Inside the method you use a and b as ordinary values. The method must then return a value that matches the output in the statement, here the sum.

    Step by step

    1. Find the input and the output. The input is a and b. The output is a + b.
    2. Add the two parameters with the + operator. For Int values it adds numerically.
    3. Return the result so the caller receives it. Returning hands the value back to whoever called sum.

    The solution in Scala

    class Solution {
        def sum(a: Int, b: Int): Int = {
            // Write the first step below:
            return a + b
        }
    }
    

    A few details about this code:

    • return is optional in Scala. The value of the last expression in a block is the block's value, so writing just a + b as the last line does exactly the same thing. This lesson uses return to make the idea explicit. Many Scala programmers leave it out.
    • Return types are checked. The method says it returns an Int, and a + b of two Int values is an Int, so the types fit.
    • Comments. Text after // to the end of the line is ignored. The comment in the starter code only marks where to type.
    • Parameters can't be reassigned. Method parameters in Scala are immutable, so a and b keep the values they were given.

    Edge cases and common mistakes

    • Negative numbers and zero. They need no special handling. sum(-4, 9) is 5, and sum(0, 0) is 0.
    • Overflow. An Int holds values up to 2,147,483,647 and wraps silently past it, with no error. sum(2147483647, 1) gives -2147483648. The challenge uses Int inputs of moderate size, but remember this limit when you move to bigger numbers: Long is the 64-bit type.
    • Forgetting to return. A body with no final expression doesn't give the method a value, and the compiler rejects it because the method promises an Int.
    • Printing instead of returning. A println shows a value on the console, but the tests read what the method returns.
    • Editing the class or method header. Changing a name or a type means the tests can no longer call your code.

    Testing

    • a = 4, b = 9 gives 13.
    • a = -4, b = 9 gives 5.
    • a = 0, b = 0 gives 0.
    • a = 2147483647, b = 1 gives -2147483648, because of the wraparound.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free