{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[32mimport \u001b[39m\u001b[36m$ivy.$ \u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import $ivy.`org.scalatest::scalatest:3.0.5`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises\n", "## Part 1\n", "Add a new non-implemented method to trait `Fruit`, that doesn't have any argument and returns a Double. Take method `color` as a reference" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mtrait\u001b[39m \u001b[36mFruit\u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trait Fruit {\n", " def color: String\n", " ???\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 2\n", "Next, we have the Companion Object for trait `Fruit` that we created in PART I. Implement method `create`, that creates a `Fruit`, using the argument `_color`, and setting the `price` to \"3.0\".\n", "\n", "TIP: Remember that a trait has no constructors, so you'll have to use anonymous class syntax to create an instance. (`new Fruit {...}`)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mobject\u001b[39m \u001b[36mFruit\u001b[39m" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "object Fruit {\n", " def create(_color: String): Fruit = ???\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 3\n", "Make class `Banana` extend from trait `Fruit`, setting the method `color` to \"yellow\" and receiving the `price` in the constructor." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mclass\u001b[39m \u001b[36mBanana\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Banana {\n", " def color: String = ???\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 4\n", "Create a fruit with any `color` using `Fruit.create` and a banana of any `price` using Banana class' constructor." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import org.scalatest._\n", "class Test(fruit:Fruit, banana:Banana) extends FunSpec with Matchers {\n", " describe(\"The fruit\") {\n", " it(\"should cost 3.0\") {\n", " // Uncomment next line once the exercise is done\n", " // fruit.price shouldBe 3.0\n", " }\n", " }\n", "\n", " describe(\"The banana\") {\n", " ignore(\"should be \\\"yellow\\\"\") {\n", " banana.color shouldBe \"yellow\"\n", " }\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val fruit: Fruit = ???\n", "val banana: Banana = ???\n", "\n", "run(new Test(fruit, banana))" ] } ], "metadata": { "kernelspec": { "display_name": "Scala", "language": "scala", "name": "scala" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala", "nbconvert_exporter": "script", "version": "2.12.8" } }, "nbformat": 4, "nbformat_minor": 2 }