So the last two weeks have introduced you to functions. You've seen ways to extract and compose functions using high-order functions, currying, you seen ways to write beautiful programs with just functions. But all these functions [INAUDIBLE] essentially other functions and primitive data, such as Ink or Double, or Boolean. What we're going to do in this week is add a new dimension to programming, and that's ways to compose and abstract data. And the way we're going to do that is by introducing objects and classes. Okay, so in this session we learn how functions can create and encapsulate data structures. As an example, let's consider rational numbers. We want to design a package for doing rational arithmetic. A quick reminder from high school mathematics, an rational number x over y is represented by two integers. Which are called the numerator x and the denominator y. Now in a sense, we can already do that with what we know from Scholar. We could define two functions, addRationalNumerator and addRationalDenominator. And they each get all the bits of the two rational numbers as arguments, so both functions would get the numerators and denominator of the first rationale. And the numerator and the denominator after second one and then that would implement the usual algorithms for rational arithmetic. However, it would be difficult to manage all these numerators and denominators. It's a much better choice to combine the numerator and denominator for rational number in a data structure. In Scala, we do this by defining a class. So here you see an example. We write class rational and then we have two arguments, call them x and y. And then, the rational class would have two Methods defs. And one is called numer and it's the same as the x argument. And the other's called denom and it's the same as the y argument. So what that does, what that definition Introduces is two entities. A new type that we can use henceforth, which is named Rational. And also a way to construct rationals to create elements of this type way to construct this encapsulated constructor. Scala keeps the names of types and values in two different name spaces. It always know from the context whether you mean a type or a value. So there's no conflict between the two definitions the constructor or the type of a rational. A type in a programming language is essentially a set of values. Now, the values that belong to a class type are called objects. We create an object by taking the constructor of the class, and prefixing it with the operator new. So new Rational (1, 2) would create a new element of the type Rational where the nominator is 1 and the denominator is 2. Let's try this out in a worksheet. So I will make a new package for the week three, and I create a new worksheet in that package, call it Rationals. The one thing I am going to do after the first object, which will serve as usual as my scratch pad to try things out is define my class of rational. So I say rational and I have the two ints as you see on the site. And that's it. So what we do here is we create a new Rational(1, 2). And what we see is we have created a new value res0 of type Rational. And the value prints as a rational act and then some number, the number happens to be the hash code of that rational number. That's just the peculiarity of the JVM, if you don't define how the things should print, that's the way they print. Okay, so lets continue the rational sample and define the numerator to be x and the denominator to be y. And what we could do then is we could call this Rational, say X. We could take the numerator of X and what we see No big surprise is 1, whereas if we take the denominator, we would get 2. So you see that objects of class Rational have now two members called numer and denom, and that the members are selected just like in Java with an infix dot between the object name and the member name. So the next step is to do some arithmetic on rationals. We want to define the usual arithmetic functions, addition, subtraction, multiplication, division, using the definitions you see here. One thing we could do is use the class rational as pure data [INAUDIBLE]. Just as something that gives us the data and define the operations as functions outside. So addRational would take two rationals, give you a rational. It would create a new rational according to the mathematical formula. That multiplies numerators and denominators of the two rationals in the way that you see here. And to make things print nicely, we could also define a makeString function that takes a rational and produces the numerator of the rational. And the denominator of the rational separated by a slash. So now we can write new Rational (1, 2) and new Rational (2, 3), add the two with addRational, produce a string with makeString. And we would get 7/6 following the usual arithmetic laws, but one can also go further and package the arithmetic functions that operate on a data abstraction in the data abstraction itself. So those functions that are put into classes are called methods. In our example, we could imagine that rational numbers now would have, in addition to the methods number and denom, also the methods add, subtract, multiply, divide, equal, and toString. Let's do this in direct sheet, let's define in addition function at that takes one rational as it's argument. And it would thenproduce the numerator times the arguments denominator plus the arguments numerator times it's own denominator. That's the numerator of the new rational. And its denominator would be just the two nominators multiplied. One thing important here is that the addition function here takes only a single parameter, not two. Where is the other one? Well, the left of add is really the rational number itself or rational number for which we define the class. So, let's see how that would work in the worksheet, we would write let's write another rational, maybe two-thirds. And then, let's write x.add(y). We still don't see very much because our rational class doesn't have a good way to print itself so let's change that. We do change that by adding a toString function to the class. A toString function will be called whenever an element of the class should be printed. So what would two string be? One thing to do it would be to say it's numer plus the slash plus denom where the plus is string concatenation. Okay, we see an error here. What do we get? It says that we override a method and we need an override modifier. I'll explain that later on what that means. So now, let's just do as the clip suggests and we see that all rational numbers now print And nicely. Okay, let's do an exercise. In your worksheet, add a method neg to class Rational that is used like this. x.neg should evaluate to the negative of the rational number x. As a second step, now add a method subtract or sub to subtract two rational numbers. And final question that you have to answer using your worksheet is with the numbers x, y, z as given on the previous slide what is the result of x- y- z? Okay, lets see how we would do that. We take first the test case. Let's take those three definitions and paste them into the worksheet. Replacing the previous one. And get to work so the first thing that we need to do is find a function. Neg. So neg should have a type, rational. And what we will do is we return a new rational with the numerator negated and the denominator as it is before. The next thing we have to do is define the function subtract, which like add takes another rational And return some other rational, so what would we do there? Well of course, we could define it with a formula not unlike the one for add. The one for subtract directly derives from that. But, a more elegant way would be to say well we add the parameter negated. So what's the advantage of doing is this way is that we do not Repeat ourselves. We do not have to write this rather complicated formula twice. So that's usually called the dry principle, don't repeat yourself, and it's a very good guideline for software development whether it's functional or not. So once we have that let's add the test case so that would be x.sub(y) Subtract that. And we get the answer minus 79 divided by 42. So in this session you have seen classes and objects as a fundamental way to organize data. In the next session we are going to learn several new things about classes and objects. It actually pays to save the worksheet that you have worked on and for this session because you will find good use for it and the next sessions this way