{"id":27,"date":"2021-12-18T21:11:01","date_gmt":"2021-12-18T21:11:01","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=27"},"modified":"2021-12-18T21:11:01","modified_gmt":"2021-12-18T21:11:01","slug":"scala-your-first-program","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-your-first-program\/","title":{"rendered":"Scala &#8211; Your First Program"},"content":{"rendered":"<p>In this lesson you will write your first program in Scala. I would also explain to you the basic syntax and structure of a Scala program. I do recommend you\u00a0 follow along in the video tutorial available in<a href=\"https:\/\/www.youtube.com\/c\/KindsonTheTechPro\" target=\"_blank\" rel=\"noopener\"> my YouTube Channel<\/a>.<\/p>\n<p>The practicals in this lesson would be based on IntelliJ IDE.<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Creating a Scala Project<\/a><\/li>\n<li><a href=\"#t2\">Writing a Scala Program<\/a><\/li>\n<li><a href=\"#t3\">Running a Scala Program<\/a><\/li>\n<li><a href=\"#t4\">Running a Scala Program with Scala Worksheet<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Creating a Scala Project<\/strong><\/h4>\n<p>Open IntelliJ and create a new Project using the settings in the screenshot below:<\/p>\n<figure id=\"attachment_29\" aria-describedby=\"caption-attachment-29\" style=\"width: 924px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-16.32.39.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-29 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-16.32.39.png\" alt=\"Creating a new Scala project IntelliJ\" width=\"924\" height=\"540\" srcset=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-16.32.39.png 924w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-16.32.39-300x175.png 300w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-16.32.39-768x449.png 768w\" sizes=\"auto, (max-width: 924px) 100vw, 924px\" \/><\/a><figcaption id=\"caption-attachment-29\" class=\"wp-caption-text\">Creating a new Scala project in IntelliJ<\/figcaption><\/figure>\n<p>Click on Next, specify a directory and a name for the project. For me, I name it SecondProgram<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Writing a Scala Program<\/strong><\/h4>\n<p>Creating a program in Scala is similar to creating a program in Java. We would create a package and then create a scala file inside the package. The file we would create would have the extension .scala. But first, you need to add Framework support to your project<\/p>\n<p><strong>Step 1<\/strong> &#8211; Right-click on your project and select Add Framework Support&#8230;Select Scala from the list and click Ok<\/p>\n<p><strong>Step 2<\/strong>\u00a0&#8211; Create a new package. I name it <em>tutorial<\/em><\/p>\n<p><strong>Step 3<\/strong> &#8211; Right-click on the tutorial package you create and choose New Scala class<\/p>\n<p><strong>Step 4<\/strong> &#8211; Name the class Main and change the type to object<\/p>\n<p><strong>Step 5<\/strong> &#8211; Enter the following code inside the class:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Your First Progam<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Main<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">App<\/span><span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> ages <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Seq<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">89<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">56<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">32<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">65<\/span><span style=\"color: #333333;\">)<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> name <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Othniel Muno\"<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span>s<span style=\"background-color: #fff0f0;\">\"The oldest of all is ${ages.max}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span>s<span style=\"background-color: #fff0f0;\">\"His name is ${name}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Running the Scala Program<\/strong><\/h4>\n<p>To run the program, follow the steps below<\/p>\n<p><strong>Step 1<\/strong> &#8211; From the Run menu, click on <strong><em>Edit configuration<\/em><\/strong>.<\/p>\n<p><strong>Step 2<\/strong> &#8211; In the Name field, enter <em><strong>Run Scala Program<\/strong><\/em> (you can use a different name as well).<\/p>\n<p><strong>Step 3<\/strong> &#8211; In the Tasks field, enter ~run. The tilde(~) would cause sbt to trigger a rebuild of the program each time a change is detected<\/p>\n<p>At this point, the configuration window will be as shown below:<\/p>\n<figure id=\"attachment_31\" aria-describedby=\"caption-attachment-31\" style=\"width: 927px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-21.36.15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-31 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-21.36.15-e1639860294209.png\" alt=\"IntelliJ Run Configuration for a Scala Program\" width=\"927\" height=\"550\" srcset=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-21.36.15-e1639860294209.png 927w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-21.36.15-e1639860294209-300x178.png 300w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-21.36.15-e1639860294209-768x456.png 768w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-21.36.15-e1639860294209-445x265.png 445w\" sizes=\"auto, (max-width: 927px) 100vw, 927px\" \/><\/a><figcaption id=\"caption-attachment-31\" class=\"wp-caption-text\">IntelliJ Run Configuration for a Scala Program<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong>Step 5<\/strong> &#8211; Click OK to Save your configuration<\/p>\n<p><strong>Step 6<\/strong> &#8211; You can now click on the Run button to run the program. You can see the output in the sbt shell window at the lower side of IDE.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Running Scala Program with Scala Worksheet<\/strong><\/h4>\n<p>Scala Worksheet is a feature that helps you run your Scala program in an interactive manner. So you are able to enter an expression and have it evaluated immediately and the output is shown in an output window.<\/p>\n<p>To use Scala worksheet, follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Right-click on the tutorial package and select Scala Worksheet<\/p>\n<p><strong>Step 2<\/strong> &#8211; Enter the following code:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">val<\/span> ages <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Seq<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">26<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">74<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">54<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">78<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> name <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Angus Uba\"<\/span>\r\nprintln<span style=\"color: #333333;\">(<\/span>s<span style=\"background-color: #fff0f0;\">\"The oldest person is ${ages.max}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\nprintln<span style=\"color: #333333;\">(<\/span>s<span style=\"background-color: #fff0f0;\">\"His name is ${name}\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> &#8211; Click on the green button to run. The code is executed and the output is shown as below:<\/p>\n<figure id=\"attachment_33\" aria-describedby=\"caption-attachment-33\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-22.03.55.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-33\" src=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-22.03.55-1024x622.png\" alt=\"Running Scala program using Scala Worksheet\" width=\"1024\" height=\"622\" srcset=\"https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-22.03.55-1024x622.png 1024w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-22.03.55-300x182.png 300w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-22.03.55-768x467.png 768w, https:\/\/www.kindsonthegenius.com\/scala\/wp-content\/uploads\/sites\/14\/2021\/12\/Screenshot-2021-12-18-at-22.03.55.png 1394w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-33\" class=\"wp-caption-text\">Running Scala program using Scala Worksheet<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>I recommend taking some time to enter different expression just to see the output.\u00a0 Once you play around a bit with Scala Worksheet, you&#8217;ll get used to it. However in subsequent tutorials, we would be writing Scala classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson you will write your first program in Scala. I would also explain to you the basic syntax and structure of a Scala &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-scala-tutorial"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/27\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}