├── .gitignore ├── LICENSE.txt ├── README.textile ├── common ├── pom.xml └── src │ └── main │ └── scala │ └── scalaexamples │ └── EmptyTest.scala ├── first-class-functions ├── pom.xml └── src │ ├── main │ └── scala │ │ └── scalaexamples │ │ └── firstclassfunctions │ │ └── Person.scala │ └── test │ └── scala │ └── scalaexamples │ └── firstclassfunctions │ └── PersonTest.scala ├── higher-order-functions ├── pom.xml └── src │ ├── main │ └── scala │ │ └── scalaexamples │ │ └── higherorderfunctions │ │ ├── Person.scala │ │ ├── PersonFilter.scala │ │ ├── PersonPartitioner.scala │ │ └── PersonTransformer.scala │ └── test │ └── scala │ └── scalaexamples │ └── higherorderfunctions │ ├── PersonFilterTest.scala │ ├── PersonPartitionerTest.scala │ └── PersonTransformerTest.scala ├── intro ├── pom.xml └── src │ ├── main │ └── scala │ │ └── scalaexamples │ │ └── intro │ │ ├── HelloWorld.scala │ │ └── Stuff.scala │ └── test │ └── scala │ └── scalaexamples │ └── intro │ ├── CreateStuffTest.scala │ └── MyFirstTest.scala ├── oo-traits ├── pom.xml └── src │ ├── main │ └── scala │ │ └── scalaexamples │ │ ├── companionobject │ │ └── Train.scala │ │ ├── inheritance │ │ ├── Circle.scala │ │ ├── Rectangle.scala │ │ ├── Shape.scala │ │ └── Square.scala │ │ └── traits │ │ └── Element.scala │ └── test │ └── scala │ └── scalaexamples │ ├── companionobject │ └── CompanionObjectTest.scala │ ├── inheritance │ └── ShapeTest.scala │ └── traits │ ├── BasicTraitTest.scala │ └── DynamicMixinTest.scala ├── pattern-matching ├── pom.xml └── src │ ├── main │ └── scala │ │ └── scalaexamples │ │ └── patternmatching │ │ └── MyCaseClass.scala │ └── test │ └── scala │ └── scalaexamples │ └── patternmatching │ ├── ListMatchingTest.scala │ ├── MyCaseClassMatchingTest.scala │ ├── RegexMatchingTest.scala │ ├── TupleTestMatchTest.scala │ └── TypeMatchingTest.scala ├── pom.xml ├── project ├── ScalaTraining.scala └── build.properties └── quiz ├── pom.xml └── src └── main ├── resources └── quiz.xml └── scala └── scalaexamples └── quiz ├── MockQuizProvider.scala ├── Model.scala ├── QuizGame.scala ├── QuizProvider.scala ├── XmlQuizProvider.scala ├── console └── ConsoleGame.scala └── swing └── SwingGame.scala /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | target 3 | .classpath 4 | .project 5 | .manager 6 | *~ 7 | build.xml 8 | manifest.mf 9 | nbproject 10 | reports 11 | catalog.xml 12 | TAGS 13 | .settings 14 | *.iws 15 | *.ipr 16 | *.iml 17 | .idea 18 | *iml 19 | lib_managed/ 20 | boot/ 21 | .scala_dependencies 22 | .cache 23 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Creative Commons 2 | Creative Commons Legal Code 3 | Attribution 3.0 Unported 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT 6 | PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS LICENSE DOES NOT 7 | CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES 8 | THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO 9 | WARRANTIES REGARDING THE INFORMATION PROVIDED, AND DISCLAIMS 10 | LIABILITY FOR DAMAGES RESULTING FROM ITS USE. 11 | 12 | License 13 | 14 | THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS 15 | CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS 16 | PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE 17 | WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS 18 | PROHIBITED. 19 | 20 | BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND 21 | AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS 22 | LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU 23 | THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH 24 | TERMS AND CONDITIONS. 25 | 26 | 1. Definitions 27 | 28 | a. "Adaptation" means a work based upon the Work, or upon the Work 29 | and other pre-existing works, such as a translation, adaptation, 30 | derivative work, arrangement of music or other alterations of a 31 | literary or artistic work, or phonogram or performance and 32 | includes cinematographic adaptations or any other form in which 33 | the Work may be recast, transformed, or adapted including in any 34 | form recognizably derived from the original, except that a work 35 | that constitutes a Collection will not be considered an 36 | Adaptation for the purpose of this License. For the avoidance of 37 | doubt, where the Work is a musical work, performance or 38 | phonogram, the synchronization of the Work in timed-relation 39 | with a moving image ("synching") will be considered an 40 | Adaptation for the purpose of this License. 41 | 42 | b. "Collection" means a collection of literary or artistic works, 43 | such as encyclopedias and anthologies, or performances, 44 | phonograms or broadcasts, or other works or subject matter other 45 | than works listed in Section 1(f) below, which, by reason of the 46 | selection and arrangement of their contents, constitute 47 | intellectual creations, in which the Work is included in its 48 | entirety in unmodified form along with one or more other 49 | contributions, each constituting separate and independent works 50 | in themselves, which together are assembled into a collective 51 | whole. A work that constitutes a Collection will not be 52 | considered an Adaptation (as defined above) for the purposes of 53 | this License. 54 | 55 | c. "Distribute" means to make available to the public the original 56 | and copies of the Work or Adaptation, as appropriate, through 57 | sale or other transfer of ownership. 58 | 59 | d. "Licensor" means the individual, individuals, entity or entities 60 | that offer(s) the Work under the terms of this License. 61 | 62 | e. "Original Author" means, in the case of a literary or artistic 63 | work, the individual, individuals, entity or entities who 64 | created the Work or if no individual or entity can be 65 | identified, the publisher; and in addition (i) in the case of a 66 | performance the actors, singers, musicians, dancers, and other 67 | persons who act, sing, deliver, declaim, play in, interpret or 68 | otherwise perform literary or artistic works or expressions of 69 | folklore; (ii) in the case of a phonogram the producer being the 70 | person or legal entity who first fixes the sounds of a 71 | performance or other sounds; and, (iii) in the case of 72 | broadcasts, the organization that transmits the broadcast. 73 | 74 | f. "Work" means the literary and/or artistic work offered under the 75 | terms of this License including without limitation any 76 | production in the literary, scientific and artistic domain, 77 | whatever may be the mode or form of its expression including 78 | digital form, such as a book, pamphlet and other writing; a 79 | lecture, address, sermon or other work of the same nature; a 80 | dramatic or dramatico-musical work; a choreographic work or 81 | entertainment in dumb show; a musical composition with or 82 | without words; a cinematographic work to which are assimilated 83 | works expressed by a process analogous to cinematography; a work 84 | of drawing, painting, architecture, sculpture, engraving or 85 | lithography; a photographic work to which are assimilated works 86 | expressed by a process analogous to photography; a work of 87 | applied art; an illustration, map, plan, sketch or 88 | three-dimensional work relative to geography, topography, 89 | architecture or science; a performance; a broadcast; a 90 | phonogram; a compilation of data to the extent it is protected 91 | as a copyrightable work; or a work performed by a variety or 92 | circus performer to the extent it is not otherwise considered a 93 | literary or artistic work. 94 | 95 | g. "You" means an individual or entity exercising rights under this 96 | License who has not previously violated the terms of this 97 | License with respect to the Work, or who has received express 98 | permission from the Licensor to exercise rights under this 99 | License despite a previous violation. 100 | 101 | h. "Publicly Perform" means to perform public recitations of the 102 | Work and to communicate to the public those public recitations, 103 | by any means or process, including by wire or wireless means or 104 | public digital performances; to make available to the public 105 | Works in such a way that members of the public may access these 106 | Works from a place and at a place individually chosen by them; 107 | to perform the Work to the public by any means or process and 108 | the communication to the public of the performances of the Work, 109 | including by public digital performance; to broadcast and 110 | rebroadcast the Work by any means including signs, sounds or 111 | images. 112 | 113 | i. "Reproduce" means to make copies of the Work by any means 114 | including without limitation by sound or visual recordings and 115 | the right of fixation and reproducing fixations of the Work, 116 | including storage of a protected performance or phonogram in 117 | digital form or other electronic medium. 118 | 119 | 2. Fair Dealing Rights. Nothing in this License is intended to reduce, 120 | limit, or restrict any uses free from copyright or rights arising 121 | from limitations or exceptions that are provided for in connection 122 | with the copyright protection under copyright law or other 123 | applicable laws. 124 | 125 | 3. License Grant. Subject to the terms and conditions of this License, 126 | Licensor hereby grants You a worldwide, royalty-free, 127 | non-exclusive, perpetual (for the duration of the applicable 128 | copyright) license to exercise the rights in the Work as stated 129 | below: 130 | 131 | a. to Reproduce the Work, to incorporate the Work into one or more 132 | Collections, and to Reproduce the Work as incorporated in the 133 | Collections; 134 | 135 | b. to create and Reproduce Adaptations provided that any such 136 | Adaptation, including any translation in any medium, takes 137 | reasonable steps to clearly label, demarcate or otherwise 138 | identify that changes were made to the original Work. For 139 | example, a translation could be marked "The original work was 140 | translated from English to Spanish," or a modification could 141 | indicate "The original work has been modified."; 142 | 143 | c. to Distribute and Publicly Perform the Work including as 144 | incorporated in Collections; and, 145 | 146 | d. to Distribute and Publicly Perform Adaptations. 147 | 148 | e. For the avoidance of doubt: 149 | 150 | i. Non-waivable Compulsory License Schemes. In those 151 | jurisdictions in which the right to collect royalties 152 | through any statutory or compulsory licensing scheme 153 | cannot be waived, the Licensor reserves the exclusive 154 | right to collect such royalties for any exercise by You of 155 | the rights granted under this License; 156 | 157 | ii. Waivable Compulsory License Schemes. In those 158 | jurisdictions in which the right to collect royalties 159 | through any statutory or compulsory licensing scheme can 160 | be waived, the Licensor waives the exclusive right to 161 | collect such royalties for any exercise by You of the 162 | rights granted under this License; and, 163 | 164 | iii. Voluntary License Schemes. The Licensor waives the right 165 | to collect royalties, whether individually or, in the 166 | event that the Licensor is a member of a collecting 167 | society that administers voluntary licensing schemes, via 168 | that society, from any exercise by You of the rights 169 | granted under this License. 170 | 171 | The above rights may be exercised in all media and formats whether now 172 | known or hereafter devised. The above rights include the right to make 173 | such modifications as are technically necessary to exercise the rights 174 | in other media and formats. Subject to Section 8(f), all rights not 175 | expressly granted by Licensor are hereby reserved. 176 | 177 | 4. Restrictions. The license granted in Section 3 above is expressly 178 | made subject to and limited by the following restrictions: 179 | 180 | a. You may Distribute or Publicly Perform the Work only under the 181 | terms of this License. You must include a copy of, or the 182 | Uniform Resource Identifier (URI) for, this License with every 183 | copy of the Work You Distribute or Publicly Perform. You may not 184 | offer or impose any terms on the Work that restrict the terms of 185 | this License or the ability of the recipient of the Work to 186 | exercise the rights granted to that recipient under the terms of 187 | the License. You may not sublicense the Work. You must keep 188 | intact all notices that refer to this License and to the 189 | disclaimer of warranties with every copy of the Work You 190 | Distribute or Publicly Perform. When You Distribute or Publicly 191 | Perform the Work, You may not impose any effective technological 192 | measures on the Work that restrict the ability of a recipient of 193 | the Work from You to exercise the rights granted to that 194 | recipient under the terms of the License. This Section 4(a) 195 | applies to the Work as incorporated in a Collection, but this 196 | does not require the Collection apart from the Work itself to be 197 | made subject to the terms of this License. If You create a 198 | Collection, upon notice from any Licensor You must, to the 199 | extent practicable, remove from the Collection any credit as 200 | required by Section 4(b), as requested. If You create an 201 | Adaptation, upon notice from any Licensor You must, to the 202 | extent practicable, remove from the Adaptation any credit as 203 | required by Section 4(b), as requested. 204 | 205 | b. If You Distribute, or Publicly Perform the Work or any 206 | Adaptations or Collections, You must, unless a request has been 207 | made pursuant to Section 4(a), keep intact all copyright notices 208 | for the Work and provide, reasonable to the medium or means You 209 | are utilizing: (i) the name of the Original Author (or 210 | pseudonym, if applicable) if supplied, and/or if the Original 211 | Author and/or Licensor designate another party or parties (e.g., 212 | a sponsor institute, publishing entity, journal) for attribution 213 | ("Attribution Parties") in Licensor's copyright notice, terms of 214 | service or by other reasonable means, the name of such party or 215 | parties; (ii) the title of the Work if supplied; (iii) to the 216 | extent reasonably practicable, the URI, if any, that Licensor 217 | specifies to be associated with the Work, unless such URI does 218 | not refer to the copyright notice or licensing information for 219 | the Work; and (iv) , consistent with Section 3(b), in the case 220 | of an Adaptation, a credit identifying the use of the Work in 221 | the Adaptation (e.g., "French translation of the Work by 222 | Original Author," or "Screenplay based on original Work by 223 | Original Author"). The credit required by this Section 4 (b) may 224 | be implemented in any reasonable manner; provided, however, that 225 | in the case of a Adaptation or Collection, at a minimum such 226 | credit will appear, if a credit for all contributing authors of 227 | the Adaptation or Collection appears, then as part of these 228 | credits and in a manner at least as prominent as the credits for 229 | the other contributing authors. For the avoidance of doubt, You 230 | may only use the credit required by this Section for the purpose 231 | of attribution in the manner set out above and, by exercising 232 | Your rights under this License, You may not implicitly or 233 | explicitly assert or imply any connection with, sponsorship or 234 | endorsement by the Original Author, Licensor and/or Attribution 235 | Parties, as appropriate, of You or Your use of the Work, without 236 | the separate, express prior written permission of the Original 237 | Author, Licensor and/or Attribution Parties. 238 | 239 | c. Except as otherwise agreed in writing by the Licensor or as may 240 | be otherwise permitted by applicable law, if You Reproduce, 241 | Distribute or Publicly Perform the Work either by itself or as 242 | part of any Adaptations or Collections, You must not distort, 243 | mutilate, modify or take other derogatory action in relation to 244 | the Work which would be prejudicial to the Original Author's 245 | honor or reputation. Licensor agrees that in those jurisdictions 246 | (e.g. Japan), in which any exercise of the right granted in 247 | Section 3(b) of this License (the right to make Adaptations) 248 | would be deemed to be a distortion, mutilation, modification or 249 | other derogatory action prejudicial to the Original Author's 250 | honor and reputation, the Licensor will waive or not assert, as 251 | appropriate, this Section, to the fullest extent permitted by 252 | the applicable national law, to enable You to reasonably 253 | exercise Your right under Section 3(b) of this License (right to 254 | make Adaptations) but not otherwise. 255 | 256 | 5. Representations, Warranties and Disclaimer 257 | 258 | UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, 259 | LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR 260 | WARRANTIES OF ANY KIND CONCERNING THE WORK, EXPRESS, IMPLIED, 261 | STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES 262 | OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, 263 | NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, 264 | ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT 265 | DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF 266 | IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU. 267 | 268 | 6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY 269 | APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY 270 | LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE 271 | OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE 272 | WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 273 | DAMAGES. 274 | 275 | 7. Termination 276 | 277 | a. This License and the rights granted hereunder will terminate 278 | automatically upon any breach by You of the terms of this 279 | License. Individuals or entities who have received Adaptations 280 | or Collections from You under this License, however, will not 281 | have their licenses terminated provided such individuals or 282 | entities remain in full compliance with those licenses. Sections 283 | 1, 2, 5, 6, 7, and 8 will survive any termination of this 284 | License. 285 | 286 | b. Subject to the above terms and conditions, the license granted 287 | here is perpetual (for the duration of the applicable copyright 288 | in the Work). Notwithstanding the above, Licensor reserves the 289 | right to release the Work under different license terms or to 290 | stop distributing the Work at any time; provided, however that 291 | any such election will not serve to withdraw this License (or 292 | any other license that has been, or is required to be, granted 293 | under the terms of this License), and this License will continue 294 | in full force and effect unless terminated as stated above. 295 | 296 | 8. Miscellaneous 297 | 298 | a. Each time You Distribute or Publicly Perform the Work or a 299 | Collection, the Licensor offers to the recipient a license to 300 | the Work on the same terms and conditions as the license granted 301 | to You under this License. 302 | 303 | b. Each time You Distribute or Publicly Perform an Adaptation, 304 | Licensor offers to the recipient a license to the original Work 305 | on the same terms and conditions as the license granted to You 306 | under this License. 307 | 308 | c. If any provision of this License is invalid or unenforceable 309 | under applicable law, it shall not affect the validity or 310 | enforceability of the remainder of the terms of this License, 311 | and without further action by the parties to this agreement, 312 | such provision shall be reformed to the minimum extent necessary 313 | to make such provision valid and enforceable. 314 | 315 | d. No term or provision of this License shall be deemed waived and 316 | no breach consented to unless such waiver or consent shall be in 317 | writing and signed by the party to be charged with such waiver 318 | or consent. 319 | 320 | e. This License constitutes the entire agreement between the 321 | parties with respect to the Work licensed here. There are no 322 | understandings, agreements or representations with respect to 323 | the Work not specified here. Licensor shall not be bound by any 324 | additional provisions that may appear in any communication from 325 | You. This License may not be modified without the mutual written 326 | agreement of the Licensor and You. 327 | 328 | f. The rights granted under, and the subject matter referenced, in 329 | this License were drafted utilizing the terminology of the Berne 330 | Convention for the Protection of Literary and Artistic Works (as 331 | amended on September 28, 1979), the Rome Convention of 1961, the 332 | WIPO Copyright Treaty of 1996, the WIPO Performances and 333 | Phonograms Treaty of 1996 and the Universal Copyright Convention 334 | (as revised on July 24, 1971). These rights and subject matter 335 | take effect in the relevant jurisdiction in which the License 336 | terms are sought to be enforced according to the corresponding 337 | provisions of the implementation of those treaty provisions in 338 | the applicable national law. If the standard suite of rights 339 | granted under applicable copyright law includes additional 340 | rights not granted under this License, such additional rights 341 | are deemed to be included in the License; this License is not 342 | intended to restrict the license of any rights under applicable 343 | law. 344 | 345 | Creative Commons Notice 346 | 347 | Creative Commons is not a party to this License, and makes no 348 | warranty whatsoever in connection with the Work. Creative Commons 349 | will not be liable to You or any party on any legal theory for any 350 | damages whatsoever, including without limitation any general, 351 | special, incidental or consequential damages arising in connection 352 | to this license. Notwithstanding the foregoing two (2) sentences, 353 | if Creative Commons has expressly identified itself as the 354 | Licensor hereunder, it shall have all rights and obligations of 355 | Licensor. 356 | 357 | Except for the limited purpose of indicating to the public that 358 | the Work is licensed under the CCPL, Creative Commons does not 359 | authorize the use by either party of the trademark "Creative 360 | Commons" or any related trademark or logo of Creative Commons 361 | without the prior written consent of Creative Commons. Any 362 | permitted use will be in compliance with Creative Commons' 363 | then-current trademark usage guidelines, as may be published on 364 | its website or otherwise made available upon request from time to 365 | time. For the avoidance of doubt, this trademark restriction does 366 | not form part of this License. 367 | 368 | Creative Commons may be contacted at http://creativecommons.org/. 369 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h2. Scala training - code 2 | 3 | This github project is the code used for the Scala training course. It is recommended to solve them supervised during a training session. 4 | 5 | The slides which go along with the code is kept at "Scala training slides":http://github.com/javaBin/scala-training-slides. You should check out the readme in the slide project to understand a little bit more of the background for the training class and how we performed it. 6 | 7 | h3. Topics 8 | 9 | The code has been divided into the following topics 10 | * Intro - a little warm up 11 | * First class functions - various exercises covering use of first class functions 12 | * Pattern matching - various types of pattern matching 13 | * oo-traits - exploring some of the topics around object orientation and trait support. Also a few exercises exploring syntax. 14 | 15 | We believe these tasks are enough for day one of the course. Second day would typically be only the quiz project, which is a larger task. To solve it you must parse some XML, write a console app and a Scala Swing app. 16 | 17 | h3. Solutions 18 | 19 | All the tasks have been solved in a Git branch called 'solutions'. You may also download this code directly from Github with the following link: 20 | 21 | "http://github.com/javaBin/scala-training-code/zipball/solutions":http://github.com/javaBin/scala-training-code/zipball/solutions 22 | 23 | h3. Requests and contributions 24 | 25 | This project was initiated in 2009. It was used for training lessons for the Norwegian Scala community. It is not actively maintained by us anymore. However, every now and then, people send us request for improvements. We are totally psyched that people are still using it. 26 | 27 | However, we would greatly appreciate pull requests if you want something changed. 'Cause then we don't need to do too much work ourselves! :) 28 | 29 | h3. License 30 | 31 | The training course is licensed under "Creative Commons Attribution 3.0 Unported":http://creativecommons.org/licenses/by/3.0/. 32 | 33 | All this means, is that you need to credit Fredrik Vraalsen (fredrik@vraalsen.no) and Alf Kristian Støyle (alf.kristian@gmail.com) of scalaBin for the original work. Apart from that you may do whatever you please with the work. 34 | 35 | Please use this, and change this however you may please. 36 | -------------------------------------------------------------------------------- /common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | 9 | common 10 | 0.1-SNAPSHOT 11 | jar 12 | Scala Examples - General functionality 13 | 2009 14 | 15 | -------------------------------------------------------------------------------- /common/src/main/scala/scalaexamples/EmptyTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | 8 | class EmptyTest { 9 | 10 | // Write a unit test. Make it fail, and then make it pass. 11 | @Test def testRun{} 12 | 13 | } 14 | -------------------------------------------------------------------------------- /first-class-functions/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | 9 | first-class-functions 10 | 0.1-SNAPSHOT 11 | jar 12 | Scala Examples - First class functions 13 | 2009 14 | 15 | 16 | no.scalabin.scala-examples 17 | common 18 | ${project.version} 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /first-class-functions/src/main/scala/scalaexamples/firstclassfunctions/Person.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.firstclassfunctions 2 | 3 | case class Person(name: String, age: Int, emailAddresses: List[EmailAddress]) 4 | 5 | case class EmailAddress(address: String) 6 | -------------------------------------------------------------------------------- /first-class-functions/src/test/scala/scalaexamples/firstclassfunctions/PersonTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.firstclassfunctions 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | // Comment in the tests below and make them run and pass 10 | 11 | // The objective in each exercise is to 12 | // transform the list 'persons' in some way, 13 | // using the functions filter, map etc. on the list 14 | 15 | @RunWith(classOf[JUnit4]) 16 | class PersonTest extends EmptyTest { 17 | 18 | val alf = Person("Alf", 30, List(EmailAddress("alf.kristian@gmail.com"))) 19 | val fredrik = Person("Fredrik", 33, List(EmailAddress("fredrik@vraalsen.no"), EmailAddress("fvr@knowit.no"))) 20 | val johannes = Person("Johannes", 0, Nil) 21 | 22 | val persons = List(alf, fredrik, johannes) 23 | 24 | // @Test 25 | def testAdults { 26 | // Filter the list of persons to find all adults 27 | val adults = Nil 28 | 29 | assertEquals(List(alf, fredrik), adults) 30 | } 31 | 32 | // @Test 33 | def testName { 34 | // Find the names of all persons 35 | val names = Nil 36 | 37 | assertEquals(List("Alf", "Fredrik", "Johannes"), names) 38 | } 39 | 40 | // @Test 41 | def testNamesOfAdults { 42 | // Create a list containing the names of the adults, 43 | // by combining the approaches in the two exercies above 44 | val names = Nil 45 | 46 | assertEquals(List("Alf", "Fredrik"), names) 47 | } 48 | 49 | // @Test 50 | def testAgeLimit { 51 | // Partition the list of persons into two new lists, 52 | // one containing the adults and one containing the kids 53 | val (adults, kids) = (Nil, Nil) 54 | 55 | assertEquals(List(alf, fredrik), adults) 56 | assertEquals(List(johannes), kids) 57 | } 58 | 59 | // @Test 60 | def testHasMultipleEmails { 61 | // Split the list of persons into two new lists containing 62 | // techies (more than one email address) and luddites (zero or only one email address) 63 | val (techies, luddites) = (Nil, Nil) 64 | 65 | assertEquals(List(fredrik), techies) 66 | assertEquals(List(alf, johannes), luddites) 67 | } 68 | 69 | // @Test 70 | def testFindByName { 71 | // Find the person named "Johannes" in the list of persons 72 | val name = "Johannes" 73 | val person: Option[Person] = None 74 | 75 | person match { 76 | case Some(person) => assertEquals(johannes, person) 77 | case _ => sys.error("No match") 78 | } 79 | } 80 | 81 | // @Test 82 | def testFindByName2 { 83 | // Find the person named "Jon-Anders" (should not match) 84 | val name = "Jon-Anders" 85 | // replace with find expression 86 | val person: Option[Person] = Some(persons(0)) 87 | 88 | person match { 89 | case None => () // OK 90 | case _ => sys.error("Unexpected match") 91 | } 92 | } 93 | 94 | // @Test 95 | def testFindEmailAddressesByName { 96 | // Find the e-mail addresses of the person named "Alf". 97 | // Here you must first find the person, 98 | // then map the collection to a different type 99 | val name = "Alf" 100 | val addresses: Option[List[EmailAddress]] = None 101 | 102 | addresses match { 103 | case Some(addresses) => assertEquals(alf.emailAddresses, addresses) 104 | case _ => sys.error("No match") 105 | } 106 | } 107 | 108 | // @Test 109 | def testFindPersonByEmail { 110 | // Find the person who has the e-mail address "fvr@knowit.no". 111 | 112 | // Hint: Try combinining finding the person and checking if 113 | // there exists an email address matching the criteria in the 114 | // person's list of email addresses. 115 | val address = EmailAddress("fredrik@vraalsen.no") 116 | val person: Option[Person] = None 117 | 118 | person match { 119 | case Some(person) => assertEquals(fredrik, person) 120 | case _ => sys.error("No match") 121 | } 122 | } 123 | 124 | // @Test 125 | def testGetFirstEmailAddress { 126 | // Create a new list of the first e-mail address of each person, 127 | // filtering out persons without e-mail addresses. 128 | 129 | // Hint: Try combining filtering the list and then 130 | // mapping it to a different type, only getting 131 | // the head of the email address list. 132 | val addresses = Nil 133 | 134 | assertEquals(List(alf.emailAddresses.head, fredrik.emailAddresses.head), addresses) 135 | } 136 | 137 | // @Test 138 | def testNameToEmailAddress { 139 | // Create a map from each persons name to their e-mail addresses, 140 | // filtering out persons without e-mail addresses 141 | // Hint: First filter list, then use foldLeft to accumulate... 142 | val emptyMap: Map[String, List[EmailAddress]] = Map() 143 | 144 | // Insert code here for emptyMap 145 | val nameToEmail = emptyMap 146 | 147 | assertEquals(Map(alf.name -> alf.emailAddresses, fredrik.name -> fredrik.emailAddresses), nameToEmail) 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /higher-order-functions/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | 9 | higher-order-functions 10 | 0.1-SNAPSHOT 11 | jar 12 | Scala Examples - Higher order functions 13 | 2009 14 | 15 | 16 | no.scalabin.scala-examples 17 | common 18 | ${project.version} 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /higher-order-functions/src/main/scala/scalaexamples/higherorderfunctions/Person.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.higherorderfunctions 2 | 3 | case class Person(name: String, age: Int, emailAddresses: List[EmailAddress]) 4 | 5 | case class EmailAddress(address: String) 6 | -------------------------------------------------------------------------------- /higher-order-functions/src/main/scala/scalaexamples/higherorderfunctions/PersonFilter.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.higherorderfunctions 2 | 3 | class PersonFilter { 4 | 5 | // Implement the method filterPersons that takes in 6 | // a list of persons and a function from Person to Boolean, 7 | // applies the function to each Person and 8 | // returns a new list of the persons for which the function returns true 9 | 10 | // def filterPersons(persons: List[Person], ...): List[Person] = Nil 11 | 12 | } 13 | -------------------------------------------------------------------------------- /higher-order-functions/src/main/scala/scalaexamples/higherorderfunctions/PersonPartitioner.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.higherorderfunctions 2 | 3 | class PersonPartitioner { 4 | 5 | // Implement the method partitionPersons that takes in 6 | // a list of persons and a function from Person to Boolean, 7 | // applies the function to each Person and 8 | // returns two new lists, one with the persons for which the function returns true 9 | // and one with the persons for which the function returns false 10 | 11 | // def partitionPersons(persons: List[Person], ...): (List[Person], List[Person]) = (Nil, Nil) 12 | 13 | } 14 | -------------------------------------------------------------------------------- /higher-order-functions/src/main/scala/scalaexamples/higherorderfunctions/PersonTransformer.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.higherorderfunctions 2 | 3 | class PersonTransformer { 4 | 5 | // Implement the method transformPersons that takes in 6 | // a list of persons and a function from Person to another type A, 7 | // applies the function to each Person and 8 | // returns a new list containing the results of applying the function to each person 9 | 10 | // def transformPersons[A](persons: List[Person], ...): List[A] = Nil 11 | 12 | } 13 | -------------------------------------------------------------------------------- /higher-order-functions/src/test/scala/scalaexamples/higherorderfunctions/PersonFilterTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.higherorderfunctions 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | // Comment in the tests below and make them run and pass 10 | 11 | @RunWith(classOf[JUnit4]) 12 | class PersonFilterTest extends EmptyTest { 13 | 14 | val personFilter = new PersonFilter 15 | 16 | val alf = Person("Alf", 30, List(EmailAddress("alf.kristian@gmail.com"))) 17 | val fredrik = Person("Fredrik", 33, List(EmailAddress("fredrik@vraalsen.no"), EmailAddress("fvr@knowit.no"))) 18 | val johannes = Person("Johannes", 0, Nil) 19 | 20 | val persons = alf :: fredrik :: johannes :: Nil 21 | 22 | // @Test 23 | def testAll { 24 | // Pass in a function that returns all persons (always true) 25 | // assertEquals(persons, personFilter.filterPersons(persons, error(""))) 26 | } 27 | 28 | // @Test 29 | def testNone { 30 | // Pass in a function that returns no persons (always false) 31 | // assertEquals(Nil, personFilter.filterPersons(persons, error(""))) 32 | } 33 | 34 | // @Test 35 | def testEmptyList { 36 | // Use the two functions from above to test personFilter on empty lists (Nil) 37 | // assertEquals(Nil, personFilter.filterPersons(Nil, error(""))) 38 | // assertEquals(Nil, personFilter.filterPersons(Nil, error(""))) 39 | } 40 | 41 | // @Test 42 | def testOld { 43 | // Pass in a function that returns the persons over 18 44 | // assertEquals(List(alf, fredrik), personFilter.filterPersons(persons, error(""))) 45 | } 46 | 47 | // @Test 48 | def testYoung { 49 | // Pass in a function that returns the persons under 18 50 | // assertEquals(List(johannes), personFilter.filterPersons(persons, error(""))) 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /higher-order-functions/src/test/scala/scalaexamples/higherorderfunctions/PersonPartitionerTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.higherorderfunctions 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | // Comment in the tests below and make them run and pass 10 | 11 | @RunWith(classOf[JUnit4]) 12 | class PersonPartitionerTest extends EmptyTest { 13 | 14 | val partitioner = new PersonPartitioner 15 | 16 | val alf = Person("Alf", 30, List(EmailAddress("alf.kristian@gmail.com"))) 17 | val fredrik = Person("Fredrik", 33, List(EmailAddress("fredrik@vraalsen.no"), EmailAddress("fvr@knowit.no"))) 18 | val johannes = Person("Johannes", 0, Nil) 19 | 20 | val persons = alf :: fredrik :: johannes :: Nil 21 | 22 | // @Test 23 | def testAgeLimit { 24 | // Pass in a function that tests whether the person is an adult 25 | val (adults, kids) = (Nil, Nil) // partitioner.partitionPersons(persons, error("")) 26 | 27 | assertEquals(List(alf, fredrik), adults) 28 | assertEquals(List(johannes), kids) 29 | } 30 | 31 | // @Test 32 | def testHasMoreThanOneEmail { 33 | // Pass in a function that tests whether the person is 34 | // a techie (2 or more e-mail addresses) or a luddite (zero or one e-mail address) 35 | val (techies, luddites) = (Nil, Nil) // partitioner.partitionPersons(persons, error("")) 36 | 37 | assertEquals(List(fredrik), techies) 38 | assertEquals(List(alf, johannes), luddites) 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /higher-order-functions/src/test/scala/scalaexamples/higherorderfunctions/PersonTransformerTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.higherorderfunctions 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | // Comment in the tests below and make them run and pass 10 | 11 | @RunWith(classOf[JUnit4]) 12 | class PersonTransformerTest extends EmptyTest { 13 | 14 | val personTransformer = new PersonTransformer 15 | 16 | val alf = Person("Alf", 30, List(EmailAddress("alf.kristian@gmail.com"))) 17 | val fredrik = Person("Fredrik", 33, List(EmailAddress("fredrik@vraalsen.no"), EmailAddress("fvr@knowit.no"))) 18 | val johannes = Person("Johannes", 0, Nil) 19 | 20 | val persons = alf :: fredrik :: johannes:: Nil 21 | 22 | // @Test 23 | def testEquals { 24 | // Pass in a function that returns the list of persons unchanged 25 | // assertEquals(persons, personTransformer.transformPersons(persons, error(""))) 26 | } 27 | 28 | // @Test 29 | def testName { 30 | // Pass in a function transforming the list to the names of the persons 31 | val expected = List("Alf", "Fredrik", "Johannes") 32 | // assertEquals(expected, personTransformer.transformPersons(persons, error(""))) 33 | () 34 | } 35 | 36 | // @Test 37 | def testAge { 38 | // Pass in a function transforming the list to the ages of the persons 39 | val expected = List(30, 33, 0) 40 | // assertEquals(expected, personTransformer.transformPersons(persons, error(""))) 41 | () 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /intro/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | 9 | intro 10 | 0.1-SNAPSHOT 11 | jar 12 | Scala Examples - Introduction topics 13 | 2009 14 | 15 | 16 | no.scalabin.scala-examples 17 | common 18 | ${project.version} 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /intro/src/main/scala/scalaexamples/intro/HelloWorld.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.intro 2 | 3 | // Print out HelloWorld 4 | object HelloWorld1 extends App { 5 | 6 | } 7 | 8 | // Print out HelloWorld 9 | object HelloWorld2 { 10 | 11 | def main(args: Array[String]) { 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /intro/src/main/scala/scalaexamples/intro/Stuff.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.intro 2 | 3 | class Stuff { 4 | 5 | // Override the toString method to say "Stuff" 6 | 7 | // Define a constant 'const' saying "This is a constant" 8 | 9 | // Define a variable 'variable' saying "This is a variable" 10 | 11 | // Define a method saying "Hello " to whatever input 12 | 13 | // Deine a method which prints hello, returning Unit (void) 14 | 15 | } 16 | 17 | object Stuff { 18 | 19 | // Define a method that returns "StuffObject" 20 | 21 | } 22 | -------------------------------------------------------------------------------- /intro/src/test/scala/scalaexamples/intro/CreateStuffTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.intro 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class CreateStuffTest extends EmptyTest { 11 | 12 | val stuff = new Stuff 13 | 14 | // @Test 15 | def accessOverridenToString { 16 | assertEquals("Stuff", stuff.toString) 17 | } 18 | 19 | // @Test 20 | def accessVal { 21 | // Make this compile and run 22 | 23 | // assertEquals("This is a constant", stuff.const) 24 | 25 | // When commenting in the following line, you must get a compile error 26 | // stuff.const = "Must give compile error" 27 | } 28 | 29 | // @Test 30 | def changeVar { 31 | // Make this compile and run 32 | 33 | // assertEquals("This is a variable", stuff.variable) 34 | val text = "Changed it" 35 | // stuff.variable = text 36 | // assertEquals(text, stuff.variable) 37 | () 38 | } 39 | 40 | // @Test 41 | def accessStuffObject { 42 | // Make this compile and run 43 | 44 | //assertEquals("StuffObject", Stuff.getString) 45 | } 46 | 47 | // @Test 48 | def testSayHello { 49 | // Make this compile and run 50 | 51 | // assertEquals("Hello Alf", stuff.sayHello("Alf")) 52 | } 53 | 54 | // @Test 55 | def testPrintHello { 56 | // Make this compile and run 57 | 58 | // assertEquals((), stuff.printHello("Alf")) 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /intro/src/test/scala/scalaexamples/intro/MyFirstTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.intro 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class MyFirstTest extends EmptyTest{ 11 | 12 | // Define a test. Make it fail, and then make it pass 13 | 14 | } 15 | -------------------------------------------------------------------------------- /oo-traits/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | 9 | oo-traits 10 | 0.1-SNAPSHOT 11 | jar 12 | Scala Examples - OO og Traits 13 | 2009 14 | 15 | 16 | no.scalabin.scala-examples 17 | common 18 | ${project.version} 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /oo-traits/src/main/scala/scalaexamples/companionobject/Train.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.companionobject 2 | 3 | // Define a primary constructor which takes a list of passengers (String) and an Option conductor also String 4 | class Train { 5 | 6 | // You should also define an auxiliary constructor, which ignores the conductor 7 | } 8 | 9 | object Train { 10 | 11 | // make a method which makes Train(Array("Alf", "Fredrik")) compile 12 | 13 | // make a method which makes Train("Alf", "Fredrik") compile 14 | 15 | // make a method which makes Train(Some("Conductor"), "Alf", "Fredrik") compile 16 | 17 | } -------------------------------------------------------------------------------- /oo-traits/src/main/scala/scalaexamples/inheritance/Circle.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.inheritance 2 | 3 | import scala.math._ 4 | 5 | // Make Circle extend Shape, and add a primary constructor for radius and color 6 | class Circle { 7 | 8 | // Create a auxiliary constructor 9 | 10 | // Define the area function 11 | 12 | // Make this compile 13 | // def circumference = 2 * Pi * radius 14 | 15 | } 16 | -------------------------------------------------------------------------------- /oo-traits/src/main/scala/scalaexamples/inheritance/Rectangle.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.inheritance 2 | 3 | class Rectangle(width: Double, height: Double, color: String) extends Shape(color) { 4 | 5 | // Create a auxiliary constructor 6 | 7 | def area = width * height 8 | def circumference = 2 * width + 2 * height 9 | 10 | } 11 | -------------------------------------------------------------------------------- /oo-traits/src/main/scala/scalaexamples/inheritance/Shape.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.inheritance 2 | 3 | abstract class Shape(val color: String) { 4 | 5 | def area: Double 6 | def circumference: Double 7 | 8 | } 9 | 10 | object Shape { 11 | 12 | val DefaultColor = "white" 13 | 14 | } 15 | 16 | -------------------------------------------------------------------------------- /oo-traits/src/main/scala/scalaexamples/inheritance/Square.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.inheritance 2 | 3 | // Subclass the Rectangle 4 | class Square { 5 | 6 | // Create an auxiliary constructor 7 | 8 | // Override the area method 9 | 10 | } 11 | -------------------------------------------------------------------------------- /oo-traits/src/main/scala/scalaexamples/traits/Element.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.traits 2 | 3 | // Make this class Ordered! 4 | class Element(val anIntValue: Int) { 5 | 6 | // def compare(other: Element) = 7 | 8 | override def toString = "anIntValue is " + anIntValue 9 | 10 | } 11 | 12 | trait InterfaceTrait { 13 | 14 | def getSomeString: String 15 | 16 | } 17 | 18 | trait MethodTrait { 19 | 20 | def mixedInMethod = "a value" 21 | 22 | } 23 | 24 | trait SubClassTrait extends Element { 25 | 26 | override def toString = "stacked toString, super toString is '" + super.toString + "'" 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /oo-traits/src/test/scala/scalaexamples/companionobject/CompanionObjectTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.companionobject 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class CompanionObjectTest extends EmptyTest{ 11 | 12 | @Test def companionObjectTest { 13 | // Comment in expression by expression and make them compile and run 14 | // Hint: You do this by changing the Train.scala definition. More hints in the source file. 15 | 16 | // val trainOne = Train(Array("Espen")) 17 | // assertEquals(List("Espen"), trainOne.passengers) 18 | // assertTrue(trainOne.conductor.isEmpty) 19 | 20 | // val trainTwo = Train("Per", "Pål") 21 | // assertEquals(List("Per", "Pål"), trainTwo.passengers) 22 | // assertTrue(trainTwo.conductor.isEmpty) 23 | 24 | // val trainObject = Train 25 | // val trainThree = trainObject(Array("Espen")) 26 | // assertEquals(List("Espen"), trainThree.passengers) 27 | // assertTrue(trainThree.conductor.isEmpty) 28 | 29 | // val trainFour = trainObject.apply(Some("conductor"), "Espen") 30 | // assertEquals(List("Espen"), trainFour.passengers) 31 | // assertTrue(trainFour.conductor.isDefined) 32 | 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /oo-traits/src/test/scala/scalaexamples/inheritance/ShapeTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.inheritance 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | 8 | import scala.math._ 9 | import scalaexamples.EmptyTest 10 | 11 | @RunWith(classOf[JUnit4]) 12 | class ShapeTest extends EmptyTest { 13 | 14 | val rectangle = new Rectangle(4.0, 10.0, Shape.DefaultColor) 15 | val circle = new Circle 16 | val square = new Square 17 | 18 | val shapes = List(rectangle, circle, square) 19 | 20 | @Test 21 | def testArea { 22 | assertEquals(40.0, rectangle.area) 23 | // Make this compile and run 24 | // assertEquals(Pi * 3.0 * 3.0, circle.area) 25 | // assertEquals(4.0, square.area) 26 | } 27 | 28 | @Test 29 | def testCircumference { 30 | assertEquals(28.0, rectangle.circumference) 31 | // Make this compile and run 32 | // assertEquals(2 * Pi * 3.0, circle.circumference) 33 | // assertEquals(8.0, square.circumference) 34 | } 35 | 36 | @Test 37 | def testColor { 38 | // Make this compile and run 39 | // shapes foreach { shape => assertEquals(Shape.DefaultColor, shape.color) } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /oo-traits/src/test/scala/scalaexamples/traits/BasicTraitTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.traits 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | 8 | import util.Sorting 9 | import scalaexamples.EmptyTest 10 | 11 | @RunWith(classOf[JUnit4]) 12 | class BasicTraitTest extends EmptyTest { 13 | 14 | val list = List(new Element(10), new Element(2)) 15 | 16 | @Test def mixInOrderedTraitTest { 17 | // Make the following compile (and run of course) 18 | // Hint implement the Ordered interface on the Element class 19 | 20 | // assertTrue((list(0) compare list(1)) > 0) 21 | // assertTrue(list(0) > list(1)) 22 | // assertTrue(list(0) >= list(1)) 23 | // assertFalse(list(0) < list(1)) 24 | // assertFalse(list(0) <= list(1)) 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /oo-traits/src/test/scala/scalaexamples/traits/DynamicMixinTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.traits 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class DynamicMixinTest extends EmptyTest { 11 | 12 | // @Test 13 | def mixInTraitTest { 14 | val myElement = new Element(0) 15 | 16 | // Make the following compile and run. 17 | // Hint this is easy to achieve by mixing in the correct 18 | // trait right here when creating the instance of Element. 19 | 20 | // assertEquals("a value", myElement.mixedInMethod) 21 | val anotherElement = new Element(0) with MethodTrait 22 | assertEquals("another value", anotherElement.mixedInMethod) 23 | } 24 | 25 | 26 | // @Test 27 | def mixInInterfaceTest { 28 | // Make the following compile and run (uncomment the code) 29 | // Hint you can open the class or subclass it right here. 30 | 31 | // val myElement = new Element(0) with InterfaceTrait 32 | // assertNotNull(myElement.getSomeString) 33 | } 34 | 35 | 36 | // @Test 37 | def stackableTraitTest { 38 | // Make the following run without assertion error. 39 | // Hint: extend ElementWithStackableTrait with Element and SubClassTrait 40 | class ElementWithStackableTrait(value: Int) 41 | val element = new ElementWithStackableTrait(1) 42 | assertEquals("stacked toString, super toString is 'anIntValue is 1'", element.toString) 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /pattern-matching/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | 9 | pattern-matching 10 | 0.1-SNAPSHOT 11 | jar 12 | Scala Examples - Pattern matching 13 | 2009 14 | 15 | 16 | no.scalabin.scala-examples 17 | common 18 | ${project.version} 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /pattern-matching/src/main/scala/scalaexamples/patternmatching/MyCaseClass.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.patternmatching 2 | 3 | sealed abstract class MyCaseClass 4 | case class FirstSubClass(value: Int) extends MyCaseClass 5 | case class SecondSubClass(value: String)extends MyCaseClass 6 | case class ThirdSubClass(value: String, elements: List[Int]) extends MyCaseClass 7 | case class FourthSubClass(value: String, element: FirstSubClass) extends MyCaseClass 8 | -------------------------------------------------------------------------------- /pattern-matching/src/test/scala/scalaexamples/patternmatching/ListMatchingTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.patternmatching 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class ListMatchingTest extends EmptyTest { 11 | 12 | val list = List("Scala", "is", "powerful") 13 | 14 | // @Test 15 | def matchFirstElementOfList { 16 | 17 | // matchedElement must find the first element. It can also skip the rest (if you want to) 18 | val mathedElement = list match { 19 | // Insert you match statement(s) here 20 | case _ => "failed" 21 | } 22 | 23 | assertEquals(list(0), mathedElement) 24 | } 25 | 26 | // @Test 27 | def matchSecondElementOfList { 28 | 29 | // matchedElement should find the second element of the list. 30 | // You may ignore the first element and any subsequent elements if you want 31 | val mathedElement = list match { 32 | // Insert you match statement here 33 | case _ => "failed" 34 | } 35 | 36 | assertEquals(list(1), mathedElement) 37 | } 38 | 39 | 40 | // @Test 41 | def matchNestedLists { 42 | val nestedList = list :: List("Indeed", "it", "is") 43 | // Same as List(List(Scala, is, powerful), Indeed, it, is) 44 | 45 | // A side note here. If you want only one list use "list ::: List("Indeed", "it", "is")" 46 | // Which prepends the entire list. 47 | 48 | // You must find the sublist to make the test pass. 49 | val mathedElement = nestedList match { 50 | // Insert you match statement here 51 | case _ => "failed" 52 | } 53 | 54 | assertEquals(list, mathedElement) 55 | } 56 | 57 | // @Test 58 | def matchNestedElementOfList { 59 | val subList = List("Indeed", "it", "is") 60 | val list = List("Scala", "is", "powerful", subList) 61 | 62 | // Here you must find the first element of the second sublist 63 | val mathedElement = list match { 64 | // Insert you match statement here 65 | case _ => "failed" 66 | } 67 | assertEquals(subList(0), mathedElement) 68 | } 69 | 70 | // @Test 71 | def matchNestedElementOfListWithHeadTailPatternMatching { 72 | val subList = List("Indeed", "it", "is") 73 | val list = List("Scala", "is", "powerful", subList) 74 | 75 | // Here you must find the first element of the second sublist 76 | // like in the example above but using head tail pattern matching 77 | def findSublist(listOfList:List[Any]): String = listOfList match { 78 | // Insert you match statement here 79 | case _ => "failed" 80 | } 81 | val mathedElement = findSublist(list) 82 | 83 | assertEquals(subList(0), mathedElement) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /pattern-matching/src/test/scala/scalaexamples/patternmatching/MyCaseClassMatchingTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.patternmatching 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class MyCaseClassMatchingTest extends EmptyTest { 11 | 12 | // @Test 13 | def matchMySuperType { 14 | val theClass: Any = FirstSubClass(1) 15 | 16 | val found = theClass match { 17 | // Add a match expression which return true 18 | case _ => false 19 | } 20 | assertTrue(found) 21 | } 22 | 23 | // @Test 24 | def matchSubType { 25 | 26 | def matchSubType(myType: MyCaseClass) = myType match { 27 | // Add match expressions which make the following code pass. 28 | case _ => sys.error("Should never reach this") 29 | } 30 | 31 | // You should not change the following code at all. Only add matches which will make the tests match. 32 | var theClass: MyCaseClass = FirstSubClass(10) 33 | var foundElement = matchSubType(theClass) 34 | assertEquals(10, foundElement) 35 | 36 | theClass = SecondSubClass("verdi") 37 | foundElement = matchSubType(theClass) 38 | assertEquals("verdi", foundElement) 39 | 40 | theClass = ThirdSubClass("verdi", List(1, 2)) 41 | foundElement = matchSubType(theClass) 42 | assertEquals(List(1, 2), foundElement) 43 | 44 | theClass = FourthSubClass("verdi", FirstSubClass(11)) 45 | foundElement = matchSubType(theClass) 46 | assertEquals(11, foundElement) 47 | } 48 | 49 | // @Test 50 | def matchWithExplicitType { 51 | val theClass: MyCaseClass = FourthSubClass("verdi", FirstSubClass(11)) 52 | 53 | val foundElement: Option[FirstSubClass] = theClass match { 54 | // Add a match expression which make the following assertion true. Use Option type 55 | case _ => None 56 | } 57 | 58 | assertEquals(FirstSubClass(11), foundElement.get) 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /pattern-matching/src/test/scala/scalaexamples/patternmatching/RegexMatchingTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.patternmatching 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class RegexMatchingTest extends EmptyTest { 11 | 12 | // @Test 13 | def matchEntireString { 14 | val string = "This is the string to find a match for." 15 | 16 | // "(string)".r makes a Scala regular expression, which may be used in matching. 17 | // The syntax to use it in a match expression is 18 | // 19 | // case myregex(one) => ... 20 | // 21 | // The match extracts the contents within the 22 | // capturing parentheses () in the regular expression. 23 | val matchRegex = "(.+)".r 24 | 25 | val mathedElement = string match { 26 | // Insert you match statement here 27 | case _ => "Failed" 28 | } 29 | 30 | assertEquals(string, mathedElement) 31 | } 32 | 33 | // @Test 34 | def matchFirstPartOfString { 35 | val start = "This is" 36 | val string = start + " the string to find a match for." 37 | val matchRegex = """^(\w+\s\w+).*""".r 38 | 39 | val mathedElement = string match { 40 | // Insert you match statement here 41 | case _ => "Failed" 42 | } 43 | 44 | assertEquals(start, mathedElement) 45 | } 46 | 47 | // @Test 48 | def matchFirstAndLastWordOfString { 49 | val start = "This" 50 | val end = "for" 51 | val string = start + " is the string to find a match " + end 52 | 53 | // Create a regular expression which will make the folliwing match and assertions correct. 54 | val matchRegex = """""".r 55 | 56 | val (first, last) = string match { 57 | case matchRegex(firstElement, lastElement) => (firstElement, lastElement) 58 | case _ => "Failed" 59 | } 60 | 61 | assertEquals(start, first) 62 | assertEquals(end, last) 63 | } 64 | 65 | // @Test 66 | def matchEmails { 67 | 68 | // Create a regex which identifies email (may be really simple) 69 | // val emailRegex = """([^@]+)@(.+)""".r 70 | // You may use the regex above. 71 | val emailRegex = """""".r 72 | 73 | def isValidEmail(stringToTest: String) = stringToTest match { 74 | // Insert you match statement here 75 | case _ => false 76 | } 77 | 78 | assertTrue(isValidEmail("aks@knowit.no")) 79 | assertTrue(isValidEmail("alf.kristian@gmail.com")) 80 | assertFalse(isValidEmail("fvr_at_knowit.no")) 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /pattern-matching/src/test/scala/scalaexamples/patternmatching/TupleTestMatchTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.patternmatching 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class TupleTestMatchTest extends EmptyTest { 11 | 12 | // @Test 13 | def matchOnTupes { 14 | val simpleTuple: Any = (1, "string") 15 | 16 | simpleTuple match { 17 | // Match on the tuple, and assert that its the same => assertEquals(simpleTuple, (one, two)) 18 | case _ => sys.error("Couldnt match it") 19 | } 20 | } 21 | 22 | // @Test 23 | def matchOnTupesWithGuard { 24 | val simpleTuple: Any = (1, "string") 25 | 26 | simpleTuple match { 27 | case (one, _) => assertEquals(2, one) 28 | // Comment in this line and make this run, (hint create guard on the previous match, so that it won't hit) 29 | // case (one, _) => assertEquals(1, one) 30 | case _ => sys.error("Couldnt match it") 31 | } 32 | } 33 | 34 | // @Test 35 | def matchOnLargerTuple{ 36 | val simpleTuple: Any = (1, "string", false) 37 | 38 | simpleTuple match { 39 | // Match on the tuple, and assert with assertFalse(three) 40 | case (one, two, three: String) => sys.error("this is not a Tuple3 with last element Boolean") 41 | case _ => sys.error("Couldnt match it") 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /pattern-matching/src/test/scala/scalaexamples/patternmatching/TypeMatchingTest.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.patternmatching 2 | 3 | import junit.framework.Assert._ 4 | import org.junit.Test 5 | import org.junit.runner.RunWith 6 | import org.junit.runners.JUnit4 7 | import scalaexamples.EmptyTest 8 | 9 | @RunWith(classOf[JUnit4]) 10 | class TypeMatchingTest extends EmptyTest { 11 | 12 | // @Test 13 | def matchOnType { 14 | val elements = List(23, "Hello", 8.5, 'q') 15 | for (element <- elements) { 16 | element match { 17 | // Make test run, and assert on types, e.g. " => assertEquals(23, integer)" or " => assertEquals("Hello", s)" 18 | // Insert your matching code here 19 | case other => sys.error("Should match other on type") 20 | } 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | pom 9 | Scala examples - Testing your Scala skills 10 | 2009 11 | 12 | 13 | 2.10.2 14 | 15 | 16 | 17 | 18 | scala-tools.org 19 | Scala-Tools Maven2 Repository 20 | http://scala-tools.org/repo-releases 21 | 22 | false 23 | 24 | 25 | 26 | 27 | 28 | 29 | scala-tools.org 30 | Scala-Tools Maven2 Repository 31 | http://scala-tools.org/repo-releases 32 | 33 | 34 | 35 | 36 | 37 | junit 38 | junit 39 | 4.11 40 | 41 | 42 | org.scala-lang 43 | scala-library 44 | ${scala.version} 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.scala-lang 53 | scala-swing 54 | ${scala.version} 55 | 56 | 57 | 58 | 59 | 60 | 61 | src/main/scala 62 | src/test/scala 63 | 64 | 65 | net.alchim31.maven 66 | scala-maven-plugin 67 | 68 | 69 | 70 | 71 | 72 | net.alchim31.maven 73 | scala-maven-plugin 74 | 3.1.5 75 | 76 | 77 | 78 | compile 79 | testCompile 80 | 81 | 82 | 83 | 84 | ${scala.version} 85 | 86 | -unchecked 87 | -feature 88 | -deprecation 89 | 90 | 91 | 92 | 93 | maven-eclipse-plugin 94 | 95 | true 96 | 97 | ch.epfl.lamp.sdt.launching.SCALA_CONTAINER 98 | org.eclipse.jdt.launching.JRE_CONTAINER 99 | 100 | 101 | ch.epfl.lamp.sdt.core.scalanature 102 | org.eclipse.jdt.core.javanature 103 | 104 | 105 | ch.epfl.lamp.sdt.core.scalabuilder 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | common 115 | intro 116 | first-class-functions 117 | higher-order-functions 118 | oo-traits 119 | pattern-matching 120 | quiz 121 | 122 | 123 | 124 | 125 | 126 | org.scala-tools 127 | maven-scala-plugin 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /project/ScalaTraining.scala: -------------------------------------------------------------------------------- 1 | import sbt._ 2 | import Keys._ 3 | 4 | object BuildSettings { 5 | val junit = "junit" % "junit" % "4.8.1" 6 | val junitInterface = "com.novocode" % "junit-interface" % "0.7" % "test->default" 7 | 8 | val globalSettings = Seq( 9 | organization := "no.scalabin.scala-examples", 10 | version := "0.1-SNAPSHOT", 11 | scalaVersion := "2.10.2", 12 | scalacOptions += "-deprecation", 13 | scalacOptions += "-feature", 14 | libraryDependencies ++= Seq(junit, junitInterface) 15 | ) 16 | val projectSettings = Defaults.defaultSettings ++ globalSettings 17 | } 18 | 19 | object ScalaTraining extends Build { 20 | import BuildSettings._ 21 | 22 | lazy val root = Project("root", file(".")) aggregate (common, intro, first_class_functions, higher_order_functions, oo_traits, pattern_matching, quiz) 23 | 24 | lazy val common = Project("common", file("common"), settings = projectSettings) 25 | lazy val intro = Project("intro", file("intro"), settings = projectSettings) dependsOn (common) 26 | lazy val first_class_functions = Project("first_class_functions", file("first-class-functions"), settings = projectSettings) dependsOn (common) 27 | lazy val higher_order_functions = Project("higher_order_functions", file("higher-order-functions"), settings = projectSettings) dependsOn (common) 28 | lazy val oo_traits = Project("oo_traits", file("oo-traits"), settings = projectSettings) dependsOn (common) 29 | lazy val pattern_matching = Project("pattern_matching", file("pattern-matching"), settings = projectSettings) dependsOn (common) 30 | 31 | lazy val quiz = Project("quiz", file("quiz"), 32 | settings = projectSettings ++ Seq(libraryDependencies += "org.scala-lang" % "scala-swing" % "2.9.1") 33 | ) 34 | } -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.12.4 2 | -------------------------------------------------------------------------------- /quiz/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | no.scalabin.scala-examples 6 | scala-examples 7 | 0.1-SNAPSHOT 8 | 9 | quiz 10 | 0.1-SNAPSHOT 11 | jar 12 | Scala Examples - Quiz game 13 | 2009 14 | 15 | 16 | 17 | org.scala-lang 18 | scala-swing 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.codehaus.mojo 26 | exec-maven-plugin 27 | 1.1 28 | 29 | 30 | 31 | java 32 | 33 | 34 | 35 | 36 | scalaexamples.quiz.QuizGame 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /quiz/src/main/resources/quiz.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | The Example Quiz 4 | 5 | 6 | In which continent is the country Japan located? 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Which one cannot swim? 18 | 19 | 20 | How many points are on a hexagon? 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /quiz/src/main/scala/scalaexamples/quiz/MockQuizProvider.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.quiz 2 | 3 | /** 4 | * Only use this if you think you have spent too much time solving the XML parsing problem. 5 | */ 6 | object MockQuiz extends QuizProvider { 7 | 8 | val quiz = 9 | Quiz("The Example Quiz", 10 | List(Question("In which continent is the country Japan located?", 11 | List(Answer("Asia",true), 12 | Answer("Europe",false), 13 | Answer("Africa",false), 14 | Answer("America",false) 15 | ) 16 | ), 17 | Question("Which one cannot swim?", 18 | List(Answer("Tuna",true), 19 | Answer("Cow",false), 20 | Answer("Whale",false), 21 | Answer("Lobster",false) 22 | ) 23 | ) 24 | ) 25 | ) 26 | 27 | } 28 | -------------------------------------------------------------------------------- /quiz/src/main/scala/scalaexamples/quiz/Model.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.quiz 2 | 3 | case class Quiz(title: String, questions: List[Question]) 4 | case class Question(text: String, answers: List[Answer]) 5 | case class Answer(text: String, correct: Boolean) -------------------------------------------------------------------------------- /quiz/src/main/scala/scalaexamples/quiz/QuizGame.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.quiz 2 | 3 | import xml._ 4 | 5 | /** 6 | * Main class. Parse XML and start the quiz type of your choice. 7 | */ 8 | object QuizGame { 9 | 10 | def main(args: Array[String]) { 11 | // val quiz = MockQuiz.quiz 12 | val quiz = new XmlQuizProvider("quiz.xml").quiz 13 | new console.ConsoleGame(quiz) 14 | // new swing.SwingGame(quiz) 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /quiz/src/main/scala/scalaexamples/quiz/QuizProvider.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.quiz 2 | 3 | trait QuizProvider { 4 | 5 | def quiz: Quiz 6 | 7 | } 8 | -------------------------------------------------------------------------------- /quiz/src/main/scala/scalaexamples/quiz/XmlQuizProvider.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.quiz 2 | 3 | import xml._ 4 | 5 | /** 6 | * This class parses the xml from a file. 7 | */ 8 | class XmlQuizProvider(file: String) extends QuizProvider { 9 | 10 | def quiz = { 11 | val xmlFromFile = XML.load(getClass.getClassLoader.getResourceAsStream(file)) 12 | parseXml(xmlFromFile) 13 | } 14 | 15 | /** 16 | * TODO: Implement parsing of the XML into a Quiz object with questions and answers 17 | */ 18 | private def parseXml(xml: Elem): Quiz = sys.error("Not implemented") 19 | 20 | } 21 | -------------------------------------------------------------------------------- /quiz/src/main/scala/scalaexamples/quiz/console/ConsoleGame.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.quiz.console 2 | 3 | import scalaexamples.quiz.{Answer, Question, Quiz} 4 | 5 | class ConsoleGame(quiz: Quiz) { 6 | 7 | println("Welcome to our faboulus quiz called '" + quiz.title + "'") 8 | 9 | // TODO: Implement quiz game UI and logic 10 | 11 | } 12 | -------------------------------------------------------------------------------- /quiz/src/main/scala/scalaexamples/quiz/swing/SwingGame.scala: -------------------------------------------------------------------------------- 1 | package scalaexamples.quiz.swing 2 | 3 | import scala.swing._ 4 | import scala.swing.Swing._ 5 | import scalaexamples.quiz.Quiz 6 | import scala.language.reflectiveCalls 7 | 8 | class SwingGame(quiz: Quiz) extends MainFrame { 9 | 10 | // TODO: Implement quiz game UI and logic 11 | 12 | size = (300, 200) 13 | setLocationRelativeTo(this) 14 | title = quiz.title 15 | visible = true 16 | 17 | } 18 | --------------------------------------------------------------------------------