├── .gitignore ├── ABitofStyle ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-4.scala └── run.bat ├── ALittleReflection ├── Name.scala ├── Name2.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala └── run.bat ├── AbstractClasses ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── Applications ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala └── run.bat ├── AtomicTest.scala ├── AutomaticStringConversion ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── AuxiliaryConstructors ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-4.scala └── run.bat ├── BaseClassInitialization ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8a.scala ├── Solution-8b.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala └── run.bat ├── Brevity ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── BuildingSystemswithTraits ├── SodaFountain.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4-FirstEdition.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── CaseClasses ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-6.scala └── run.bat ├── ClassArguments ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8.scala ├── Solution-9.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-8.scala ├── Starter-9.scala └── run.bat ├── ClassExercises ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6.scala └── run.bat ├── ClassesAndObjects ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala └── run.bat ├── CombiningSequenceswithzip ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala └── run.bat ├── CompanionObjects ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── Composition ├── Solution-1.scala ├── Solution-1_1stEdition.scala ├── Solution-2.scala ├── Solution-2_1stEdition.scala ├── Solution-3.scala ├── Solution-3_1stEdition.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-1_1stEdition.scala ├── Starter-2.scala ├── Starter-2_1stEdition.scala ├── Starter-3.scala ├── Starter-3_1stEdition.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6.scala └── run.bat ├── CompoundExpressions ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── Comprehensions ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-5.scala ├── Starter-6.scala └── run.bat ├── ConditionalExpressions ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala └── run.bat ├── Constructors ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── ConstructorsAndExceptions ├── CodeListing.scala ├── CodeListingTester.scala ├── Solution-1.scala ├── Solution-2a.scala ├── Solution-2b.scala ├── Solution-3.scala └── run.bat ├── ConvertingExceptionswithTry ├── CodeListing.scala ├── Errors.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala └── run.bat ├── CopyRight.txt ├── CreatingClasses ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala └── run.bat ├── CustomErrorReporting ├── CodeListing.scala ├── CodeListingTester.scala ├── Fail.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-3.scala └── run.bat ├── DataTypes ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8.scala └── run.bat ├── DefiningOperators ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── DesignbyContract ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3a.scala ├── Solution-3b.scala ├── Solution-3c.scala └── run.bat ├── Enumerations ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-6.scala └── run.bat ├── ErrorHandlingwithExceptions ├── Solution-1.scala ├── Solution-10.scala ├── Solution-2a.scala ├── Solution-2b.scala ├── Solution-3a.scala ├── Solution-3b.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8.scala ├── Solution-9.scala └── run.bat ├── ErrorReportingwithEither ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── EvaluationOrder ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala └── run.bat ├── Expressions ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala └── run.bat ├── ExtensibleSystemswithTypeClasses ├── Solution-1a.scala ├── Solution-1b.scala ├── Solution-2a.scala ├── Solution-2b.scala ├── Solution-3.scala ├── Solution-4a.scala ├── Solution-4b.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7a.scala ├── Solution-7b.scala └── run.bat ├── ExtensionMethods ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala └── run.bat ├── Fields ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── ForLoops ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala └── run.bat ├── FunctionsasObjects ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8.scala ├── Solution-9.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6.scala ├── Starter-7.scala ├── Starter-8.scala ├── Starter-9.scala └── run.bat ├── HandlingNon-ValueswithOption ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-4.scala └── run.bat ├── IdiomaticScala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala └── run.bat ├── ImportsAndPackages-1stEdition ├── Crest.scala ├── ImportTests.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── TheRoyalty.scala ├── Trivia.scala └── run.bat ├── ImportsAndPackages-2ndEdition ├── EquilateralTriangle.scala ├── PythagoreanTheorem.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3a.scala ├── Solution-3b.scala ├── Solution-4a.scala ├── Solution-4b.scala ├── Solution-4c.scala ├── Trivia.scala └── run.bat ├── Inheritance ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── ListsAndRecursion ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── Logging ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala └── run.bat ├── Maps ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-7b.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── Methods ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6.scala ├── Starter-7.scala └── run.bat ├── MethodsInsideClasses ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── MoreConditionals ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── NamedAndDefaultArguments ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5a.scala ├── Solution-5b.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-6.scala └── run.bat ├── Overloading ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-2.scala └── run.bat ├── OverridingMethods ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── ParameterizedTypes ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── PatternMatching ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── PatternMatchingwithCaseClasses ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── PatternMatchingwithTuples ├── PaintColors.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── PatternMatchingwithTypes ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala └── run.bat ├── Polymorphism ├── Name.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5-1stEdition.scala ├── Solution-5.scala ├── Solution-6-1stEdition.scala ├── Solution-6.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6-1stEdition.scala ├── Starter-6.scala └── run.bat ├── README.md ├── ReachingintoJava ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Starter-1.scala ├── Starter-4.scala ├── Starter-5.scala └── run.bat ├── ReferencesAndMutability ├── Solution-1a.scala ├── Solution-1b.scala ├── Solution-1c.scala ├── Solution-1d.scala ├── Solution-2a.scala ├── Solution-2b.scala ├── Solution-3a.scala ├── Solution-3b.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala └── run.bat ├── Sequences ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6.scala └── run.bat ├── Sets ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── StringInterpolation ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── Summary1 ├── Solution-1.scala ├── Solution-10.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8.scala ├── Solution-9.scala └── run.bat ├── Summary2 ├── BasicMethods.scala ├── ClassBodies.scala ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8.scala └── run.bat ├── TaggingTraitsAndCaseObjects ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-2.scala └── run.bat ├── Testing ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Starter-5.scala └── run.bat ├── Traits ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala └── run.bat ├── Tuples ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala └── run.bat ├── TypeParameterConstraints ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3-FirstEdition.scala ├── Solution-3a.scala ├── Solution-3b.scala ├── Solution-3c.scala ├── Solution-3d.scala └── run.bat ├── UniformAccessAndSetters ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala └── run.bat ├── UsingTraits ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-2.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6.scala └── run.bat ├── Values ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala └── run.bat ├── Variables ├── Solution-1a.scala ├── Solution-1b.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala └── run.bat ├── Vectors ├── Solution-1.scala ├── Solution-10.scala ├── Solution-11.scala ├── Solution-12.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Solution-7.scala ├── Solution-8.scala ├── Solution-9.scala ├── Starter-3.scala ├── Starter-4.scala ├── Starter-5.scala ├── Starter-6.scala └── run.bat ├── ZZZ_BuildTools ├── Autosave.py ├── Default (Windows).sublime-keymap ├── FindingStraightQuotesInWord.txt ├── FormatCodeForExercise.py ├── InsertOutput.py ├── Notes.txt ├── OUTPUT_SHOULD_BE.sublime-macro ├── OUTPUT_SHOULD_CONTAIN.sublime-macro ├── OldSuccessfullyRun.py ├── SuccessfullyRun - Copy.py ├── SuccessfullyRun.py ├── atomic-scala-solutions.sublime-build ├── colorama-test.py ├── copyrun.bat ├── fixunicode.bat ├── runner.bat ├── snuggleColons.bat ├── solutionDirs.py ├── stripTrailingLines.bat └── termcolor-test.py ├── _.bat ├── libs └── commons-math3-3.3.jar ├── mapAndreduce ├── Solution-1.scala ├── Solution-2.scala ├── Solution-3.scala ├── Solution-4.scala ├── Solution-5.scala ├── Solution-6.scala ├── Starter-1.scala ├── Starter-6.scala └── run.bat └── run.bat /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.out 3 | *.err 4 | *.pyc 5 | _AtomicTestErrors.txt 6 | tmp.txt 7 | results.txt 8 | Succeeded.txt 9 | trace.tst 10 | Logging/AtomicLog* 11 | -------------------------------------------------------------------------------- /ABitofStyle/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "A Bit of Style" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Exclaim(val s:String) { 6 | def parens() = s + "!" 7 | def noParens = s + "!" 8 | } 9 | 10 | val e = new Exclaim("yes") 11 | e.noParens is "yes!" 12 | e.parens() is "yes!" 13 | 14 | /* OUTPUT_SHOULD_BE 15 | yes! 16 | yes! 17 | */ 18 | -------------------------------------------------------------------------------- /ABitofStyle/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "A Bit of Style" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Exclaim2(val s:String) { 6 | def parens() = s + "!" 7 | val noParens = s + "!" 8 | } 9 | 10 | val e2 = new Exclaim2("yes") 11 | e2.noParens is "yes!" 12 | e2.parens() is "yes!" 13 | 14 | /* OUTPUT_SHOULD_BE 15 | yes! 16 | yes! 17 | */ 18 | -------------------------------------------------------------------------------- /ABitofStyle/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "A Bit of Style" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Exclaim3(val s:String) { 6 | def noParens = s + "!" 7 | } 8 | 9 | val e3 = new Exclaim3("big yes") 10 | e3.noParens is "big yes!" 11 | 12 | /* OUTPUT_SHOULD_BE 13 | big yes! 14 | */ 15 | -------------------------------------------------------------------------------- /ABitofStyle/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "A Bit of Style" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Exclaim4(val s:String) { 6 | var count = 0 7 | def exclaim() = { 8 | count = count + 1 9 | s + "!" 10 | } 11 | } 12 | 13 | val e4 = new Exclaim4("counting") 14 | 15 | e4.exclaim() is "counting!" 16 | e4.exclaim() is "counting!" 17 | e4.count is 2 18 | 19 | /* OUTPUT_SHOULD_BE 20 | counting! 21 | counting! 22 | 2 23 | */ 24 | -------------------------------------------------------------------------------- /ABitofStyle/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "A Bit of Style" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val e = new Exclaim("yes") 6 | e.noParens is "yes!" 7 | e.parens() is "yes!" 8 | -------------------------------------------------------------------------------- /ABitofStyle/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "A Bit of Style" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val e2 = new Exclaim2("yes") 6 | e2.noParens is "yes!" 7 | e2.parens() is "yes!" 8 | -------------------------------------------------------------------------------- /ABitofStyle/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "A Bit of Style" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val e4 = new Exclaim4("counting") 6 | // Call exclamation method 7 | // Call exclamation method again 8 | e4.count is 2 9 | -------------------------------------------------------------------------------- /ABitofStyle/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ALittleReflection/Name.scala: -------------------------------------------------------------------------------- 1 | // Name.scala 2 | package com.atomicscala 3 | import reflect.runtime.currentMirror 4 | 5 | object Name { 6 | def className(o:Any) = 7 | currentMirror.reflect(o).symbol. 8 | toString.replace('$', ' ').split(' ').last 9 | } 10 | 11 | trait Name { 12 | override def toString = Name.className(this) 13 | } 14 | -------------------------------------------------------------------------------- /ALittleReflection/Name2.scala: -------------------------------------------------------------------------------- 1 | // Name2.scala 2 | package com.atomicscala 3 | import reflect.runtime.currentMirror 4 | 5 | object Name2 { 6 | def className(o:Any) = 7 | currentMirror.reflect(o).symbol. 8 | toString 9 | } 10 | 11 | trait Name2 { 12 | override def toString = Name2.className(this) 13 | } 14 | -------------------------------------------------------------------------------- /ALittleReflection/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "A Little Reflection" 3 | import com.atomicscala.AtomicTest._ 4 | import com.atomicscala.Name 5 | 6 | class ReflectedToy2(name:String, batteryOperated:Boolean) extends Name 7 | 8 | val reflected2 = new ReflectedToy2("Flamingo", false) 9 | 10 | reflected2 is "ReflectedToy2" 11 | 12 | /* OUTPUT_SHOULD_BE 13 | ReflectedToy2 14 | */ 15 | -------------------------------------------------------------------------------- /ALittleReflection/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "A Little Reflection" 3 | import com.atomicscala.AtomicTest._ 4 | import com.atomicscala.Name2 5 | 6 | case class ReflectedToy3(name:String, batteryOperated:Boolean) extends Name2 7 | 8 | val reflected3 = new ReflectedToy3("Flamingo", false) 9 | 10 | reflected3 is "class $ReflectedToy3" 11 | 12 | /* OUTPUT_SHOULD_BE 13 | class $ReflectedToy3 14 | */ 15 | -------------------------------------------------------------------------------- /ALittleReflection/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /AbstractClasses/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Abstract Classes" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | abstract class Adder(x:Int) { 6 | def add(y:Int):Int 7 | } 8 | 9 | class NumericAdder(x:Int) extends Adder(x) { 10 | def add(y:Int):Int = x + y 11 | } 12 | 13 | val num = new NumericAdder(5) 14 | num.add(10) is 15 15 | 16 | /* OUTPUT_SHOULD_BE 17 | 15 18 | */ 19 | -------------------------------------------------------------------------------- /AbstractClasses/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Abstract Classes" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | abstract class Adder(x:Int) { 6 | def add(y:Int):Int 7 | } 8 | 9 | case class MyAdder(x:Int) extends Adder(x) { 10 | def add(y:Int):Int = x+y 11 | } 12 | 13 | val myNum = MyAdder(15) 14 | myNum.add(20) is 35 15 | println(myNum) 16 | 17 | /* OUTPUT_SHOULD_BE 18 | 35 19 | MyAdder(15) 20 | */ 21 | -------------------------------------------------------------------------------- /AbstractClasses/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Abstract Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val duck = new Duck 6 | duck.food is "plants" 7 | val cow = new Cow 8 | cow.food is "grass" 9 | -------------------------------------------------------------------------------- /AbstractClasses/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Abstract Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val chicken = new Chicken 6 | chicken.food is "insects" 7 | val pig = new Pig 8 | pig.food is "anything" 9 | -------------------------------------------------------------------------------- /AbstractClasses/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Abstract Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class NumericAdder(val x:Int) extends Adder(x) { 6 | def add(y:Int):Int = // Complete this 7 | } 8 | 9 | val num = new NumericAdder(5) 10 | num.add(10) is 15 11 | -------------------------------------------------------------------------------- /AbstractClasses/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Applications/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Applications" 3 | // Compile, then run with 4 | // > scala WhenAmI 5 | import com.atomicscala.AtomicTest._ 6 | 7 | object WhenAmI extends App { 8 | hi 9 | println(new java.util.Date()) 10 | def hi = println("Hello! It's:") 11 | } 12 | 13 | /* OUTPUT_SHOULD_CONTAIN 14 | Hello! It's: 15 | */ 16 | -------------------------------------------------------------------------------- /Applications/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /AutomaticStringConversion/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Automatic String Conversion" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Bicycle(riders:Int) { 6 | override def toString = "Bicycle built for 2" 7 | } 8 | 9 | val forTwo = Bicycle(2) 10 | forTwo is "Bicycle built for 2" 11 | 12 | /* OUTPUT_SHOULD_BE 13 | Bicycle built for 2 14 | */ 15 | -------------------------------------------------------------------------------- /AutomaticStringConversion/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Automatic String Conversion" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val forTwo = Bicycle(2) 6 | forTwo is "Bicycle built for 2" 7 | -------------------------------------------------------------------------------- /AutomaticStringConversion/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Automatic String Conversion" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val c1 = Cycle(1) 6 | c1 is "Unicycle" 7 | val c2 = Cycle(2) 8 | c2 is "Bicycle" 9 | val cn = Cycle(5) 10 | cn is "Cycle with 5 wheels" 11 | -------------------------------------------------------------------------------- /AutomaticStringConversion/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Automatic String Conversion" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | Cycle(-2) is "That's not a cycle!" 6 | -------------------------------------------------------------------------------- /AutomaticStringConversion/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /AuxiliaryConstructors/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Auxiliary Constructors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | // You can't put anything before the call to the primary constructor 6 | 7 | class TryMe(val name:String) { 8 | def this() = { 9 | // Can't even put a val definition: 10 | val someVal = 0 11 | this("Default Name") 12 | } 13 | } 14 | 15 | /* OUTPUT_SHOULD_CONTAIN 16 | error: 'this' expected but 'val' found. 17 | val someVal = 0 18 | ^ 19 | one error found 20 | */ 21 | -------------------------------------------------------------------------------- /AuxiliaryConstructors/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Auxiliary Constructors" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val washer = new ClothesWasher3("LG 100", 3.6) 6 | washer.wash(2, 1) is "Wash used 2 bleach and 1 fabric softener" 7 | washer.wash() is "Simple wash" 8 | -------------------------------------------------------------------------------- /AuxiliaryConstructors/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /BaseClassInitialization/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "Base Class Initialization" 3 | 4 | println("The real benefit appears in the CarTrip class") 5 | 6 | /* OUTPUT_SHOULD_BE 7 | The real benefit appears in the CarTrip class 8 | */ 9 | -------------------------------------------------------------------------------- /BaseClassInitialization/Solution-7.scala: -------------------------------------------------------------------------------- 1 | // Solution-7.scala 2 | // Solution to Exercise 7 in "Base Class Initialization" 3 | 4 | println("You could pass parameters into the constructor, but that would pollute the class.") 5 | 6 | /* OUTPUT_SHOULD_BE 7 | You could pass parameters into the constructor, but that would pollute the class. 8 | */ 9 | -------------------------------------------------------------------------------- /BaseClassInitialization/Solution-8a.scala: -------------------------------------------------------------------------------- 1 | // Solution-8a.scala 2 | // Solution to Exercise 8 in "Base Class Initialization" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Teacher(name:String) 6 | 7 | // You can inherit an ordinary class from a case class: 8 | class StudentTeacher(name:String) extends Teacher(name) 9 | 10 | val st = new StudentTeacher("Fran") 11 | st is "Teacher(Fran)" 12 | 13 | /* OUTPUT_SHOULD_BE 14 | Teacher(Fran) 15 | */ 16 | -------------------------------------------------------------------------------- /BaseClassInitialization/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Base Class Initialization" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val h = new Home 6 | h.toString is "Where the heart is" 7 | h.heart is true 8 | -------------------------------------------------------------------------------- /BaseClassInitialization/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Base Class Initialization" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v = new VacationHouse("MI", "49431", 6, 8) 6 | v is "Rented house in MI for months of " + 7 | "June through August." 8 | -------------------------------------------------------------------------------- /BaseClassInitialization/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Brevity/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Brevity" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def assignResult(arg:Boolean):Int = { 6 | if(arg) 42 else 47 7 | } 8 | 9 | assignResult(true) is 42 10 | assignResult(false) is 47 11 | 12 | /* OUTPUT_SHOULD_BE 13 | 42 14 | 47 15 | */ 16 | -------------------------------------------------------------------------------- /Brevity/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Brevity" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def assignResult2(arg:Boolean):Int = 6 | if(arg) 42 else 47 7 | 8 | assignResult2(true) is 42 9 | assignResult2(false) is 47 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 42 13 | 47 14 | */ 15 | -------------------------------------------------------------------------------- /Brevity/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Brevity" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def assignResult3(arg:Boolean) = 6 | if(arg) 42 else 47 7 | 8 | assignResult3(true) is 42 9 | assignResult3(false) is 47 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 42 13 | 47 14 | */ 15 | -------------------------------------------------------------------------------- /Brevity/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Brevity" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def assignResult(arg:Boolean):Int = { 6 | val result = if(arg) 42 else 47 7 | result 8 | } 9 | 10 | assignResult(true) is 42 11 | assignResult(false) is 47 12 | -------------------------------------------------------------------------------- /Brevity/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Brevity" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | assignResult2(true) is 42 6 | assignResult2(false) is 47 7 | -------------------------------------------------------------------------------- /Brevity/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Brevity" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | assignResult3(true) is 42 6 | assignResult3(false) is 47 7 | -------------------------------------------------------------------------------- /Brevity/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /BuildingSystemswithTraits/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Building Systems with Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | Coffee(Single, Caf, Here, Skim, Choc) is 6 | "Coffee(Single,Caf,Here,Skim,Choc)" 7 | Coffee(Double, Caf, Here, NoMilk, NoFlavor) is 8 | "Coffee(Double,Caf,Here,NoMilk,NoFlavor)" 9 | Coffee(Double, HalfCaf, ToGo, Skim, Choc) is 10 | "Coffee(Double,HalfCaf,ToGo,Skim,Choc)" 11 | -------------------------------------------------------------------------------- /BuildingSystemswithTraits/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Building Systems with Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val latte = new Latte(Single, Caf, Here, Skim) 6 | latte is "Latte(Single,Caf,Here,Skim)" 7 | val usual = new Coffee(Double, Caf, Here) 8 | usual is "Coffee(Double,Caf,Here)" 9 | -------------------------------------------------------------------------------- /BuildingSystemswithTraits/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Building Systems with Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val mocha = new Mocha(Double,Caf,ToGo,Skim) 6 | mocha is "Mocha(Double,Caf,ToGo,Skim,Choc)" 7 | -------------------------------------------------------------------------------- /BuildingSystemswithTraits/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Building Systems with Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | TakeHome(Pint, Chocolate) is 6 | "TakeHome(Pint,Chocolate)" 7 | TakeHome(Quart, Strawberry) is 8 | "TakeHome(Quart,Strawberry)" 9 | TakeHome(HalfGallon, Vanilla) is 10 | "TakeHome(HalfGallon,Vanilla)" 11 | -------------------------------------------------------------------------------- /BuildingSystemswithTraits/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /CaseClasses/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Case Classes" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Person(first:String, last:String, email:String) 6 | 7 | val p = Person("Jane", "Smile", "jane@smile.com") 8 | p.first is "Jane" 9 | p.last is "Smile" 10 | p.email is "jane@smile.com" 11 | 12 | /* OUTPUT_SHOULD_BE 13 | Jane 14 | Smile 15 | jane@smile.com 16 | */ 17 | -------------------------------------------------------------------------------- /CaseClasses/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Case Classes" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Dimension(var height:Int, var width:Int) 6 | 7 | val c = new Dimension(5,7) 8 | c.height is 5 9 | c.height = 10 10 | c.height is 10 11 | c.width = 19 12 | c.width is 19 13 | 14 | /* OUTPUT_SHOULD_BE 15 | 5 16 | 10 17 | 19 18 | */ 19 | -------------------------------------------------------------------------------- /CaseClasses/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Case Classes" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Dimension1(val height:Int, var width:Int) 6 | 7 | val c2 = new Dimension1(5,7) 8 | c2.height is 5 9 | //c2.height = 10 10 | c2.width = 19 11 | c2.width is 19 12 | 13 | /* OUTPUT_SHOULD_BE 14 | 5 15 | 19 16 | */ 17 | -------------------------------------------------------------------------------- /CaseClasses/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val p = Person("Jane", "Smile", "jane@smile.com") 6 | p.first is "Jane" 7 | p.last is "Smile" 8 | p.email is "jane@smile.com" 9 | -------------------------------------------------------------------------------- /CaseClasses/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val people = Vector( 6 | Person("Jane","Smile","jane@smile.com"), 7 | Person("Ron","House","ron@house.com"), 8 | Person("Sally","Dove","sally@dove.com")) 9 | 10 | people(0) is "Person(Jane,Smile,jane@smile.com)" 11 | people(1) is "Person(Ron,House,ron@house.com)" 12 | people(2) is "Person(Sally,Dove,sally@dove.com)" 13 | -------------------------------------------------------------------------------- /CaseClasses/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val dogs = Vector( 6 | /* Insert Vector initialization */ 7 | ) 8 | 9 | dogs(0) is "Dog(Fido,Golden Lab)" 10 | dogs(1) is "Dog(Ruff,Alaskan Malamute)" 11 | dogs(2) is "Dog(Fifi,Miniature Poodle)" 12 | -------------------------------------------------------------------------------- /CaseClasses/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val c = new Dimension(5,7) 6 | c.height is 5 7 | c.height = 10 8 | c.height is 10 9 | c.width = 19 10 | c.width is 19 11 | -------------------------------------------------------------------------------- /CaseClasses/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val anotherT1 = new SimpleTimeDefault(10, 30) 6 | val anotherT2 = new SimpleTimeDefault(9) 7 | val anotherST = anotherT1.subtract(anotherT2) 8 | 9 | anotherST.hours is 1 10 | anotherST.minutes is 30 11 | 12 | val anotherST2 = 13 | new SimpleTimeDefault(10).subtract(new SimpleTimeDefault(9, 45)) 14 | 15 | anotherST2.hours is 0 16 | anotherST2.minutes is 15 17 | -------------------------------------------------------------------------------- /CaseClasses/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ClassArguments/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Class Arguments" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class FlexibleFamily(mom:String, dad:String, kids:String*) { 6 | def familySize():Int = { 7 | var count = 2 8 | for(i<-kids) { 9 | count = count + 1 10 | } 11 | count 12 | } 13 | } 14 | 15 | val familyNoKids = new FlexibleFamily("Mom", "Dad") 16 | familyNoKids.familySize is 2 17 | 18 | /* OUTPUT_SHOULD_BE 19 | 2 20 | */ 21 | -------------------------------------------------------------------------------- /ClassArguments/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Class Arguments" 3 | 4 | // This produces an error: 5 | 6 | class Family3(parents:String*, kids:String*) { 7 | def familySize():Int = { 8 | var count = 2 9 | for(i<-kids) { 10 | count = count + 1 11 | } 12 | count 13 | } 14 | } 15 | 16 | /* OUTPUT_SHOULD_CONTAIN 17 | error: *-parameter must come last 18 | class Family3(parents:String*, kids:String*) { 19 | ^ 20 | one error found 21 | */ 22 | -------------------------------------------------------------------------------- /ClassArguments/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Class Arguments" 3 | 4 | // This produces an error: 5 | 6 | class Family4(kids:String*, mom:String, dad:String) { 7 | def familySize():Int = { 8 | var count = 2 9 | for(i<-kids) { 10 | count = count + 1 11 | } 12 | count 13 | } 14 | } 15 | 16 | /* OUTPUT_SHOULD_CONTAIN 17 | error: *-parameter must come last 18 | class Family4(kids:String*, mom:String, dad:String) { 19 | ^ 20 | one error found 21 | */ 22 | -------------------------------------------------------------------------------- /ClassArguments/Solution-7.scala: -------------------------------------------------------------------------------- 1 | // Solution-7.scala 2 | // Solution to Exercise 7 in "Class Arguments" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Cup3(var percentFull:Int) { 6 | val max = 100 7 | def add(amount:Int):Int = { 8 | percentFull += amount 9 | if(percentFull > max) { 10 | percentFull = max 11 | } 12 | percentFull 13 | } 14 | } 15 | 16 | val cup3 = new Cup3(0) 17 | cup3.percentFull = 100 18 | cup3.percentFull is 100 19 | 20 | /* OUTPUT_SHOULD_BE 21 | 100 22 | */ 23 | -------------------------------------------------------------------------------- /ClassArguments/Solution-9.scala: -------------------------------------------------------------------------------- 1 | // Solution-9.scala 2 | // Solution to Exercise 9 in "Class Arguments" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def squareThem(x:Int*):Int = { 6 | var square = 0 7 | for(i <- x) { 8 | square = square + i*i 9 | } 10 | square 11 | } 12 | 13 | squareThem(2) is 4 14 | squareThem(2, 4) is 20 15 | squareThem(1, 2, 4) is 21 16 | 17 | /* OUTPUT_SHOULD_BE 18 | 4 19 | 20 20 | 21 21 | */ 22 | -------------------------------------------------------------------------------- /ClassArguments/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Class Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val family1 = new Family("Mom", "Dad", "Sally", "Dick") 6 | family1.familySize() is 4 7 | val family2 = new Family("Dad", "Mom", "Harry") 8 | family2.familySize() is 3 9 | -------------------------------------------------------------------------------- /ClassArguments/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Class Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val family3 = new FlexibleFamily("Mom", "Dad", "Sally", "Dick") 6 | family3.familySize() is 4 7 | val family4 = new FlexibleFamily("Dad", "Mom", "Harry") 8 | family4.familySize() is 3 9 | -------------------------------------------------------------------------------- /ClassArguments/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Class Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val familyNoKids = new FlexibleFamily("Mom", "Dad") 6 | familyNoKids.familySize() is 2 7 | -------------------------------------------------------------------------------- /ClassArguments/Starter-8.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 8 2 | // From "Class Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val cup5 = new Cup5(0) 6 | cup5.add(20, 30, 50, 20, 10, -10, -40, 10, 50) is 100 7 | cup5.add(10, 10, -10, 10, 90, 70, -70) is 30 8 | -------------------------------------------------------------------------------- /ClassArguments/Starter-9.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 9 2 | // From "Class Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | squareThem(2) is 4 6 | squareThem(2, 4) is 20 7 | squareThem(1, 2, 4) is 21 8 | -------------------------------------------------------------------------------- /ClassArguments/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ClassExercises/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Class Exercises" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Dimension(var height:Int, var width:Int) 6 | val c = new Dimension(5,7) 7 | c.height is 5 8 | c.height = 10 9 | c.height is 10 10 | c.width = 19 11 | c.width is 19 12 | 13 | /* OUTPUT_SHOULD_BE 14 | 5 15 | 10 16 | 19 17 | */ 18 | -------------------------------------------------------------------------------- /ClassExercises/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Class Exercises" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Info(val name:String, var description:String) 6 | 7 | val info = new Info("stuff", "Something") 8 | info.name is "stuff" 9 | info.description is "Something" 10 | info.description = "Something else" 11 | info.description is "Something else" 12 | 13 | /* OUTPUT_SHOULD_BE 14 | stuff 15 | Something 16 | Something else 17 | */ 18 | -------------------------------------------------------------------------------- /ClassExercises/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Class Exercises" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val c = new Dimension(5,7) 6 | c.height is 5 7 | c.height = 10 8 | c.height is 10 9 | c.width = 19 10 | c.width is 19 11 | -------------------------------------------------------------------------------- /ClassExercises/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Class Exercises" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val info = new Info("stuff", "Something") 6 | info.name is "stuff" 7 | info.description is "Something" 8 | info.description = "Something else" 9 | info.description is "Something else" 10 | -------------------------------------------------------------------------------- /ClassExercises/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Class Exercises" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val info = new Info("stuff", "Something") 6 | info.name is "stuff" 7 | info.description is "Something" 8 | info.description = "Something else" 9 | info.description is "Something else" 10 | info.name = "This is the new name" 11 | info.name is "This is the new name" 12 | -------------------------------------------------------------------------------- /ClassExercises/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Class Exercises" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val t1 = new SimpleTime(10, 30) 6 | val t2 = new SimpleTime(9, 30) 7 | val st = t1.subtract(t2) 8 | st.hours is 1 9 | st.minutes is 0 10 | val st2 = new SimpleTime(10, 30).subtract(new SimpleTime(9, 45)) 11 | st2.hours is 0 12 | st2.minutes is 45 13 | val st3 = new SimpleTime(9, 30).subtract(new SimpleTime(10, 0)) 14 | st3.hours is 0 15 | st3.minutes is 0 16 | -------------------------------------------------------------------------------- /ClassExercises/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Class Exercises" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val anotherT1 = new SimpleTimeDefault(10, 30) 6 | val anotherT2 = new SimpleTimeDefault(9) 7 | val anotherST = anotherT1.subtract(anotherT2) 8 | anotherST.hours is 1 9 | anotherST.minutes is 30 10 | val anotherST2 = 11 | new SimpleTimeDefault(10).subtract(new SimpleTimeDefault(9, 45)) 12 | anotherST2.hours is 0 13 | anotherST2.minutes is 15 14 | -------------------------------------------------------------------------------- /ClassExercises/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Class Exercises" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val auxT1 = new SimpleTimeAux(10, 5) 6 | val auxT2 = new SimpleTimeAux(6) 7 | val auxST = auxT1.subtract(auxT2) 8 | auxST.hours is 4 9 | auxST.minutes is 5 10 | val auxST2= new SimpleTimeAux(12).subtract(new SimpleTimeAux(9, 45)) 11 | auxST2.hours is 2 12 | auxST2.minutes is 15 13 | -------------------------------------------------------------------------------- /ClassExercises/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ClassesAndObjects/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Classes & Objects" 3 | 4 | val r1 = Range(0, 10) 5 | println(r1.step) 6 | 7 | val r2 = Range(0, 10, 2) 8 | println(r2.step) 9 | 10 | // BONUS 11 | val r3 = r1.by(4) 12 | println(r3.step) 13 | 14 | /* OUTPUT_SHOULD_BE 15 | 1 16 | 2 17 | 4 18 | */ 19 | -------------------------------------------------------------------------------- /ClassesAndObjects/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Classes & Objects" 3 | 4 | val s = "This is an experiment" 5 | val afterSplit = s.split(" ") 6 | 7 | // Bonus 8 | println(afterSplit(0)) 9 | println(afterSplit(1)) 10 | println(afterSplit(2)) 11 | println(afterSplit(3)) 12 | 13 | /* OUTPUT_SHOULD_BE 14 | This 15 | is 16 | an 17 | experiment 18 | */ 19 | -------------------------------------------------------------------------------- /ClassesAndObjects/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Classes & Objects" 3 | 4 | val s1 = "Sally" 5 | val s2 = "Sally" 6 | 7 | if(s1.equals(s2)) { 8 | println("s1 and s2 are equal") 9 | } else { 10 | println("s1 and s2 are not equal") 11 | } 12 | println("s1: " + s1) 13 | println("s2: " + s2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | s1 and s2 are equal 17 | s1: Sally 18 | s2: Sally 19 | */ 20 | -------------------------------------------------------------------------------- /ClassesAndObjects/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Classes & Objects" 3 | 4 | val s1 = "Sally" 5 | val s2 = "Sam" 6 | 7 | if(s1.equals(s2)) { 8 | println("s1 and s2 are equal") 9 | } else { 10 | println("s1 and s2 are not equal") 11 | } 12 | println("s1: " + s1) 13 | println("s2: " + s2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | s1 and s2 are not equal 17 | s1: Sally 18 | s2: Sam 19 | */ 20 | -------------------------------------------------------------------------------- /ClassesAndObjects/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Classes & Objects" 3 | 4 | val s1 = "Sally" 5 | val s3 = s1.toUpperCase 6 | 7 | if(s3.contentEquals(s1)) { 8 | println("s1 and s3 are equal") 9 | } else { 10 | println("s1 and s3 are not equal") 11 | } 12 | println("s1: " + s1) 13 | println("s3: " + s3) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | s1 and s3 are not equal 17 | s1: Sally 18 | s3: SALLY 19 | */ 20 | -------------------------------------------------------------------------------- /ClassesAndObjects/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /CombiningSequenceswithzip/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Combining Sequences with zip" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def number(s:String) = 6 | s.zip(Range(0, s.length)).map { 7 | case (ch, n) => (n, ch) 8 | } 9 | 10 | number("Howdy") is 11 | Vector((0,'H'), (1,'o'), (2,'w'), 12 | (3,'d'), (4,'y')) 13 | 14 | /* OUTPUT_SHOULD_BE 15 | Vector((0,H), (1,o), (2,w), (3,d), (4,y)) 16 | */ 17 | -------------------------------------------------------------------------------- /CombiningSequenceswithzip/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Combining Sequences with zip" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val people = Vector("Sally Smith", 6 | "Dan Jones", "Tom Brown", "Betsy Blanc", 7 | "Stormy Morgan", "Hal Goodsen") 8 | 9 | val group1 = // fill this in 10 | val group2 = // fill this in 11 | val pairs = // fill this in 12 | 13 | pairs is Vector( 14 | ("Sally Smith","Betsy Blanc"), 15 | ("Dan Jones","Stormy Morgan"), 16 | ("Tom Brown","Hal Goodsen")) 17 | -------------------------------------------------------------------------------- /CombiningSequenceswithzip/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /CompanionObjects/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Companion Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class WalkActivity 6 | 7 | object WalkActivity { 8 | def start(athlete:String) = println(s"$athlete started!") 9 | } 10 | 11 | WalkActivity.start("Johnny") 12 | 13 | /* OUTPUT_SHOULD_BE 14 | Johnny started! 15 | */ 16 | -------------------------------------------------------------------------------- /CompanionObjects/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Companion Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def calories(lbs:Int, mins:Int, mph:Double=3):Long = 6 | math.round((MET * 3.5 * lbs * 0.45)/200.0 * mins) 7 | 8 | val sally = new WalkActivity3 9 | sally.calories(150, 30) is 82 10 | -------------------------------------------------------------------------------- /CompanionObjects/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Composition/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Composition" Atom (2nd edition) 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val walker = new Robot("Legs", 6 | "Visible Spectrum", "Magnet") 7 | walker is 8 | "Legs, Visible Spectrum, Magnet" 9 | val crawler = new Robot("Treads", 10 | "Infrared", "Claw") 11 | crawler is "Treads, Infrared, Claw" 12 | val arial = new Robot("Propeller", 13 | "UV", "None") 14 | arial is "Propeller, UV, None" 15 | -------------------------------------------------------------------------------- /Composition/Starter-1_1stEdition.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Composition" atom (1st Edition) 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val serving = new ServingKitchen 6 | serving.serveFood is true 7 | val prep = new PrepKitchen 8 | prep.prepFood is true 9 | -------------------------------------------------------------------------------- /Composition/Starter-2_1stEdition.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Composition" atom (1st Edition) 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val serving = new ServingKitchen 6 | serving.serveFood is true 7 | serving.prepFood is false 8 | val prep = new PrepKitchen 9 | prep.prepFood is true 10 | prep.serveFood is false 11 | val simple = new SimpleKitchen 12 | simple.serveFood is true 13 | simple.prepFood is true 14 | -------------------------------------------------------------------------------- /Composition/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Composition" Atom (2nd edition) 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val bot = new Robot( 6 | Vector(Mobility("Propeller"), 7 | Mobility("Legs"), 8 | Vision("UV"), 9 | Vision("Visible Spectrum"), 10 | Manipulator("Magnet"), 11 | Manipulator("Claw")) 12 | ) 13 | 14 | bot is "Mobility(Propeller), " + 15 | "Mobility(Legs), Vision(UV), " + 16 | "Vision(Visible Spectrum), " + 17 | "Manipulator(Magnet), " + 18 | "Manipulator(Claw)" 19 | -------------------------------------------------------------------------------- /Composition/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Composition" Atom (2nd edition) 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val bot = new Robot + 6 | Mobility("Propeller") + 7 | Mobility("Legs") + 8 | Vision("UV") + 9 | Vision("Visible Spectrum") + 10 | Manipulator("Magnet") + 11 | Manipulator("Claw") 12 | 13 | bot is "Mobility(Propeller)," + 14 | " Mobility(Legs) | Vision(UV)," + 15 | " Vision(Visible Spectrum) |" + 16 | " Manipulator(Magnet)," + 17 | " Manipulator(Claw)" 18 | -------------------------------------------------------------------------------- /Composition/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /CompoundExpressions/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Compound Expressions" 3 | 4 | val a = 1 5 | val b = 5 // change to 6 to test exercise 2 6 | val c = 5 7 | 8 | if(a <=c && b <=c) { 9 | println("both are!") 10 | } else if(a <= c || b <= c) { 11 | println("one is and one isn't!") 12 | } 13 | 14 | /* OUTPUT_SHOULD_BE 15 | both are! 16 | */ 17 | -------------------------------------------------------------------------------- /CompoundExpressions/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Compound Expressions" 3 | 4 | val distance = 9 5 | val activity = "biking" 6 | 7 | val willDo = { 8 | if(activity == "running") { 9 | distance < 10 10 | } else if(activity == "biking") { 11 | distance < 20 12 | } else if(activity == "walking") { 13 | distance < 6 14 | } else { 15 | false 16 | } 17 | } 18 | 19 | println(activity + ": " + willDo) 20 | 21 | /* OUTPUT_SHOULD_BE 22 | biking: true 23 | */ 24 | -------------------------------------------------------------------------------- /CompoundExpressions/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Compound Expressions" atom 3 | 4 | val distance = 9 5 | val activity = "running" 6 | val willDo = // fill this in 7 | 8 | /* Output 9 | (run 3 times, once for each activity): 10 | running: true 11 | walking: false 12 | biking: true 13 | */ 14 | -------------------------------------------------------------------------------- /CompoundExpressions/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Comprehensions/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Comprehensions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def oddLT10(v:Vector[Int]):Vector[Int] = { 6 | val result = for { 7 | n <- v 8 | if n < 10 9 | if n % 2 != 0 10 | } yield n 11 | result 12 | } 13 | 14 | val v = Vector(1,2,3,5,6,7,8,10,13,14,17) 15 | oddLT10(v) is Vector(1,3,5,7) 16 | 17 | /* OUTPUT_SHOULD_BE 18 | Vector(1, 3, 5, 7) 19 | */ 20 | -------------------------------------------------------------------------------- /Comprehensions/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Comprehensions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def yielding2(l:List[Int]):List[Int] = { 6 | for { 7 | n <- l 8 | if n < 10 9 | isOdd = (n % 2 != 0) 10 | if(isOdd) 11 | } yield n 12 | } 13 | 14 | val theList = List(1,2,3,5,6,7,8,10,13,14,17) 15 | yielding2(theList) is List(1,3,5,7) 16 | 17 | /* OUTPUT_SHOULD_BE 18 | List(1, 3, 5, 7) 19 | */ 20 | -------------------------------------------------------------------------------- /Comprehensions/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Comprehensions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def yielding3(v:Vector[Int]):Vector[Int]={ 6 | val result:Vector[Int] = for { 7 | n <- v 8 | if n < 10 9 | if n % 2 != 0 10 | } yield n * 10 + 2 11 | result 12 | } 13 | 14 | val v = Vector(1,2,3,5,6,7,8,10,13,14,17) 15 | yielding3(v) is Vector(12, 32, 52, 72) 16 | 17 | /* OUTPUT_SHOULD_BE 18 | Vector(12, 32, 52, 72) 19 | */ 20 | -------------------------------------------------------------------------------- /Comprehensions/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Comprehensions" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | // Here's the original yielding2: 6 | def yielding2(v:Vector[Int]):Vector[Int]={ 7 | for { 8 | n <- v 9 | if n < 10 10 | isOdd = (n % 2 != 0) 11 | if(isOdd) 12 | } yield n 13 | } 14 | 15 | val theList = List(1,2,3,5,6,7,8,10,13,14,17) 16 | yielding2(theList) is List(1,3,5,7) 17 | -------------------------------------------------------------------------------- /Comprehensions/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Comprehensions" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | // Here's the original yielding3: 6 | def yielding3(v:Vector[Int]):Vector[Int]={ 7 | for { 8 | n <- v 9 | if n < 10 10 | isOdd = (n % 2 != 0) 11 | if(isOdd) 12 | } yield { 13 | val u = n * 10 14 | u + 2 15 | } 16 | } 17 | 18 | val v = Vector(1,2,3,5,6,7,8,10,13,14,17) 19 | yielding3(v) is Vector(12, 32, 52, 72) 20 | -------------------------------------------------------------------------------- /Comprehensions/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Comprehensions" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val activities = Vector( 6 | Activity("01-01", "Run"), 7 | Activity("01-03", "Ski"), 8 | Activity("01-04", "Run"), 9 | Activity("01-10", "Ski"), 10 | Activity("01-03", "Run")) 11 | 12 | getDates("Ski", activities) is Vector("01-03", "01-10") 13 | getDates("Run", activities) is Vector("01-01", "01-04", "01-03") 14 | getDates("Bike", activities) is Vector() 15 | -------------------------------------------------------------------------------- /Comprehensions/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ConditionalExpressions/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Conditional Expressions" 3 | 4 | val a = 1 5 | val b = 5 6 | 7 | if(a < b) { 8 | println("a is less than b") 9 | } else { 10 | println("a is not less than b") 11 | } 12 | 13 | /* OUTPUT_SHOULD_BE 14 | a is less than b 15 | */ 16 | -------------------------------------------------------------------------------- /ConditionalExpressions/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Conditional Expressions" 3 | 4 | val a = 1 5 | val b = 5 6 | 7 | if(a < 2) { 8 | println("a is less than 2") 9 | } else { 10 | println("a is not less than 2") 11 | } 12 | 13 | if(b < 2) { 14 | println("b is less than 2") 15 | } else { 16 | println("b is not less than 2") 17 | } 18 | 19 | /* OUTPUT_SHOULD_BE 20 | a is less than 2 21 | b is not less than 2 22 | */ 23 | -------------------------------------------------------------------------------- /ConditionalExpressions/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Conditional Expressions" 3 | 4 | val a = 1 5 | val b = 5 6 | val c = 5 7 | 8 | if(a < c) { 9 | println ("a is less than c") 10 | } else { 11 | println("a is not less than c") 12 | } 13 | 14 | if(b < c) { 15 | println("b is less than c") 16 | } else { 17 | println ("b is not less than c") 18 | } 19 | 20 | /* OUTPUT_SHOULD_BE 21 | a is less than c 22 | b is not less than c 23 | */ 24 | -------------------------------------------------------------------------------- /ConditionalExpressions/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Constructors/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Constructors" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val doubleHalfCaf = new Coffee(shots=2, decaf=1) 6 | val tripleHalfCaf = new Coffee(shots=3, decaf=2) 7 | 8 | doubleHalfCaf.decaf is 1 9 | doubleHalfCaf.caf is 1 10 | doubleHalfCaf.shots is 2 11 | tripleHalfCaf.decaf is 2 12 | tripleHalfCaf.caf is 1 13 | tripleHalfCaf.shots is 3 14 | -------------------------------------------------------------------------------- /Constructors/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ConstructorsAndExceptions/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Constructors & Exceptions" 3 | import com.atomicscala.AtomicTest._ 4 | import codelisting._ 5 | 6 | class CodeListingTester3(makeList:String => IndexedSeq[String]) { 7 | val l = makeList("NoSuchFile.scala") 8 | var index = 1 9 | for(i <- l) { 10 | println (s"$index $i") 11 | index = index + 1 12 | } 13 | } 14 | 15 | new CodeListingTester3(CodeListing.apply) 16 | 17 | /* OUTPUT_SHOULD_BE 18 | 1 File Not Found: NoSuchFile.scala 19 | */ 20 | -------------------------------------------------------------------------------- /ConstructorsAndExceptions/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ConvertingExceptionswithTry/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Converting Exceptions with Try" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | f(0) is "OK Bob" 6 | f(1) is "Reason" 7 | f(2) is "11" 8 | f(3) is "1.618" 9 | -------------------------------------------------------------------------------- /ConvertingExceptionswithTry/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /CreatingClasses/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Creating Classes" 3 | 4 | class Lion 5 | class Giraffe 6 | 7 | val lion2 = new Lion 8 | val giraffe2 = new Giraffe 9 | val giraffe3 = new Giraffe 10 | 11 | println(lion2) 12 | println(giraffe2) 13 | println(giraffe3) 14 | 15 | /* OUTPUT_SHOULD_CONTAIN 16 | Main$$anon$1$Lion@ 17 | Main$$anon$1$Giraffe@ 18 | Main$$anon$1$Giraffe@ 19 | */ 20 | -------------------------------------------------------------------------------- /CreatingClasses/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Creating Classes" 3 | 4 | class Zebra { 5 | println("I have stripes!") 6 | } 7 | 8 | val z = new Zebra 9 | 10 | /* OUTPUT_SHOULD_BE 11 | I have stripes! 12 | */ 13 | -------------------------------------------------------------------------------- /CreatingClasses/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /CustomErrorReporting/Fail.scala: -------------------------------------------------------------------------------- 1 | // Fail.scala 2 | package com.atomicscala.reporterr 3 | import util.Failure 4 | import util.control.NoStackTrace 5 | 6 | class FailMsg(val msg:String) extends 7 | Throwable with NoStackTrace { 8 | override def toString = msg 9 | } 10 | 11 | object Fail { 12 | def apply(msg:String) = 13 | Failure(new FailMsg(msg)) 14 | } 15 | -------------------------------------------------------------------------------- /CustomErrorReporting/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /DataTypes/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Data Types" 3 | 4 | val v1:Int = 5 5 | println(v1) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | 5 9 | */ -------------------------------------------------------------------------------- /DataTypes/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Data Types" 3 | 4 | val s:String = "ABC1234" 5 | println(s) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | ABC1234 9 | */ 10 | -------------------------------------------------------------------------------- /DataTypes/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Data Types" 3 | 4 | val d1:Double = 5.4D 5 | println(d1) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | 5.4 9 | */ 10 | -------------------------------------------------------------------------------- /DataTypes/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Data Types" 3 | 4 | val b:Boolean = true 5 | println(b) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | true 9 | */ 10 | -------------------------------------------------------------------------------- /DataTypes/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Data Types" 3 | 4 | val s2:String = """This 5 | allows 6 | you 7 | to 8 | store 9 | lots 10 | of 11 | lines""" 12 | 13 | println(s2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | This 17 | allows 18 | you 19 | to 20 | store 21 | lots 22 | of 23 | lines 24 | */ 25 | -------------------------------------------------------------------------------- /DataTypes/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "Data Types" 3 | 4 | // This will not run: 5 | val b:Boolean = "maybe" 6 | 7 | /* OUTPUT_SHOULD_CONTAIN 8 | error: type mismatch; 9 | found : String("maybe") 10 | required: Boolean 11 | val b:Boolean = "maybe" 12 | ^ 13 | one error found 14 | */ 15 | -------------------------------------------------------------------------------- /DataTypes/Solution-7.scala: -------------------------------------------------------------------------------- 1 | // Solution-7.scala 2 | // Solution to Exercise 7 in "Data Types" 3 | 4 | // This will not run: 5 | val v3:Int = 15.4 6 | 7 | /* OUTPUT_SHOULD_CONTAIN 8 | error: type mismatch; 9 | found : Double(15.4) 10 | required: Int 11 | val v3:Int = 15.4 12 | ^ 13 | one error found 14 | */ 15 | -------------------------------------------------------------------------------- /DataTypes/Solution-8.scala: -------------------------------------------------------------------------------- 1 | // Solution-8.scala 2 | // Solution to Exercise 8 in "Data Types" 3 | 4 | val d:Double = 15 5 | println(d) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | 15.0 9 | */ 10 | -------------------------------------------------------------------------------- /DataTypes/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /DefiningOperators/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Defining Operators" 3 | import com.atomicscala.AtomicTest._ 4 | import scala.math.pow 5 | 6 | class FancyNumber1(val num:Int) { 7 | def power(n:Int):Int = pow(num.toDouble, n.toDouble).toInt 8 | } 9 | 10 | val a1 = new FancyNumber1(2) 11 | a1.power(3) is 8 12 | val b1 = new FancyNumber1(10) 13 | b1.power(2) is 100 14 | 15 | /* OUTPUT_SHOULD_BE 16 | 8 17 | 100 18 | */ 19 | -------------------------------------------------------------------------------- /DefiningOperators/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Defining Operators" 3 | import com.atomicscala.AtomicTest._ 4 | import scala.math.pow 5 | 6 | class FancyNumber2(val num:Int) { 7 | def power(n:Int):Int = pow(num.toDouble, n.toDouble).toInt 8 | def ^(n:Int):Int = power(n) 9 | } 10 | 11 | val a2 = new FancyNumber2(2) 12 | a2.^(3) is 8 13 | val b2 = new FancyNumber2(10) 14 | b2 ^ 2 is 100 15 | 16 | /* OUTPUT_SHOULD_BE 17 | 8 18 | 100 19 | */ 20 | -------------------------------------------------------------------------------- /DefiningOperators/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Defining Operators" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val someT1 = new SimpleTime2(10, 30) 6 | val someT2 = new SimpleTime2(9, 30) 7 | val someST = someT1 - someT2 8 | someST.hours is 1 9 | someST.minutes is 0 10 | 11 | val someST2 = new SimpleTime2(10, 30) - new SimpleTime2(9, 45) 12 | someST2.hours is 0 13 | someST2.minutes is 45 14 | -------------------------------------------------------------------------------- /DefiningOperators/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Defining Operators" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val a1 = new FancyNumber1(2) 6 | a1.power(3) is 8 7 | val b1 = new FancyNumber1(10) 8 | b1.power(2) is 100 9 | -------------------------------------------------------------------------------- /DefiningOperators/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Defining Operators" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val a2 = new FancyNumber2(2) 6 | a2.^(3) is 8 7 | val b2 = new FancyNumber2(10) 8 | b2 ^ 2 is 100 9 | -------------------------------------------------------------------------------- /DefiningOperators/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Defining Operators" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val a3 = new FancyNumber3(2) 6 | a3.^(3) is 8 7 | a3.**(3) is 8 8 | val b3 = new FancyNumber3(10) 9 | b3 ^ 2 is 100 10 | b3 ** 2 is 100 11 | -------------------------------------------------------------------------------- /DefiningOperators/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /DesignbyContract/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Enumerations/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Enumerations" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | MonthName.February is "February" 6 | MonthName.February.id is 1 7 | -------------------------------------------------------------------------------- /Enumerations/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Enumerations" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | MonthName2.February is "February" 6 | MonthName2.February.id is 2 7 | MonthName2.December.id is 12 8 | MonthName2.July.id is 7 9 | -------------------------------------------------------------------------------- /Enumerations/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Enumerations" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | July is "July" 6 | monthNumber(July) is 7 7 | -------------------------------------------------------------------------------- /Enumerations/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Enumerations" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | season(January) is "Winter" 6 | season(April) is "Spring" 7 | season(August) is "Summer" 8 | season(November) is "Autumn" 9 | -------------------------------------------------------------------------------- /Enumerations/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Enumerations" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | Level.Draining is Draining 6 | Level.Draining.id is 5 7 | checkLevel(Low) is "Level Low OK" 8 | checkLevel(Empty) is "Alert" 9 | checkLevel(Draining) is "Level Draining OK" 10 | checkLevel(Pooling) is "Warning!" 11 | checkLevel(Dry) is "Alert" 12 | -------------------------------------------------------------------------------- /Enumerations/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ErrorHandlingwithExceptions/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Error Handling with Exceptions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def solution1 = 6 | try { 7 | throw new Exception("solution1") 8 | } catch { 9 | case err:Exception => err.getMessage 10 | } 11 | 12 | solution1 is "solution1" 13 | 14 | /* OUTPUT_SHOULD_BE 15 | solution1 16 | */ 17 | -------------------------------------------------------------------------------- /ErrorHandlingwithExceptions/Solution-2a.scala: -------------------------------------------------------------------------------- 1 | // Solution-2a.scala 2 | // Solution to Exercise 2 in "Error Handling with Exceptions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Solution2 { 6 | def f = "Solution2" 7 | } 8 | 9 | var s2:Solution2 = null 10 | 11 | s2.f 12 | 13 | /* OUTPUT_SHOULD_CONTAIN 14 | java.lang.NullPointerException 15 | */ 16 | -------------------------------------------------------------------------------- /ErrorHandlingwithExceptions/Solution-2b.scala: -------------------------------------------------------------------------------- 1 | // Solution-2b.scala 2 | // Solution to Exercise 2 in "Error Handling with Exceptions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Solution2 { 6 | def f = "Solution2" 7 | } 8 | 9 | var s2:Solution2 = null 10 | 11 | def t = try { 12 | s2.f 13 | } catch { 14 | case e:NullPointerException => "Throws null pointer exception" 15 | } 16 | 17 | t is "Throws null pointer exception" 18 | 19 | /* OUTPUT_SHOULD_BE 20 | Throws null pointer exception 21 | */ 22 | -------------------------------------------------------------------------------- /ErrorHandlingwithExceptions/Solution-3a.scala: -------------------------------------------------------------------------------- 1 | // Solution-3a.scala 2 | // Solution to Exercise 3 in "Error Handling with Exceptions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v = Vector(1, 7, 22, 11, 17) 6 | v(4) is 17 7 | 8 | v(5) 9 | 10 | /* OUTPUT_SHOULD_CONTAIN 11 | 17 12 | java.lang.IndexOutOfBoundsException: 5 13 | */ 14 | -------------------------------------------------------------------------------- /ErrorHandlingwithExceptions/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Error Handling with Exceptions" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class FauxPas(msg:String) extends Exception(msg) { 6 | def what = msg 7 | } 8 | 9 | def t = try { 10 | throw new FauxPas("Oops!") 11 | } catch { 12 | case e:FauxPas => e.what 13 | } 14 | 15 | t is "Oops!" 16 | 17 | /* OUTPUT_SHOULD_BE 18 | Oops! 19 | */ 20 | -------------------------------------------------------------------------------- /ErrorHandlingwithExceptions/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ErrorReportingwithEither/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Error Reporting with Either" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | letters is "Vector(Left(a), Right(b), " + 6 | "Right(c), Right(d), Left(e), Right(f), " + 7 | "Right(g), Right(h), Left(i), Right(j), " + 8 | "Right(k), Right(l), Right(m), Right(n), " + 9 | "Left(o), Right(p), Right(q), Right(r), " + 10 | "Right(s), Right(t), Left(u), Right(v), " + 11 | "Right(w), Right(x), Right(y), Right(z))" 12 | -------------------------------------------------------------------------------- /ErrorReportingwithEither/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Error Reporting with Either" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | testLetters(0) is "Vowel: a" 6 | testLetters(4) is "Vowel: e" 7 | testLetters(13) is "Consonant: n" 8 | -------------------------------------------------------------------------------- /ErrorReportingwithEither/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /EvaluationOrder/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Evaluation Order" 3 | 4 | val sky = "sunny" 5 | var temperature = 87 6 | 7 | val sunnyAndWarm = sky == "sunny" && temperature > 80 8 | println(sunnyAndWarm) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | true 12 | */ 13 | -------------------------------------------------------------------------------- /EvaluationOrder/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Evaluation Order" 3 | 4 | val sky = "sunny" 5 | var temperature = 87 6 | val somewhatSunnyAndWarm = 7 | (sky == "sunny" || 8 | sky == "party cloudy") && 9 | temperature > 80 10 | 11 | println(somewhatSunnyAndWarm) 12 | 13 | /* OUTPUT_SHOULD_BE 14 | true 15 | */ 16 | -------------------------------------------------------------------------------- /EvaluationOrder/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Evaluation Order" 3 | 4 | val sky = "sunny" 5 | val temperature = 10 6 | 7 | val sunnyAndExtreme = 8 | (sky == "sunny" || 9 | sky == "party cloudy") && 10 | (temperature > 80 || 11 | temperature < 20) 12 | 13 | println(sunnyAndExtreme) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | true 17 | */ 18 | -------------------------------------------------------------------------------- /EvaluationOrder/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Evaluation Order" 3 | 4 | val faren = 80.0 5 | val celsius = (faren-32.0) * (5.0/9.0) 6 | println(celsius) 7 | 8 | /* OUTPUT_SHOULD_BE 9 | 26.666666666666668 10 | */ 11 | -------------------------------------------------------------------------------- /EvaluationOrder/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Evaluation Order" 3 | 4 | val c = 26.67 5 | val f = c*(9.0/5) + 32 6 | println(f) 7 | 8 | /* OUTPUT_SHOULD_BE 9 | 80.006 10 | */ 11 | -------------------------------------------------------------------------------- /EvaluationOrder/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Expressions/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Expressions" 3 | 4 | val feetPerMile = 5280 5 | 6 | println(feetPerMile) 7 | 8 | /* OUTPUT_SHOULD_BE 9 | 5280 10 | */ 11 | -------------------------------------------------------------------------------- /Expressions/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Expressions" 3 | 4 | val feetPerMile = 5280 5 | val yardsPerMile=feetPerMile/3.0 6 | 7 | println(feetPerMile) 8 | println(yardsPerMile) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 5280 12 | 1760.0 13 | */ 14 | -------------------------------------------------------------------------------- /Expressions/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Expressions" 3 | 4 | val feetPerMile = 5280 5 | val yardsPerMile=feetPerMile/3.0 6 | val miles = 2000/yardsPerMile 7 | 8 | println(yardsPerMile) 9 | println(miles) 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 1760.0 13 | 1.1363636363636365 14 | */ 15 | -------------------------------------------------------------------------------- /Expressions/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Expressions" 3 | 4 | val yards2:Double = { 5 | val feetPerMile = 5280 6 | feetPerMile/3 7 | } 8 | val miles2 = 2000/yards2 9 | 10 | println(yards2) 11 | println(miles2) 12 | 13 | /* OUTPUT_SHOULD_BE 14 | 1760.0 15 | 1.1363636363636365 16 | */ 17 | -------------------------------------------------------------------------------- /Expressions/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ExtensibleSystemswithTypeClasses/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ExtensionMethods/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Extension Methods" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Book(title:String) 6 | 7 | def categorize(book:Book, category:String) = 8 | s"$book, category: $category" 9 | 10 | categorize(Book("Dracula"), "Vampire") is 11 | "Book(Dracula), category: Vampire" 12 | 13 | /* OUTPUT_SHOULD_BE 14 | Book(Dracula), category: Vampire 15 | */ 16 | -------------------------------------------------------------------------------- /ExtensionMethods/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Fields/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Fields" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Cup2 { 6 | var percentFull = 0 7 | val max = 100 8 | def add(increase:Int):Int = { 9 | percentFull += increase 10 | if(percentFull > max) percentFull = max 11 | percentFull // Return value 12 | } 13 | } 14 | 15 | val cup2 = new Cup2 16 | cup2.add(45) is 45 17 | cup2.add(-15) is 30 18 | cup2.add(-50) is -20 19 | 20 | /* OUTPUT_SHOULD_BE 21 | 45 22 | 30 23 | -20 24 | */ 25 | -------------------------------------------------------------------------------- /Fields/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Fields" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Cup3 { 6 | var percentFull = 0 7 | val max = 100 8 | def add(increase:Int):Int = { 9 | percentFull += increase 10 | if(percentFull > max) percentFull = max 11 | if(percentFull < 0) percentFull = 0 12 | percentFull // Return this value 13 | } 14 | } 15 | 16 | val cup3 = new Cup3 17 | cup3.percentFull = 56 18 | cup3.percentFull is 56 19 | 20 | /* OUTPUT_SHOULD_BE 21 | 56 22 | */ 23 | -------------------------------------------------------------------------------- /Fields/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Fields" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val cup2 = new Cup2 6 | cup2.add(45) is 45 7 | cup2.add(-15) is 30 8 | cup2.add(-50) is -20 9 | -------------------------------------------------------------------------------- /Fields/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Fields" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val cup3 = new Cup3 6 | cup3.add(45) is 45 7 | cup3.add(-55) is 0 8 | cup3.add(10) is 10 9 | cup3.add(-9) is 1 10 | cup3.add(-2) is 0 11 | -------------------------------------------------------------------------------- /Fields/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Fields" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | cup3.percentFull = 56 6 | cup3.percentFull is 56 7 | -------------------------------------------------------------------------------- /Fields/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Fields" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val cup4 = new Cup4 6 | cup4.set(56) 7 | cup4.get() is 56 8 | -------------------------------------------------------------------------------- /Fields/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ForLoops/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "For Loops" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val r1 = Range(0, 10) 6 | r1 is Range(0, 10) 7 | 8 | /* OUTPUT_SHOULD_BE 9 | Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 10 | */ 11 | -------------------------------------------------------------------------------- /ForLoops/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "For Loops" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val r2 = Range(0, 10).inclusive 6 | r2 is (0 to 10) 7 | r2 is (0 until 11) 8 | 9 | /* OUTPUT_SHOULD_BE 10 | Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 11 | Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 12 | */ 13 | -------------------------------------------------------------------------------- /ForLoops/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "For Loops" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var total:Int = 0 6 | for(i <- Range(0, 10).inclusive) 7 | total = total + i 8 | total is 55 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 55 12 | */ 13 | -------------------------------------------------------------------------------- /ForLoops/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "For Loops" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var totalEvens:Int = 0 6 | for(i <- Range(0, 10).inclusive) { 7 | if(i % 2 == 0) 8 | totalEvens = totalEvens + i 9 | } 10 | totalEvens is 30 11 | 12 | /* OUTPUT_SHOULD_BE 13 | 30 14 | */ 15 | -------------------------------------------------------------------------------- /ForLoops/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "For Loops" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var evens:Int = 0 6 | var odds:Int = 0 7 | 8 | for(i <- 0 to 10) { 9 | if(i % 2 == 0) evens = evens + i 10 | else odds = odds + i 11 | } 12 | 13 | evens is 30 14 | odds is 25 15 | (evens + odds) is 55 16 | 17 | /* OUTPUT_SHOULD_BE 18 | 30 19 | 25 20 | 55 21 | */ 22 | -------------------------------------------------------------------------------- /ForLoops/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "For Loops" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var evens2:Int = 0 6 | var odds2:Int = 0 7 | 8 | for(i<- Range.inclusive(0, 10)) { 9 | if(i % 2 == 0) evens2 = evens2 + i 10 | else odds2 = odds2 + i 11 | } 12 | 13 | println(evens2) 14 | println(odds2) 15 | evens2 + odds2 is 55 16 | 17 | /* OUTPUT_SHOULD_BE 18 | 30 19 | 25 20 | 55 21 | */ 22 | -------------------------------------------------------------------------------- /ForLoops/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "For Loops" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val r1 = // fill this in 6 | r1 is // fill this in 7 | -------------------------------------------------------------------------------- /ForLoops/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "For Loops" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | total is 55 6 | -------------------------------------------------------------------------------- /ForLoops/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "For Loops" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | totalEvens is 30 6 | -------------------------------------------------------------------------------- /ForLoops/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "For Loops" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | evens is 30 6 | odds is 25 7 | (evens + odds) is 55 8 | -------------------------------------------------------------------------------- /ForLoops/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var str = "" 6 | val numberV = Vector(1, 2, 3, 4) 7 | numberV.foreach(n => str += n) 8 | str is "1234" 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 1234 12 | */ 13 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var str1 = "" 6 | val numberV = Vector(1, 2, 3, 4) 7 | numberV.foreach(n => str1 += n + ",") 8 | str1 is "1,2,3,4," 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 1,2,3,4, 12 | */ 13 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val dogYears = (x:Int) => (x * 7) 6 | dogYears(10) is 70 7 | 8 | /* OUTPUT_SHOULD_BE 9 | 70 10 | */ 11 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v = Vector(1, 5, 7, 8) 6 | val dogYears = (x:Int) => (x * 7) 7 | 8 | var s = "" 9 | v.foreach((x:Int) => (s = s + (dogYears(x) + " "))) 10 | s is "7 35 49 56 " 11 | 12 | /* OUTPUT_SHOULD_BE 13 | 7 35 49 56 14 | */ 15 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var s = "" 6 | val v = Vector(1, 5, 7, 8) 7 | v.foreach((x:Int) => (s = s + (x * 7 + " "))) 8 | 9 | s is "7 35 49 56 " 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 7 35 49 56 13 | */ 14 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val between = (temperature:Int, low:Int, high:Int) => 6 | if(temperature >= low && temperature <= high) true else false 7 | 8 | between(70, 80, 90) is false 9 | between(70, 60, 90) is true 10 | 11 | /* OUTPUT_SHOULD_BE 12 | false 13 | true 14 | */ 15 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-7.scala: -------------------------------------------------------------------------------- 1 | // Solution-7.scala 2 | // Solution to Exercise 7 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var s = "" 6 | val numbers = Vector(1, 2, 5, 3, 7) 7 | numbers.foreach(x => (s = s + (x*x + " "))) 8 | 9 | s is "1 4 25 9 49 " 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 1 4 25 9 49 13 | */ 14 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-8.scala: -------------------------------------------------------------------------------- 1 | // Solution-8.scala 2 | // Solution to Exercise 8 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val pluralize = (x:String) => (x + "s") 6 | pluralize("cat") is "cats" 7 | pluralize("dog") is "dogs" 8 | pluralize("silly") is "sillys" 9 | 10 | /* OUTPUT_SHOULD_BE 11 | cats 12 | dogs 13 | sillys 14 | */ 15 | -------------------------------------------------------------------------------- /FunctionsasObjects/Solution-9.scala: -------------------------------------------------------------------------------- 1 | // Solution-9.scala 2 | // Solution to Exercise 9 in "Functions as Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val pluralize = (x:String) => (x + "s") 6 | 7 | val words = Vector("word", "cat", "animal") 8 | 9 | var s = "" 10 | words.foreach((x:String) => (s = s + (pluralize(x) + " "))) 11 | 12 | s is "words cats animals " 13 | 14 | /* OUTPUT_SHOULD_BE 15 | words cats animals 16 | */ 17 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | str is "1234" 6 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | str is "1,2,3,4," 6 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val dogYears = // Your function here 6 | dogYears(10) is 70 7 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val s = "" 6 | val v = Vector(1, 5, 7, 8) 7 | v.foreach(/* Fill this in */) 8 | s is "7 35 49 56 " 9 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val s = "" 6 | val v = Vector(1, 5, 7, 8) 7 | v.foreach(/* Fill this in */) 8 | s is "7 35 49 56 " 9 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | between(70, 80, 90) is false 6 | between(70, 60, 90) is true 7 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-7.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 7 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var s = "" 6 | val numbers = Vector(1, 2, 5, 3, 7) 7 | numbers.foreach(/* Fill this in */) 8 | s is "1 4 25 9 49 " 9 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-8.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 8 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | pluralize("cat") is "cats" 6 | pluralize("dog") is "dogs" 7 | pluralize("silly") is "sillys" 8 | -------------------------------------------------------------------------------- /FunctionsasObjects/Starter-9.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 9 2 | // From "Functions as Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val s = "" 6 | val words = Vector("word", "cat", "animal") 7 | words.foreach(/* Fill this in */) 8 | s is "words cats animals " 9 | -------------------------------------------------------------------------------- /FunctionsasObjects/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /HandlingNon-ValueswithOption/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Handling Non-Values with Option" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def f(i:Int) = 6 | if(i == 0) 7 | None 8 | else 9 | Some(24/i) 10 | 11 | f(4) is Some(6) 12 | f(5) is Some(4) 13 | f(6) is Some(4) 14 | f(0) is None 15 | f(24) is Some(1) 16 | f(25) is Some(0) 17 | 18 | /* OUTPUT_SHOULD_BE 19 | Some(6) 20 | Some(4) 21 | Some(4) 22 | None 23 | Some(1) 24 | Some(0) 25 | */ 26 | -------------------------------------------------------------------------------- /HandlingNon-ValueswithOption/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Handling Non-Values with Option" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def f2(i:Int):Option[Int] = 6 | if(i == 0) 7 | None 8 | else 9 | Some(24/i) 10 | 11 | f2(4) is Some(6) 12 | f2(5) is Some(4) 13 | f2(6) is Some(4) 14 | f2(0) is None 15 | f2(24) is Some(1) 16 | f2(25) is Some(0) 17 | 18 | /* OUTPUT_SHOULD_BE 19 | Some(6) 20 | Some(4) 21 | Some(4) 22 | None 23 | Some(1) 24 | Some(0) 25 | */ 26 | -------------------------------------------------------------------------------- /HandlingNon-ValueswithOption/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Handling Non-Values with Option" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | f(4) is Some(6) 6 | f(5) is Some(4) 7 | f(6) is Some(4) 8 | f(0) is None 9 | f(24) is Some(1) 10 | f(25) is Some(0) 11 | -------------------------------------------------------------------------------- /HandlingNon-ValueswithOption/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Handling Non-Values with Option" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | alphanumeric(0) is Some(0) 6 | alphanumeric('a') is Some('a') 7 | alphanumeric('m') is Some('m') 8 | alphanumeric('$') is None 9 | alphanumeric('Z') is Some('Z') 10 | -------------------------------------------------------------------------------- /HandlingNon-ValueswithOption/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /IdiomaticScala/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Idiomatic Scala" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def addMultiply(x:Int, y:Double, s:String) = { 6 | println(s) 7 | (x + y) * 2.1 8 | } 9 | 10 | val r2:Double = addMultiply(7, 9, "Inside addMultiply") 11 | r2 is 33.6 12 | 13 | /* OUTPUT_SHOULD_BE 14 | Inside addMultiply 15 | 33.6 16 | */ 17 | -------------------------------------------------------------------------------- /IdiomaticScala/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ImportsAndPackages-1stEdition/Crest.scala: -------------------------------------------------------------------------------- 1 | package com.atomicscala.royals 2 | 3 | class Crest(name:String, year:String) { 4 | def description():String = { 5 | name + " in the year " + year 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /ImportsAndPackages-1stEdition/ImportTests.scala: -------------------------------------------------------------------------------- 1 | // ImportTests.scala 2 | // Solution to Exercise 1 in "Imports & Packages" 3 | import com.atomicscala.royals.Royalty 4 | 5 | val royal = new Royalty("Henry", "Laughs") 6 | val title = royal.title() 7 | assert("Sir Laughsalot" == title, "Expected Sir Laughsalot, Got " + title) 8 | println(title) 9 | -------------------------------------------------------------------------------- /ImportsAndPackages-1stEdition/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Imports & Packages" 3 | import com.atomicscala.royals.Royalty 4 | 5 | val royal = new Royalty("Henry", "Laughs") 6 | val title = royal.title() 7 | assert("Sir Laughsalot" == title, "Expected Sir Laughsalot, Got " + title) 8 | println(title) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | Sir Laughsalot 12 | */ 13 | -------------------------------------------------------------------------------- /ImportsAndPackages-1stEdition/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Imports & Packages" 3 | import com.atomicscala.royals.Crest 4 | 5 | val crest = new Crest("Bear", "1875") 6 | assert("Bear in the year 1875" == 7 | crest.description(), 8 | "Expected Bear in the year 1875, Got " + crest) 9 | println(crest.description) 10 | 11 | /* OUTPUT_SHOULD_BE 12 | Bear in the year 1875 13 | */ 14 | -------------------------------------------------------------------------------- /ImportsAndPackages-1stEdition/TheRoyalty.scala: -------------------------------------------------------------------------------- 1 | // TheRoyalty.scala 2 | package com.atomicscala.royals 3 | 4 | class Royalty(name:String, 5 | characteristic:String) { 6 | def title():String = { 7 | "Sir " + characteristic + "alot" 8 | } 9 | def fancyTitle():String = { 10 | "Sir " + name + " " + characteristic + "alot" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ImportsAndPackages-1stEdition/Trivia.scala: -------------------------------------------------------------------------------- 1 | package com.atomicscala.trivia 2 | 3 | class Movies 4 | 5 | class Science 6 | 7 | class Literature 8 | -------------------------------------------------------------------------------- /ImportsAndPackages-1stEdition/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/EquilateralTriangle.scala: -------------------------------------------------------------------------------- 1 | // EquilateralTriangle.scala 2 | package com.atomicscala.pythagorean 3 | 4 | class EquilateralTriangle { 5 | def area(side:Double):Double = { 6 | side*side*Math.sqrt(3)/4 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/PythagoreanTheorem.scala: -------------------------------------------------------------------------------- 1 | // PythagoreanTheorem.scala 2 | package com.atomicscala.pythagorean 3 | 4 | class RightTriangle { 5 | def hypotenuse(a:Double, b:Double):Double = { 6 | Math.sqrt(a*a + b*b) 7 | } 8 | def area(a:Double, b:Double):Double = { 9 | a*b/2 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Imports & Packages" 3 | import com.atomicscala.pythagorean.RightTriangle 4 | 5 | val rt = new RightTriangle 6 | println(rt.hypotenuse(3,4)) 7 | println(rt.area(3,4)) 8 | assert(rt.hypotenuse(3,4) == 5) 9 | assert(rt.area(3,4) == 6) 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 5.0 13 | 6.0 14 | */ 15 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Imports & Packages" 3 | import com.atomicscala.pythagorean.EquilateralTriangle 4 | 5 | val et = new EquilateralTriangle 6 | println(et.area(2)) 7 | assert(et.area(2) == Math.sqrt(3)) 8 | println(et.area(9.49)) 9 | assert(Math.round(et.area(9.49)) == 39) 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 1.7320508075688772 13 | 38.99716723368346 14 | */ 15 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/Solution-4a.scala: -------------------------------------------------------------------------------- 1 | // Solution-4a.scala 2 | // Solution to Exercise 4 in "Imports & Packages" 3 | 4 | // Solution 1: import 1 class 5 | import com.atomicscala.trivia.Literature 6 | 7 | val lit = new Literature 8 | println("Imported 1 class") 9 | 10 | /* OUTPUT_SHOULD_BE 11 | Imported 1 class 12 | */ 13 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/Solution-4b.scala: -------------------------------------------------------------------------------- 1 | // Solution-4b.scala 2 | // Solution to Exercise 4 in "Imports & Packages" 3 | 4 | // Solution 2: import 2 classes 5 | import com.atomicscala.trivia.{Science, Movies} 6 | 7 | val science = new Science 8 | val movies = new Movies 9 | println("Imported 2 classes") 10 | 11 | /* OUTPUT_SHOULD_BE 12 | Imported 2 classes 13 | */ 14 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/Solution-4c.scala: -------------------------------------------------------------------------------- 1 | // Solution-4c.scala 2 | // Solution to Exercise 4 in "Imports & Packages" 3 | 4 | // Solution 3: import everything 5 | import com.atomicscala.trivia._ 6 | 7 | val c1 = new Literature 8 | val c2 = new Science 9 | val c3 = new Movies 10 | println("Imported everything") 11 | 12 | /* OUTPUT_SHOULD_BE 13 | Imported everything 14 | */ 15 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/Trivia.scala: -------------------------------------------------------------------------------- 1 | package com.atomicscala.trivia 2 | 3 | class Movies 4 | 5 | class Science 6 | 7 | class Literature 8 | -------------------------------------------------------------------------------- /ImportsAndPackages-2ndEdition/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Inheritance/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Inheritance" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Cycle { 6 | val wheels = 2 7 | def ride = "Riding" 8 | } 9 | 10 | class Bicycle extends Cycle 11 | 12 | val c = new Cycle 13 | c.ride is "Riding" 14 | val b = new Bicycle 15 | b.ride is "Riding" 16 | b.wheels is 2 17 | 18 | /* OUTPUT_SHOULD_BE 19 | Riding 20 | Riding 21 | 2 22 | */ 23 | -------------------------------------------------------------------------------- /Inheritance/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Inheritance" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val ape1 = new GreatApe 6 | ape1.vocalize is "Grrr!" 7 | val ape2 = new Bonobo 8 | ape2.vocalize is "Grrr!" 9 | val ape3 = new Chimpanzee 10 | ape3.vocalize is "Grrr!" 11 | -------------------------------------------------------------------------------- /Inheritance/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Inheritance" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | says(new GreatApe) is "says Grrr!" 6 | says(new Bonobo) is "says Grrr!" 7 | says(new Chimpanzee) is "says Grrr!" 8 | says(new BonoboB) is "says Grrr!" 9 | -------------------------------------------------------------------------------- /Inheritance/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Inheritance" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val c = new Cycle 6 | c.ride is "Riding" 7 | val b = new Bicycle 8 | b.ride is "Riding" 9 | b.wheels is 2 10 | -------------------------------------------------------------------------------- /Inheritance/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ListsAndRecursion/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Lists & Recursion" 3 | import com.atomicscala.AtomicTest._ 4 | import Math.max 5 | 6 | def findMax(aList:List[Int], maxVal:Int):Int = 7 | if(aList.isEmpty) 8 | maxVal 9 | else 10 | findMax(aList.tail, max(maxVal, aList.head)) 11 | 12 | val aList = List(10, 20, 45, 15, 30) 13 | findMax(aList, 0) is 45 14 | 15 | /* OUTPUT_SHOULD_BE 16 | 45 17 | */ 18 | -------------------------------------------------------------------------------- /ListsAndRecursion/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Lists & Recursion" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def sumIt(someNumbers:List[Int]):Int = 6 | someNumbers.reduce((total, n) => total + n) 7 | 8 | sumIt(List(1, 2, 3)) is 6 9 | sumIt(List(45, 45, 45, 60)) is 195 10 | 11 | /* ANSWER: This is somewhat subjective. However, it's probably 12 | less effort & debugging to implement the "reduce" solution. */ 13 | 14 | /* OUTPUT_SHOULD_BE 15 | 6 16 | 195 17 | */ 18 | -------------------------------------------------------------------------------- /ListsAndRecursion/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Lists & Recursion" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val aList = List(10, 20, 45, 15, 30) 6 | max(aList) is 45 7 | -------------------------------------------------------------------------------- /ListsAndRecursion/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Lists & Recursion" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | sumIt(List(1, 2, 3)) is 6 6 | sumIt(List(45, 45, 45, 60)) is 195 7 | -------------------------------------------------------------------------------- /ListsAndRecursion/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Lists & Recursion" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | calcFreq(animalList, "cat") is 4 6 | calcFreq(animalList, "dog") is 1 7 | -------------------------------------------------------------------------------- /ListsAndRecursion/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Logging/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Maps/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Maps" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Name(firstName:String, lastName:String) 6 | 7 | val m = Map("sally@taylor.com"->Name("Sally","Taylor"), 8 | "mary@smith.com"->Name("Mary", "Smith")) 9 | 10 | m("sally@taylor.com") is Name("Sally", "Taylor") 11 | 12 | /* OUTPUT_SHOULD_BE 13 | Name(Sally,Taylor) 14 | */ 15 | -------------------------------------------------------------------------------- /Maps/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Maps" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val map1 = Map("English" -> "", "French" -> "", "Spanish" -> "", "German" -> "", "Chinese" -> "") 6 | map1.size is 5 7 | val map2 = map1 + ("Turkish" -> "") 8 | map2.size is 6 9 | map2.contains("English") is true 10 | map2.contains("Turkish") is true 11 | 12 | /* OUTPUT_SHOULD_BE 13 | 5 14 | 6 15 | true 16 | true 17 | */ 18 | -------------------------------------------------------------------------------- /Maps/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Maps" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val map1 = Map("English" -> "", "French" -> "", "Spanish" -> "", "German" -> "", "Chinese" -> "") 6 | map1.size is 5 7 | val map2 = map1 + ("French" -> "") 8 | map2.size is 5 9 | map2.contains("English") is true 10 | map2.contains("French") is true 11 | 12 | /* OUTPUT_SHOULD_BE 13 | 5 14 | 5 15 | true 16 | true 17 | */ 18 | -------------------------------------------------------------------------------- /Maps/Solution-7.scala: -------------------------------------------------------------------------------- 1 | // Solution-7.scala 2 | // Solution to Exercise 7 in "Maps" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Person(name:String) 6 | 7 | val m = Map(Person("Bob") -> "CEO", Person("Janice") -> "CFO") 8 | 9 | m(Person("Janice")) is "CFO" 10 | 11 | /* OUTPUT_SHOULD_BE 12 | CFO 13 | */ 14 | -------------------------------------------------------------------------------- /Maps/Solution-7b.scala: -------------------------------------------------------------------------------- 1 | // Solution-7b.scala 2 | // Solution to Exercise 7 in "Maps" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Person(name:String) 6 | 7 | val m = Map(new Person("Bob") -> "CEO", new Person("Janice") -> "CFO") 8 | 9 | // Uncomment this: 10 | // m(new Person("Janice")) is "CFO" 11 | // And your error message will contain this: 12 | // java.util.NoSuchElementException: key not found: Main$$anon$1$Person 13 | 14 | /* OUTPUT_SHOULD_BE 15 | 16 | */ // Dummy to make test pass 17 | -------------------------------------------------------------------------------- /Maps/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Maps" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val m = Map("sally@taylor.com" -> Name("Sally","Taylor")) 6 | m("sally@taylor.com") is Name("Sally", "Taylor") 7 | -------------------------------------------------------------------------------- /Maps/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Maps" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | m2("jiminy@cricket.com") is Name("Jiminy", "Cricket") 6 | m2("sally@taylor.com") is Name("Sally", "Taylor") 7 | -------------------------------------------------------------------------------- /Maps/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Methods/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Methods" 3 | 4 | def getSquare(num:Int):Int = { 5 | num * num 6 | } 7 | 8 | val a = getSquare(3) 9 | println(a) 10 | assert(a == 9, "Expected 9, Got " + a) 11 | val b = getSquare(6) 12 | println(b) 13 | assert(b == 36, "Expected 36, Got " + b) 14 | val c = getSquare(5) 15 | println(c) 16 | assert(c == 25, "Expected 25, Got " + c) 17 | 18 | /* OUTPUT_SHOULD_BE 19 | 9 20 | 36 21 | 25 22 | */ 23 | -------------------------------------------------------------------------------- /Methods/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Methods" 3 | 4 | def getSquareDouble(num:Double):Double = { 5 | num * num 6 | } 7 | 8 | val sd1 = getSquareDouble(1.2) 9 | println(sd1) 10 | assert(1.44 == sd1, "Expected 1.44, Got " + sd1) 11 | val sd2 = getSquareDouble(5.7) 12 | println(sd2) 13 | assert(32.49 == sd2, "Expected 32.49, Got " + sd2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | 1.44 17 | 32.49 18 | */ 19 | -------------------------------------------------------------------------------- /Methods/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Methods" 3 | 4 | def isArg1GreaterThanArg2(num1:Double, num2:Double):Boolean = { 5 | num1 > num2 6 | } 7 | 8 | val t1 = isArg1GreaterThanArg2(4.1, 4.12) 9 | println(t1) 10 | assert(false == t1, "Expected false, Got " + t1) 11 | val t2 = isArg1GreaterThanArg2(2.1, 1.2) 12 | println(t2) 13 | assert(true == t2, "Expected true, Got " + t2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | false 17 | true 18 | */ 19 | -------------------------------------------------------------------------------- /Methods/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Methods" 3 | 4 | def getMe(str:String):String = { 5 | str.toLowerCase 6 | } 7 | 8 | val g1 = getMe("abraCaDabra") 9 | println(g1) 10 | assert("abracadabra" == g1, "Expected abracadabra, Got " + g1) 11 | val g2 = getMe("zyxwVUT") 12 | println(g2) 13 | assert("zyxwvut"== g2, "Expected zyxwvut, Got " + g2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | abracadabra 17 | zyxwvut 18 | */ 19 | -------------------------------------------------------------------------------- /Methods/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Methods" 3 | 4 | def addStrings(str1:String, str2:String):String = { 5 | str1 + str2 6 | } 7 | 8 | val s1 = addStrings("abc", "def") 9 | println(s1) 10 | assert("abcdef" == s1, "Expected abcdef, Got " + s1) 11 | val s2 = addStrings("zyx", "abc") 12 | println(s2) 13 | assert("zyxabc" == s2, "Expected zyxabc, Got " + s2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | abcdef 17 | zyxabc 18 | */ 19 | -------------------------------------------------------------------------------- /Methods/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "Methods" 3 | 4 | def manyTimesString(str:String, num:Int):String = { 5 | str * num 6 | } 7 | 8 | val m1 = manyTimesString("abc", 3) 9 | println(m1) 10 | assert("abcabcabc" == m1, "Expected abcabcabc, Got " + m1) 11 | val m2 = manyTimesString("123", 2) 12 | println(m2) 13 | assert("123123" == m2, "Expected 123123, Got " + m2) 14 | 15 | /* OUTPUT_SHOULD_BE 16 | abcabcabc 17 | 123123 18 | */ 19 | -------------------------------------------------------------------------------- /Methods/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Methods" atom 3 | 4 | val a = getSquare(3) 5 | assert(/* fill this in */) 6 | val b = getSquare(6) 7 | assert(/* fill this in */) 8 | val c = getSquare(5) 9 | assert(/* fill this in */) 10 | -------------------------------------------------------------------------------- /Methods/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Methods" atom 3 | 4 | val sd1 = getSquareDouble(1.2) 5 | assert(1.44 == sd1, "Your message here") 6 | val sd2 = getSquareDouble(5.7) 7 | assert(32.49 == sd2, "Your message here") 8 | -------------------------------------------------------------------------------- /Methods/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Methods" atom 3 | 4 | val t1 = isArg1GreaterThanArg2(4.1, 4.12) 5 | assert(/* fill this in */) 6 | val t2 = isArg1GreaterThanArg2(2.1, 1.2) 7 | assert(/* fill this in */) 8 | -------------------------------------------------------------------------------- /Methods/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Methods" atom 3 | 4 | val g1 = getMe("abraCaDabra") 5 | assert("abracadabra" == g1, "Your message here") 6 | val g2 = getMe("zyxwVUT") 7 | assert("zyxwvut"== g2, "Your message here") 8 | -------------------------------------------------------------------------------- /Methods/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Methods" atom 3 | 4 | val s1 = addStrings("abc", "def") 5 | assert(/* fill this in */) 6 | val s2 = addStrings("zyx", "abc") 7 | assert(/* fill this in */) 8 | -------------------------------------------------------------------------------- /Methods/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Methods" atom 3 | 4 | val m1 = manyTimesString("abc", 3) 5 | assert("abcabcabc" == m1, "Your message here") 6 | val m2 = manyTimesString("123", 2) 7 | assert("123123" == m2, "Your message here") 8 | -------------------------------------------------------------------------------- /Methods/Starter-7.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 7 2 | // From "Methods" atom 3 | 4 | val normal = bmiStatus(160, 68) 5 | assert("Normal weight" == normal, 6 | "Expected Normal weight, Got " + normal) 7 | val overweight = bmiStatus(180, 60) 8 | assert("Overweight" == overweight, 9 | "Expected Overweight, Got " + overweight) 10 | val underweight = bmiStatus(100, 68) 11 | assert("Underweight" == underweight, 12 | "Expected Underweight, Got " + underweight) 13 | -------------------------------------------------------------------------------- /Methods/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /MethodsInsideClasses/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Methods Inside Classes" 3 | 4 | class Flare { 5 | def light():String = { "Flare used!"} 6 | } 7 | 8 | val flare = new Flare 9 | val f1 = flare.light 10 | assert(f1 == "Flare used!", "Expected Flare used!, Got " + f1) 11 | println(f1) 12 | 13 | /* OUTPUT_SHOULD_BE 14 | Flare used! 15 | */ 16 | -------------------------------------------------------------------------------- /MethodsInsideClasses/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Methods Inside Classes" atom 3 | 4 | val flare = new Flare 5 | val f1 = flare.light 6 | assert(f1 == "Flare used!", "Expected Flare used!, Got " + f1) 7 | -------------------------------------------------------------------------------- /MethodsInsideClasses/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Methods Inside Classes" atom 3 | 4 | val sailboat2 = new Sailboat2 5 | val signal = sailboat2.signal() 6 | assert(signal == "Flare used!", "Expected Flare used! Got " + signal) 7 | val motorboat2 = new Motorboat2 8 | val flare2 = motorboat2.signal() 9 | assert(flare2 == "Flare used!", "Expected Flare used!, Got " + flare2) 10 | -------------------------------------------------------------------------------- /MethodsInsideClasses/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /MoreConditionals/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "More Conditionals" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def isPalindrome(str:String):Boolean = { 6 | val s = str.reverse 7 | s.equals(str) 8 | } 9 | 10 | isPalindrome("mom") is true 11 | isPalindrome("dad") is true 12 | isPalindrome("street") is false 13 | 14 | /* OUTPUT_SHOULD_BE 15 | true 16 | true 17 | false 18 | */ 19 | -------------------------------------------------------------------------------- /MoreConditionals/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "More Conditionals" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def isPalIgnoreCase(str:String):Boolean = { 6 | val s = str.toUpperCase.reverse 7 | s.equals(str.toUpperCase) 8 | } 9 | 10 | isPalIgnoreCase("Bob") is true 11 | isPalIgnoreCase("DAD") is true 12 | isPalIgnoreCase("Blob") is false 13 | 14 | /* OUTPUT_SHOULD_BE 15 | true 16 | true 17 | false 18 | */ 19 | -------------------------------------------------------------------------------- /MoreConditionals/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "More Conditionals" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | isPalindrome("mom") is true 6 | isPalindrome("dad") is true 7 | isPalindrome("street") is false 8 | -------------------------------------------------------------------------------- /MoreConditionals/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "More Conditionals" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | isPalIgnoreCase("Bob") is true 6 | isPalIgnoreCase("DAD") is true 7 | isPalIgnoreCase("Blob") is false 8 | -------------------------------------------------------------------------------- /MoreConditionals/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "More Conditionals" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var createdStr = "" 6 | 7 | for(c <- str) { 8 | // Convert to Int for comparison: 9 | val theValue = c.toInt 10 | if(/* check for letters */) { 11 | createdStr += c 12 | } 13 | else if(/* Check for numbers */) { 14 | createdStr += c 15 | } 16 | } 17 | 18 | isPalIgnoreSpecial("Madam I'm adam") is true 19 | isPalIgnoreSpecial("trees") is false 20 | -------------------------------------------------------------------------------- /MoreConditionals/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Named & Default Arguments" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class SimpleTime(val hours:Int, val minutes:Int) 6 | 7 | val t = new SimpleTime(hours=5, minutes=30) 8 | t.hours is 5 9 | t.minutes is 30 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 5 13 | 30 14 | */ 15 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Named & Default Arguments" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class SimpleTime2(val hours:Int, val minutes:Int = 0) 6 | 7 | val t2 = new SimpleTime2(hours=10) 8 | t2.hours is 10 9 | t2.minutes is 0 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 10 13 | 0 14 | */ 15 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Named & Default Arguments" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Planet(val name:String, val description:String, val moons:Int = 1) { 6 | def hasMoon():Boolean = { 7 | if(moons > 0) { 8 | true 9 | } else { 10 | false 11 | } 12 | } 13 | } 14 | 15 | val p = new Planet(name = "Mercury", description = "small and hot planet", moons = 0) 16 | p.hasMoon is false 17 | 18 | /* OUTPUT_SHOULD_BE 19 | false 20 | */ 21 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Named & Default Arguments" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Planet(val name:String, val description:String, val moons:Int = 1) { 6 | def hasMoon():Boolean = { 7 | if(moons > 0) true else false 8 | } 9 | } 10 | 11 | val earth = new Planet(moons = 1, name="Earth", description = "a hospitable planet") 12 | earth.hasMoon is true 13 | 14 | /* OUTPUT_SHOULD_BE 15 | true 16 | */ 17 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Named & Default Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val t = new SimpleTime(hours=5, minutes=30) 6 | t.hours is 5 7 | t.minutes is 30 8 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Named & Default Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val t2 = new SimpleTime2(hours=10) 6 | t2.hours is 10 7 | t2.minutes is 0 8 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Named & Default Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val p = new Planet(name = "Mercury", description = "small and hot planet", moons = 0) 6 | p.hasMoon is false 7 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Named & Default Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val earth = new Planet(moons = 1, name = "Earth", description = "a hospitable planet") 6 | earth.hasMoon is true 7 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Named & Default Arguments" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val flour = new Item(name = "flour", 4) 6 | flour.cost(grocery = true) is 4 7 | val sunscreen = new Item(name = "sunscreen", 3) 8 | sunscreen.cost() is 3.3 9 | val tv = new Item(name = "television", 500) 10 | tv.cost(taxRate = 0.06) is 530 11 | -------------------------------------------------------------------------------- /NamedAndDefaultArguments/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Overloading/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Overloading" 3 | 4 | // This won't work -- redefining functions 5 | 6 | def f(n:Int):Int = { 77 } 7 | def f(n:Int):Int = { n + 1 } 8 | 9 | class MethodOverloading1 { 10 | def f(n:Int):Int = { 88 } 11 | def f(n:Int):Int = { n + 2 } 12 | } 13 | 14 | /* OUTPUT_SHOULD_CONTAIN 15 | error: method f is defined twice 16 | error: method f is defined twice 17 | two errors found 18 | */ 19 | -------------------------------------------------------------------------------- /Overloading/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Overloading" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def f():Int = { 0 } 6 | def f(i:Int):Int = { i } 7 | def f(i:Int, j:Int):Int = { i + j } 8 | def f(i:Int, j:Int, k:Int):Int = { i + j + k } 9 | def f(i:Int, j:Int, k:Int, l:Int):Int = { i + j + k + l } 10 | 11 | f() is 0 12 | f(1) is 1 13 | f(1, 2) is 3 14 | f(1, 2, 3) is 6 15 | f(1, 2, 3, 4) is 10 16 | 17 | /* OUTPUT_SHOULD_BE 18 | 0 19 | 1 20 | 3 21 | 6 22 | 10 23 | */ 24 | -------------------------------------------------------------------------------- /Overloading/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Overloading" 3 | 4 | class FunNumbers2 { 5 | def f(i:Int, j:Int):Double = { i + j } 6 | // This won't work (redefined function, different only in return type): 7 | def f(i:Int, j:Int):Int = { i + j } 8 | } 9 | 10 | 11 | /* OUTPUT_SHOULD_CONTAIN 12 | error: method f is defined twice 13 | def f(i:Int, j:Int):Int = { i + j } 14 | ^ 15 | one error found 16 | */ 17 | -------------------------------------------------------------------------------- /Overloading/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Overloading" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | f() is 0 6 | f(1) is 1 7 | f(1, 2) is 3 8 | f(1, 2, 3) is 6 9 | f(1, 2, 3, 4) is 10 10 | -------------------------------------------------------------------------------- /Overloading/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /OverridingMethods/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Overriding Methods" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | println("""eat() modifies the variable energy. 6 | It is idiomatic Scala to use parentheses 7 | in the method definition if it alters state.""") 8 | 9 | /* OUTPUT_SHOULD_BE 10 | eat() modifies the variable energy. 11 | It is idiomatic Scala to use parentheses 12 | in the method definition if it alters state. 13 | */ 14 | -------------------------------------------------------------------------------- /OverridingMethods/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Overriding Methods" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val roaringApe = new GreatApe2(112, 9, "Male") 6 | roaringApe.myWords is Vector("Roar") 7 | val chattyBonobo = new Bonobo2(150, 14, "Female") 8 | chattyBonobo.myWords is Vector("Roar", "Hello") 9 | -------------------------------------------------------------------------------- /OverridingMethods/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ParameterizedTypes/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Parameterized Types" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def explicitDouble(v1:Double, v2:Double, v3:Double):Vector[Double] = { 6 | Vector(v1, v2, v3) 7 | } 8 | 9 | explicitDouble(1, 2, 3) is Vector(1.0, 2.0, 3.0) 10 | 11 | /* OUTPUT_SHOULD_BE 12 | Vector(1.0, 2.0, 3.0) 13 | */ 14 | -------------------------------------------------------------------------------- /ParameterizedTypes/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Parameterized Types" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def explicitList(v1:Vector[Double]):List[Double] = { 6 | v1.toList 7 | } 8 | 9 | explicitList(Vector(10.0, 20.0)) is List(10.0, 20.0) 10 | explicitList(Vector(1, 2, 3)) is List(1.0, 2.0, 3.0) 11 | 12 | /* OUTPUT_SHOULD_BE 13 | List(10.0, 20.0) 14 | List(1.0, 2.0, 3.0) 15 | */ 16 | -------------------------------------------------------------------------------- /ParameterizedTypes/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Parameterized Types" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def explicitSet(v1:Vector[Double]):Set[Double] = { 6 | v1.toSet 7 | } 8 | 9 | explicitSet(Vector(10.0, 20.0, 10.0)) is Set(10.0, 20.0) 10 | explicitSet(Vector(1, 2, 3, 2, 3, 4)) is Set(1.0, 2.0, 3.0, 4.0) 11 | 12 | /* OUTPUT_SHOULD_BE 13 | Set(10.0, 20.0) 14 | Set(1.0, 2.0, 3.0, 4.0) 15 | */ 16 | -------------------------------------------------------------------------------- /ParameterizedTypes/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Parameterized Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | explicitDouble(1.0, 2.0, 3.0) is Vector(1.0, 2.0, 3.0) 6 | -------------------------------------------------------------------------------- /ParameterizedTypes/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Parameterized Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | explicitList(Vector(10.0, 20.0)) is List(10.0, 20.0) 6 | explicitList(Vector(1, 2, 3)) is List(1.0, 2.0, 3.0) 7 | -------------------------------------------------------------------------------- /ParameterizedTypes/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Parameterized Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | explicitSet(Vector(10.0, 20.0, 10.0)) is Set(10.0, 20.0) 6 | explicitSet(Vector(1, 2, 3, 2, 3, 4)) is Set(1.0, 2.0, 3.0, 4.0) 7 | -------------------------------------------------------------------------------- /ParameterizedTypes/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Parameterized Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val weather = Vector(100, 80, 20, 100, 20) 6 | historicalData(weather) is "Sunny=2, Mostly Sunny=1, Mostly Cloudy=2" 7 | -------------------------------------------------------------------------------- /ParameterizedTypes/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /PatternMatching/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Pattern Matching" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def matchColor(color:String):String = { 6 | if(color == "red") "RED" 7 | else if(color == "blue") "BLUE" 8 | else if(color == "green") "GREEN" 9 | else "UNKNOWN COLOR: " + color 10 | } 11 | 12 | matchColor("white") is "UNKNOWN COLOR: white" 13 | matchColor("blue") is "BLUE" 14 | 15 | /* OUTPUT_SHOULD_BE 16 | UNKNOWN COLOR: white 17 | BLUE 18 | */ 19 | -------------------------------------------------------------------------------- /PatternMatching/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Pattern Matching" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def oneOrTheOther(exp:Boolean):String = { 6 | exp match { 7 | case true =>"True!" // No 'return' 8 | case _ => "It's false" 9 | } 10 | } 11 | 12 | val v = Vector(1) 13 | val v2 = Vector(3, 4) 14 | oneOrTheOther(v == v.reverse) is "True!" 15 | oneOrTheOther(v2 == v2.reverse) is "It's false" 16 | 17 | /* OUTPUT_SHOULD_BE 18 | True! 19 | It's false 20 | */ 21 | -------------------------------------------------------------------------------- /PatternMatching/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Pattern Matching" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | matchColor("white") is "UNKNOWN COLOR: white" 6 | matchColor("blue") is "BLUE" 7 | -------------------------------------------------------------------------------- /PatternMatching/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Pattern Matching" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v = Vector(1) 6 | val v2 = Vector(3, 4) 7 | oneOrTheOther(v == v.reverse) is "True!" 8 | oneOrTheOther(v2 == v2.reverse) is "It's false" 9 | -------------------------------------------------------------------------------- /PatternMatching/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Pattern Matching" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | checkTruth(true || false, true) is "Both are true" 6 | checkTruth(1 > 0 && -1 < 0, 1 == 2) is "First: true, second: false" 7 | checkTruth(1 >= 2, 1 >= 1) is "First: false, second: true" 8 | checkTruth(true && false, false && true) is "Both are false" 9 | -------------------------------------------------------------------------------- /PatternMatching/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Pattern Matching" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | forecast(100) is "Sunny" 6 | forecast(80) is "Mostly Sunny" 7 | forecast(50) is "Partly Sunny" 8 | forecast(20) is "Mostly Cloudy" 9 | forecast(0) is "Cloudy" 10 | forecast(15) is "Unknown" 11 | -------------------------------------------------------------------------------- /PatternMatching/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /PatternMatchingwithCaseClasses/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Pattern Matching with Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val trip2 = Vector( 6 | Train(travelers, "Reading"), 7 | Plane(travelers, "B757"), 8 | Bus(travelers, 100)) 9 | 10 | travel(trip2(1)) is "Plane B757 Vector(Passenger(Harvey,Rabbit), Passenger(Dorothy,Gale))" 11 | -------------------------------------------------------------------------------- /PatternMatchingwithCaseClasses/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Pattern Matching with Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | travel2(Passenger("Sally", "Marie")) is "Sally is walking" 6 | -------------------------------------------------------------------------------- /PatternMatchingwithCaseClasses/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Pattern Matching with Case Classes" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Kitten(name:String) 6 | 7 | travel2(Kitten("Kitty")) is "Kitten(Kitty) is in limbo!" 8 | -------------------------------------------------------------------------------- /PatternMatchingwithCaseClasses/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /PatternMatchingwithTuples/PaintColors.scala: -------------------------------------------------------------------------------- 1 | // PaintColors.scala 2 | package paintcolors 3 | 4 | object Color extends Enumeration { 5 | type Color = Value 6 | val red, blue, yellow, purple, 7 | green, orange, brown = Value 8 | } 9 | -------------------------------------------------------------------------------- /PatternMatchingwithTuples/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Pattern Matching with Tuples" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | blend(red, yellow) is orange 6 | blend(red, red) is red 7 | blend(yellow,blue) is green 8 | -------------------------------------------------------------------------------- /PatternMatchingwithTuples/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Pattern Matching with Tuples" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | blend2(red, yellow) is orange 6 | blend2(red, red) is red 7 | blend2(yellow,blue) is green 8 | blend2(yellow, magenta) is purple 9 | blend2(red, magenta) is purple 10 | -------------------------------------------------------------------------------- /PatternMatchingwithTuples/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Pattern Matching with Tuples" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | blend3(red, yellow) is orange 6 | blend3(red, red) is red 7 | blend3(yellow,blue) is green 8 | blend3(yellow, magenta) is purple 9 | blend3(red, magenta) is purple 10 | blend3(purple, white) is purple 11 | blend3(white, red) is red 12 | -------------------------------------------------------------------------------- /PatternMatchingwithTuples/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /PatternMatchingwithTypes/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Pattern Matching with Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | plus1("car") is "cars" 6 | plus1(67) is 68 7 | plus1(Person("Joanna")) is "Person(Joanna) + guest" 8 | -------------------------------------------------------------------------------- /PatternMatchingwithTypes/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Pattern Matching with Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | convertToSize(45) is 45 6 | convertToSize("car") is 3 7 | convertToSize("truck") is 5 8 | convertToSize(Person("Joanna")) is 1 9 | convertToSize(45.6F) is 45.6F 10 | convertToSize(Vector(1, 2, 3)) is 0 11 | -------------------------------------------------------------------------------- /PatternMatchingwithTypes/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Pattern Matching with Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | convertToSize2(45) is 45 6 | convertToSize2("car") is 3 7 | convertToSize2("truck") is 5 8 | convertToSize2(Person("Joanna")) is 1 9 | convertToSize2(45.6F) is 46 10 | convertToSize2(Vector(1, 2, 3)) is 0 11 | -------------------------------------------------------------------------------- /PatternMatchingwithTypes/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Pattern Matching with Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | quantify(100) is "medium" 6 | quantify(20.56) is "small" 7 | quantify(100000) is "large" 8 | quantify(-15999) is "small" 9 | -------------------------------------------------------------------------------- /PatternMatchingwithTypes/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Pattern Matching with Types" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | forecast(100) is "Sunny" 6 | forecast(81) is "Sunny" 7 | forecast(80) is "Mostly Sunny" 8 | forecast(51) is "Mostly Sunny" 9 | forecast(50) is "Partly Sunny" 10 | forecast(21) is "Partly Sunny" 11 | forecast(20) is "Mostly Cloudy" 12 | forecast(1) is "Mostly Cloudy" 13 | forecast(0) is "Cloudy" 14 | forecast(-1) is "Unknown" 15 | -------------------------------------------------------------------------------- /PatternMatchingwithTypes/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Polymorphism/Name.scala: -------------------------------------------------------------------------------- 1 | // Name.scala 2 | package com.atomicscala 3 | import reflect.runtime.currentMirror 4 | 5 | object Name { 6 | def className(o:Any) = 7 | currentMirror.reflect(o).symbol. 8 | toString.replace('$', ' ').split(' ').last 9 | } 10 | 11 | trait Name { 12 | override def toString = Name.className(this) 13 | } 14 | -------------------------------------------------------------------------------- /Polymorphism/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Polymorphism" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val e = new Element 6 | e.draw is "Drawing the element" 7 | val in = new Inert 8 | in.draw is "Inert drawing!" 9 | val wall = new Wall 10 | wall.draw is "Inert drawing!" 11 | -------------------------------------------------------------------------------- /Polymorphism/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Polymorphism" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val wall = new Wall 6 | wall.draw is "Don't draw on the wall!" 7 | -------------------------------------------------------------------------------- /Polymorphism/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Polymorphism" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Character(val player:String="None") extends Element 6 | // Change the next line 7 | class Dragon extends Character 8 | val d = new Dragon("Puff") 9 | d.player is "Puff" 10 | -------------------------------------------------------------------------------- /Polymorphism/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Polymorphism" atom (2nd edition) 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val garden = new Garden(new Tomato, new Corn, new Zucchini) 6 | garden is "Tomato, Corn, Zucchini" 7 | -------------------------------------------------------------------------------- /Polymorphism/Starter-6-1stEdition.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Polymorphism" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val d3 = new NoMagicNoFlyingDragon 6 | d3.player is "Puff" 7 | d3.id is "NoMagicNoFlyingDragon" 8 | -------------------------------------------------------------------------------- /Polymorphism/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Polymorphism" atom (2nd edition) 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val drawing = new Drawing( 6 | new Rectangle, new Square, 7 | new Ellipse, new Circle) 8 | drawing.draw is "Vector(Rectangle," + 9 | " Square, Ellipse, Circle)" 10 | drawing is "Rectangle, Square," + 11 | " Ellipse, Circle" 12 | -------------------------------------------------------------------------------- /Polymorphism/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ReachingintoJava/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Reaching into Java" 3 | import com.atomicscala.AtomicTest._ 4 | import java.text.SimpleDateFormat 5 | 6 | val datePattern = new SimpleDateFormat("MM/dd/yy") 7 | val mayDay = datePattern.parse("05/01/12") 8 | mayDay.getDate is 1 9 | mayDay.getMonth is 4 10 | 11 | /* OUTPUT_SHOULD_BE 12 | warning: there were two deprecation warnings; re-run with -deprecation for details 13 | one warning found 14 | 1 15 | 4 16 | */ 17 | -------------------------------------------------------------------------------- /ReachingintoJava/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Reaching into Java" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | println("""ANSWER: Months start with 0. 6 | Days start with 1. 7 | It's horribly inconsistent.""") 8 | 9 | /* OUTPUT_SHOULD_BE 10 | ANSWER: Months start with 0. 11 | Days start with 1. 12 | It's horribly inconsistent. 13 | */ 14 | -------------------------------------------------------------------------------- /ReachingintoJava/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Reaching into Java" 3 | import com.atomicscala.AtomicTest._ 4 | import org.apache.commons.math3._ 5 | import stat.descriptive._ 6 | 7 | val s = new SummaryStatistics 8 | // add values here 9 | s.addValue(10) 10 | s.addValue(20) 11 | s.addValue(30) 12 | s.addValue(80) 13 | s.addValue(90) 14 | s.addValue(100) 15 | 16 | s.getMean is 55 17 | s.getStandardDeviation is 39.370039370059054 18 | 19 | /* OUTPUT_SHOULD_BE 20 | 55.0 21 | 39.370039370059054 22 | */ 23 | -------------------------------------------------------------------------------- /ReachingintoJava/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Reaching into Java" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val mayDay = datePattern.parse("05/01/12") 6 | mayDay.getDate is 1 7 | mayDay.getMonth is 4 8 | -------------------------------------------------------------------------------- /ReachingintoJava/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Reaching into Java" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val f = new Frequency 6 | // add values for cat, dog, cat, bird, 7 | // cat, cat, kitten, mouse here 8 | f.getCount("cat") is 4 9 | -------------------------------------------------------------------------------- /ReachingintoJava/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Reaching into Java" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val s = new SummaryStatistics 6 | // add values here 7 | s.getMean is 55 8 | s.getStandardDeviation is 39.370039370059054 9 | -------------------------------------------------------------------------------- /ReachingintoJava/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ReferencesAndMutability/Solution-1a.scala: -------------------------------------------------------------------------------- 1 | // Solution-1a.scala 2 | // Solution to Exercise 1 in "References & Mutability" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var m = Map("Foo" -> "Bar") 6 | 7 | m("Goat") = "Calico" // Try to add a new pair 8 | 9 | /* OUTPUT_SHOULD_CONTAIN 10 | error: value update is not a member of scala.collection.immutable.Map[String,String] 11 | m("Goat") = "Calico" // Try to add a new pair 12 | ^ 13 | one error found 14 | */ 15 | -------------------------------------------------------------------------------- /ReferencesAndMutability/Solution-1b.scala: -------------------------------------------------------------------------------- 1 | // Solution-1b.scala 2 | // Solution to Exercise 1 in "References & Mutability" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var m = Map("Foo" -> "Bar") 6 | 7 | m("Foo") = "Zub" // Try to modify an existing element 8 | 9 | /* OUTPUT_SHOULD_CONTAIN 10 | error: value update is not a member of scala.collection.immutable.Map[String,String] 11 | m("Foo") = "Zub" // Try to modify an existing element 12 | ^ 13 | one error found 14 | */ 15 | -------------------------------------------------------------------------------- /ReferencesAndMutability/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "References & Mutability" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class K 6 | 7 | def f(k:K) = k = new K 8 | 9 | /* OUTPUT_SHOULD_CONTAIN 10 | error: reassignment to val 11 | def f(k:K) = k = new K 12 | ^ 13 | one error found 14 | */ 15 | -------------------------------------------------------------------------------- /ReferencesAndMutability/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "References & Mutability" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Foo(var x:Int) 6 | 7 | def modarg(f:Foo) = f.x += 1 8 | 9 | val z = new Foo(1) 10 | z.x is 1 11 | modarg(z) 12 | z.x is 2 13 | println("Yes, modarg has side effects") 14 | 15 | /* OUTPUT_SHOULD_BE 16 | 1 17 | 2 18 | Yes, modarg has side effects 19 | */ 20 | -------------------------------------------------------------------------------- /ReferencesAndMutability/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Sequences/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Sequences" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Person(first:String, last:String, email:String) { 6 | override def toString = s"$first $last" 7 | val fullName = toString 8 | } 9 | 10 | val p = Person("John", "Smith", "john@smith.com") 11 | p.fullName is "John Smith" 12 | p.first is "John" 13 | p.email is "john@smith.com" 14 | 15 | /* OUTPUT_SHOULD_BE 16 | John Smith 17 | John 18 | john@smith.com 19 | */ 20 | -------------------------------------------------------------------------------- /Sequences/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Sequences" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | case class Person(first:String, last:String, email:String) { 6 | override def toString = s"$first $last" 7 | val fullName = toString 8 | } 9 | 10 | val people = Vector( 11 | Person("John", "Smith", "john@smith.com"), 12 | Person("Mary", "Smith", "mary@smith.com"), 13 | Person("Sally", "Small", "sally@small.com") 14 | ) 15 | 16 | people.size is 3 17 | 18 | /* OUTPUT_SHOULD_BE 19 | 3 20 | */ 21 | -------------------------------------------------------------------------------- /Sequences/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Sequences" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val p = Person("John", "Smith", "john@smith.com") 6 | p.fullName is "John Smith" 7 | p.first is "John" 8 | p.email is "john@smith.com" 9 | -------------------------------------------------------------------------------- /Sequences/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Sequences" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | people.size is 3 6 | -------------------------------------------------------------------------------- /Sequences/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Sequences" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val people = Vector( 6 | Person("Zach","Smith","zach@smith.com"), 7 | Person("Mary", "Add", "mary@add.com"), 8 | Person("Sally", "Taylor", "sally@taylor.com") 9 | ) 10 | 11 | val sorted = // call sort here 12 | 13 | sorted is "Vector(" + 14 | "Person(Mary,Add,mary@add.com)," + 15 | "Person(Zach,Smith,zach@smith.com)," + 16 | "Person(Sally,Taylor,sally@taylor.com))" 17 | -------------------------------------------------------------------------------- /Sequences/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Sequences" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val friends = Vector( 6 | new Friend("Zach", "Smith", "zach@smith.com"), 7 | new Friend("Mary", "Add", "mary@add.com"), 8 | new Friend("Sally","Taylor","sally@taylor.com") 9 | ) 10 | 11 | val sorted = // call sort here 12 | sorted is "Vector(Mary Add, Sally Taylor, Zach Smith)" 13 | -------------------------------------------------------------------------------- /Sequences/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Sequences" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val friends2 = Vector( 6 | new Friend("Zach", "Smith", "zach@smith.com"), 7 | new Friend("Mary", "Add", "mary@add.com"), 8 | new Friend("Sally","Taylor","sally@taylor.com"), 9 | new Friend("Mary", "Smith", "mary@smith.com") 10 | ) 11 | 12 | val s1 = // call first sort here 13 | val s2 = // sort s1 here 14 | s2 is "Vector(Mary Add, Mary Smith, Zach Smith, Sally Taylor)" 15 | -------------------------------------------------------------------------------- /Sequences/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Sequences" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val friends3 = Vector( 6 | new Friend("Zach", "Smith", "zach@smith.com"), 7 | new Friend("Mary", "Add", "mary@add.com"), 8 | new Friend("Sally","Taylor","sally@taylor.com"), 9 | new Friend("Mary", "Smith", "mary@smith.com") 10 | ) 11 | 12 | val s3 = // Perform first sort here 13 | val s4 = // Sort s3 here 14 | s4 is "Vector(Mary Add, Mary Smith, Sally Taylor, Zach Smith)" 15 | -------------------------------------------------------------------------------- /Sequences/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Sets/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Sets" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val vegetarian = Set("kidney beans", "black beans", "tofu") 6 | val groceryCart2 = Set("apple", "pretzels", 7 | "bread", "orange", "beef", 8 | "beans", "asparagus", "sweet potatoes", 9 | "kidney beans", "black beans") 10 | percentMeat(groceryCart2) is 10.0 11 | percentVegetarian(groceryCart2) is 20.0 12 | percentProtein(groceryCart2) is 30.0 13 | -------------------------------------------------------------------------------- /Sets/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /StringInterpolation/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "String Interpolation" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val gnome = new GardenGnome(20.0, 110.0, false) 6 | gnome.show() is "20.0 110.0 false true" 7 | val bob = new GardenGnome("Bob") 8 | bob.show() is "15.0 100.0 true true" 9 | -------------------------------------------------------------------------------- /StringInterpolation/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "String Interpolation" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val gnome = new GardenGnome(20.0, 50.0, false) 6 | gnome.show(87) is "Poof! 87 false true" 7 | val bob = new GardenGnome("Bob") 8 | bob.show(25) is "Poof! 25 true true" 9 | -------------------------------------------------------------------------------- /StringInterpolation/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "String Interpolation" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val gnome = new GardenGnome(20.0, 110.0, false) 6 | gnome.show() is "height: 20.0 weight: 110.0 happy: false painted: true" 7 | val bob = new GardenGnome("Bob") 8 | bob.show() is "height: 15.0 weight: 100.0 true true" 9 | -------------------------------------------------------------------------------- /StringInterpolation/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Summary1/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | val i = 7 8 | println(i) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 7 12 | */ 13 | -------------------------------------------------------------------------------- /Summary1/Solution-10.scala: -------------------------------------------------------------------------------- 1 | // Solution-10.scala 2 | // Solution to Exercise 10 in "Summary 1" 3 | 4 | val aHeight = 68.0 5 | val idealBMI = 22.0 6 | val idealWeight = { 7 | idealBMI * (aHeight*aHeight) / 703.07 8 | } 9 | println(idealWeight) 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 144.69114028475116 13 | */ 14 | -------------------------------------------------------------------------------- /Summary1/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | val i = 7 8 | i = 5 9 | 10 | /* OUTPUT_SHOULD_CONTAIN 11 | error: reassignment to val 12 | i = 5 13 | ^ 14 | one error found 15 | */ 16 | -------------------------------------------------------------------------------- /Summary1/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | var j = 67 8 | j = 98.2 9 | 10 | /* OUTPUT_SHOULD_CONTAIN 11 | error: type mismatch; 12 | found : Double(98.2) 13 | required: Int 14 | j = 98.2 15 | ^ 16 | one error found 17 | */ 18 | -------------------------------------------------------------------------------- /Summary1/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | val d = 45d 8 | println(d) 9 | 10 | val d2:Double = 64.0 11 | println(d2) 12 | 13 | /* OUTPUT_SHOULD_BE 14 | 45.0 15 | 64.0 16 | */ 17 | -------------------------------------------------------------------------------- /Summary1/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | val d3:Double = 15 8 | println(d3) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 15.0 12 | */ 13 | -------------------------------------------------------------------------------- /Summary1/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | val aString = """this is a string with 8 | | many lines. 9 | | How will it print?""" 10 | 11 | println(aString) 12 | 13 | /* OUTPUT_SHOULD_BE 14 | this is a string with 15 | | many lines. 16 | | How will it print? 17 | */ 18 | -------------------------------------------------------------------------------- /Summary1/Solution-7.scala: -------------------------------------------------------------------------------- 1 | // Solution-7.scala 2 | // Solution to Exercise 7 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | val b:Boolean = "maybe" 8 | 9 | /* OUTPUT_SHOULD_CONTAIN 10 | error: type mismatch; 11 | found : String("maybe") 12 | required: Boolean 13 | val b:Boolean = "maybe" 14 | ^ 15 | one error found 16 | */ 17 | -------------------------------------------------------------------------------- /Summary1/Solution-8.scala: -------------------------------------------------------------------------------- 1 | // Solution-8.scala 2 | // Solution to Exercise 8 in "Summary 1" 3 | 4 | // Work this exercise in the REPL. Start the REPL by typing 'scala' 5 | // in a shell. 6 | 7 | val i:Int = 15.4 8 | 9 | /* OUTPUT_SHOULD_CONTAIN 10 | error: type mismatch; 11 | found : Double(15.4) 12 | required: Int 13 | val i:Int = 15.4 14 | ^ 15 | one error found 16 | */ 17 | -------------------------------------------------------------------------------- /Summary1/Solution-9.scala: -------------------------------------------------------------------------------- 1 | // Solution-9.scala 2 | // Solution to Exercise 9 in "Summary 1" 3 | 4 | val lbs = 150.0 5 | val height = 68.0 6 | val weightStatus = { 7 | val bmi = lbs/(height*height) * 703.07 8 | if(bmi < 18.5) println("Underweight") 9 | else if(bmi < 25) println("Normal weight") 10 | else println("Overweight") 11 | } 12 | println(weightStatus) 13 | 14 | /* OUTPUT_SHOULD_BE 15 | Normal weight 16 | () 17 | */ 18 | -------------------------------------------------------------------------------- /Summary1/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Summary2/BasicMethods.scala: -------------------------------------------------------------------------------- 1 | package com.atomicscala.BasicLibrary 2 | 3 | class WhizBang { 4 | def cube(x:Int):Int = { x * x * x } 5 | def bang(s:String):String = { s + "!" } 6 | } 7 | -------------------------------------------------------------------------------- /Summary2/ClassBodies.scala: -------------------------------------------------------------------------------- 1 | package com.atomicscala.Bodies 2 | 3 | class NoBody { 4 | def who():String = { 5 | "Nobody's home" 6 | } 7 | } 8 | 9 | class SomeBody { 10 | def name():String = { 11 | "Janet Doe" 12 | } 13 | println(name + " is SomeBody") 14 | } 15 | 16 | class EveryBody { 17 | val all = Vector(new SomeBody, new SomeBody, new SomeBody) 18 | } 19 | -------------------------------------------------------------------------------- /Summary2/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Summary 2" 3 | // To run, first compile BasicMethods.scala: 4 | // scalac BasicMethods.scala 5 | import com.atomicscala.AtomicTest._ 6 | import com.atomicscala.BasicLibrary._ 7 | 8 | val wb = new WhizBang 9 | wb.cube(3) is 27 10 | wb.bang("pop") is "pop!" 11 | 12 | /* OUTPUT_SHOULD_BE 13 | 27 14 | pop! 15 | */ 16 | -------------------------------------------------------------------------------- /Summary2/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /TaggingTraitsAndCaseObjects/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Tagging Traits & Case Objects" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | object EnumColor extends Enumeration { 6 | type EnumColor = Value 7 | val Red, Green, Blue = Value 8 | } 9 | 10 | EnumColor.Red is "Red" 11 | EnumColor.Blue is "Blue" 12 | EnumColor.Green is "Green" 13 | 14 | /* OUTPUT_SHOULD_BE 15 | Red 16 | Blue 17 | Green 18 | */ 19 | -------------------------------------------------------------------------------- /TaggingTraitsAndCaseObjects/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Tagging Traits & Case Objects" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | EnumColor.Red is "Red" 6 | EnumColor.Blue is "Blue" 7 | EnumColor.Green is "Green" 8 | -------------------------------------------------------------------------------- /TaggingTraitsAndCaseObjects/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Testing/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Testing" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val myValue1 = 20 6 | val myValue2 = 10 7 | myValue1 is myValue2 8 | 9 | /* OUTPUT_SHOULD_BE 10 | 20 11 | [Error] expected: 12 | 10 13 | */ 14 | -------------------------------------------------------------------------------- /Testing/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Testing" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val myValue3 = 10 6 | val myValue4 = 10 7 | myValue3 is myValue4 8 | 9 | /* OUTPUT_SHOULD_BE 10 | 10 11 | */ 12 | -------------------------------------------------------------------------------- /Testing/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Testing" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val myValue2 = 10 6 | val myValue3 = 10 7 | myValue2 is myValue3 8 | 9 | /* OUTPUT_SHOULD_BE 10 | 10 11 | */ 12 | -------------------------------------------------------------------------------- /Testing/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Testing" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val myValue2 = 10 6 | val myValue5 = "10" 7 | myValue2 is myValue5 8 | 9 | /* OUTPUT_SHOULD_BE 10 | 10 11 | */ 12 | -------------------------------------------------------------------------------- /Testing/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Testing" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def squareArea(x:Int):Int = { 6 | x * x 7 | } 8 | 9 | def rectangleArea(x:Int, y:Int):Int = { 10 | x * x 11 | } 12 | 13 | def trapezoidArea(x:Int, y:Int, h:Int):Double = { 14 | h/2 * (x + y) 15 | } 16 | 17 | // needs solution and tests 18 | -------------------------------------------------------------------------------- /Testing/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Traits/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Traits" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | trait BatteryPower { 6 | def monitor(level:Int) = level match { 7 | case i if(i < 20) => "red" 8 | case i if(i <= 39) => "yellow" 9 | case _ => "green" 10 | } 11 | } 12 | 13 | trait Toy 14 | 15 | class BatteryPoweredToy extends Toy with BatteryPower 16 | 17 | val toy = new BatteryPoweredToy 18 | toy.monitor(50) is "green" 19 | 20 | /* OUTPUT_SHOULD_BE 21 | green 22 | */ 23 | -------------------------------------------------------------------------------- /Traits/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Traits" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | trait BatteryPower { 6 | def monitor(level:Int) = level match { 7 | case i if(i < 20) => "red" 8 | case i if(i <= 39) => "yellow" 9 | case _ => "green" 10 | } 11 | } 12 | 13 | trait Toy 14 | 15 | val toy2 = new Toy with BatteryPower 16 | toy2.monitor(50) is "green" 17 | 18 | /* OUTPUT_SHOULD_BE 19 | green 20 | */ 21 | -------------------------------------------------------------------------------- /Traits/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class Battery extends EnergySource with BatteryPower 6 | val battery = new Battery 7 | battery.monitor(80) is "green" 8 | battery.monitor(30) is "yellow" 9 | battery.monitor(10) is "red" 10 | -------------------------------------------------------------------------------- /Traits/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val toy = new BatteryPoweredToy 6 | toy.monitor(50) is "green" 7 | -------------------------------------------------------------------------------- /Traits/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val toy2 = new // Fill this in 6 | toy2.monitor(50) is "green" 7 | -------------------------------------------------------------------------------- /Traits/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Tuples/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Tuples" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val info = (50, 45) 6 | info._1 is 50 7 | info._2 is 45 8 | 9 | /* OUTPUT_SHOULD_BE 10 | 50 11 | 45 12 | */ 13 | -------------------------------------------------------------------------------- /Tuples/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Tuples" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val info = // fill this in 6 | info./* what goes here? */ is 50 7 | info./* what goes here? */ is 45 8 | -------------------------------------------------------------------------------- /Tuples/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Tuples" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | weather(81, 45) is ("Hot", "Humid") 6 | weather(50, 45) is ("Temperate", "Humid") 7 | -------------------------------------------------------------------------------- /Tuples/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Tuples" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val (/* fill this in */) = weather(81, 45) 6 | heat1 is "Hot" 7 | moisture1 is "Humid" 8 | val (/* fill this in */) = weather(27, 55) 9 | heat2 is "Cold" 10 | moisture2 is "Damp" 11 | -------------------------------------------------------------------------------- /Tuples/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /TypeParameterConstraints/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Type Parameter Constraints" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class WithF { 6 | def f(n:Int) = n * 11 7 | } 8 | 9 | def callF[T <: WithF](t:T, n:Int) = t.f(n) 10 | 11 | callF(new WithF, 2) is 22 12 | 13 | callF(new WithF { 14 | override def f(n:Int) = n * 7 15 | }, 2) is 14 16 | 17 | /* OUTPUT_SHOULD_BE 18 | 22 19 | 14 20 | */ 21 | -------------------------------------------------------------------------------- /TypeParameterConstraints/Solution-3-FirstEdition.scala: -------------------------------------------------------------------------------- 1 | // Solution-3-FirstEdition.scala 2 | // Solution to Exercise 3 in "Type Parameter Constraints" (First Edition of book) 3 | 4 | /* This exercise was changed because it was too vague/uninteresting */ 5 | 6 | /*{oldDescription} Would it ever make sense to have a type parameter 7 | that specifies an exact type rather than a bounded type? Write code 8 | to explore the possibilities and see if you can determine the 9 | differences. {oldDescription}*/ 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 13 | */ 14 | -------------------------------------------------------------------------------- /TypeParameterConstraints/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /UniformAccessAndSetters/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Uniform Access & Setters" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | class FunStuff { 6 | var internal = "all the fun happens here" 7 | def x = internal 8 | def x_=(newVal:String) = internal = newVal 9 | } 10 | 11 | val fun = new FunStuff 12 | fun.x is "all the fun happens here" 13 | fun.x = "make it so" 14 | fun.x is "make it so" 15 | 16 | /* OUTPUT_SHOULD_BE 17 | all the fun happens here 18 | make it so 19 | */ 20 | -------------------------------------------------------------------------------- /UniformAccessAndSetters/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /UsingTraits/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "Using Traits" 3 | 4 | /* This exercise in the first edition of the book was poorly-formed, 5 | and has been eliminated from the 2nd edition */ 6 | 7 | /* OUTPUT_SHOULD_BE 8 | */ 9 | -------------------------------------------------------------------------------- /UsingTraits/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "Using Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val webcam = new WIFICamera 6 | webcam.showImage is "Showing video" 7 | webcam.address is "192.168.0.200" 8 | webcam.reportStatus is "working" 9 | -------------------------------------------------------------------------------- /UsingTraits/Starter-2.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 2 2 | // From "Using Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val c = new Object with Connections 6 | c.maxConnections is 5 7 | c.connect(true) is true 8 | c.connected is 1 9 | for(i <- 0 to 3) 10 | c.connect(true) is true 11 | c.connect(true) is false 12 | c.connect(false) is true 13 | c.connected is 4 14 | for(i <- 0 to 3) 15 | c.connect(false) is true 16 | c.connected is 0 17 | c.connect(false) is false 18 | -------------------------------------------------------------------------------- /UsingTraits/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Using Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val c2 = new WIFICamera with Connections 6 | c2.maxConnections is 5 7 | c2.connect(true) is true 8 | c2.connected is 1 9 | c2.connect(false) is true 10 | c2.connected is 0 11 | c2.connect(false) is false 12 | -------------------------------------------------------------------------------- /UsingTraits/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Using Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | // From 6 | wikipedia.org/wiki/Art_periods 7 | // Pre-Renaissance: before 1300 8 | // Renaissance: 1300-1599 9 | // Baroque: 1600-1699 10 | // Late Baroque: 1700-1789 11 | // Romanticism: 1790-1880 12 | // Modern: 1881-1970 13 | // Contemporary: after 1971 14 | val art = new ArtPeriod 15 | art.period(1400) is "Renaissance" 16 | art.period(1650) is "Baroque" 17 | art.period(1279) is "Pre-Renaissance" 18 | -------------------------------------------------------------------------------- /UsingTraits/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Using Traits" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val painting = new Painting("The Starry Night", 1889) 6 | painting.period is "Modern" 7 | -------------------------------------------------------------------------------- /UsingTraits/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Using Traits" atom 3 | 4 | /* This exercise in the first edition of the book was poorly-formed, 5 | and has been eliminated from the 2nd edition */ 6 | 7 | -------------------------------------------------------------------------------- /UsingTraits/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Values/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "Values" 3 | 4 | val v1 = 17 5 | println(v1) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | 17 9 | */ 10 | -------------------------------------------------------------------------------- /Values/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Values" 3 | 4 | val v1 = 17 5 | println(v1) 6 | // This won't work: 7 | v1 = 20 8 | println("You can't change a val") 9 | 10 | /* OUTPUT_SHOULD_CONTAIN 11 | error: reassignment to val 12 | v1 = 20 13 | ^ 14 | one error found 15 | */ -------------------------------------------------------------------------------- /Values/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Values" 3 | 4 | val s = "ABC1234" 5 | println(s) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | ABC1234 9 | */ -------------------------------------------------------------------------------- /Values/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Values" 3 | 4 | val s = "ABC1234" 5 | println(s) 6 | // This won't work: 7 | s = "DEF1234" 8 | println("You can't change a val") 9 | 10 | /* OUTPUT_SHOULD_CONTAIN 11 | error: reassignment to val 12 | s = "DEF1234" 13 | ^ 14 | one error found 15 | */ -------------------------------------------------------------------------------- /Values/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Values" 3 | 4 | val d1 = 15.56D 5 | println(d1) 6 | 7 | /* OUTPUT_SHOULD_BE 8 | 15.56 9 | */ -------------------------------------------------------------------------------- /Values/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Variables/Solution-1a.scala: -------------------------------------------------------------------------------- 1 | // Solution-1a.scala 2 | // Solution to Exercise 1 in "Variables" 3 | 4 | val someV = 5 5 | // This won't work: 6 | someV = 10 7 | 8 | /* OUTPUT_SHOULD_CONTAIN 9 | error: reassignment to val 10 | someV = 10 11 | ^ 12 | one error found 13 | */ 14 | -------------------------------------------------------------------------------- /Variables/Solution-1b.scala: -------------------------------------------------------------------------------- 1 | // Solution-1b.scala 2 | // Solution to Exercise 1 in "Variables" 3 | 4 | var v = 5 5 | v = 10 6 | println(v) 7 | 8 | /* OUTPUT_SHOULD_BE 9 | 10 10 | */ 11 | -------------------------------------------------------------------------------- /Variables/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Variables" 3 | 4 | var v1 = 5 5 | v1 = 10 6 | val constantv1 = v1 7 | println(v1) 8 | println(constantv1) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 10 12 | 10 13 | */ 14 | -------------------------------------------------------------------------------- /Variables/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Variables" 3 | 4 | var v1 = 10 5 | val constantv1 = v1 6 | v1 = 15 7 | 8 | println(constantv1) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 10 12 | */ 13 | -------------------------------------------------------------------------------- /Variables/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Variables" 3 | 4 | var v1 = 15 5 | val constantv1 = v1 6 | var v2 = v1 7 | v1 = 20 8 | println(v2) 9 | 10 | /* OUTPUT_SHOULD_BE 11 | 15 12 | */ 13 | -------------------------------------------------------------------------------- /Variables/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /Vectors/Solution-12.scala: -------------------------------------------------------------------------------- 1 | // Solution-12.scala 2 | // Solution to Exercise 12 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val myVector1 = Vector(1, 2, 3, 4, 5, 6) 6 | val myVector2 = Vector(1, 2, 3, 4, 5, 6) 7 | 8 | myVector1 is myVector2 9 | 10 | /* OUTPUT_SHOULD_BE 11 | Vector(1, 2, 3, 4, 5, 6) 12 | */ 13 | -------------------------------------------------------------------------------- /Vectors/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v4 = Vector(Vector(0, 1, 2), Vector(3, 4, 5)) 6 | println(v4) 7 | 8 | /* REPL Session: 9 | scala> val v4 = Vector(Vector(0, 1, 2), Vector(3, 4, 5)) 10 | v4: scala.collection.immutable.Vector[scala.collection.immutable.Vector[Int]] = Vector(Vector(0, 1, 2), Vector(3, 4, 5)) 11 | */ 12 | 13 | /* OUTPUT_SHOULD_BE 14 | Vector(Vector(0, 1, 2), Vector(3, 4, 5)) 15 | */ 16 | -------------------------------------------------------------------------------- /Vectors/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v5 = Vector("The", "dog", "visited", "the", "firehouse") 6 | var sentence: String = "" 7 | for(word <- v5) { 8 | sentence = sentence + word + " " 9 | } 10 | 11 | sentence.toString() is "The dog visited the firehouse " 12 | 13 | /* OUTPUT_SHOULD_BE 14 | The dog visited the firehouse 15 | */ 16 | -------------------------------------------------------------------------------- /Vectors/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | var sentence = "The dog visited the firehouse " 6 | val theString = sentence.replace("firehouse ", "firehouse!") 7 | theString is "The dog visited the firehouse!" 8 | 9 | /* OUTPUT_SHOULD_BE 10 | The dog visited the firehouse! 11 | */ 12 | -------------------------------------------------------------------------------- /Vectors/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v5 = Vector("The", "dog", "visited", "the", "firehouse") 6 | 7 | for(word <- v5) { 8 | println(word.reverse) 9 | } 10 | 11 | /* OUTPUT_SHOULD_BE 12 | ehT 13 | god 14 | detisiv 15 | eht 16 | esuoherif 17 | */ 18 | -------------------------------------------------------------------------------- /Vectors/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v5 = Vector("The", "dog", "visited", "the", "firehouse") 6 | 7 | for(word <- v5.reverse) { 8 | println(word) 9 | } 10 | 11 | /* OUTPUT_SHOULD_BE 12 | firehouse 13 | the 14 | visited 15 | dog 16 | The 17 | */ 18 | -------------------------------------------------------------------------------- /Vectors/Solution-7.scala: -------------------------------------------------------------------------------- 1 | // Solution-7.scala 2 | // Solution to Exercise 7 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val intVector = Vector(10, 20, 30, 40, 50) 6 | val doubleVector = Vector(10.1D, 20.2D, 30.3D, 40.4D, 50.5D) 7 | 8 | intVector.sum is 150 9 | intVector.min is 10 10 | intVector.max is 50 11 | 12 | doubleVector.sum is 151.5D 13 | doubleVector.min is 10.1D 14 | doubleVector.max is 50.5D 15 | 16 | /* OUTPUT_SHOULD_BE 17 | 150 18 | 10 19 | 50 20 | 151.5 21 | 10.1 22 | 50.5 23 | */ 24 | -------------------------------------------------------------------------------- /Vectors/Solution-8.scala: -------------------------------------------------------------------------------- 1 | // Solution-8.scala 2 | // Solution to Exercise 8 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v6 = Vector("the", "dog", "visited", "the", "firehouse") 6 | 7 | // String doesn't have a sum operation 8 | //println("The string Vector sum is " + v6.sum) 9 | // max and min are defined by alphabetical order 10 | v6.max is "visited" 11 | v6.min is "dog" 12 | 13 | /* OUTPUT_SHOULD_BE 14 | visited 15 | dog 16 | */ 17 | -------------------------------------------------------------------------------- /Vectors/Solution-9.scala: -------------------------------------------------------------------------------- 1 | // Solution-9.scala 2 | // Solution to Exercise 9 in "Vectors" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val rangeSum = Range.inclusive(0, 10).sum 6 | rangeSum is 55 7 | 8 | /* OUTPUT_SHOULD_BE 9 | 55 10 | */ 11 | -------------------------------------------------------------------------------- /Vectors/Starter-3.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 3 2 | // From "Vectors" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | sentence.toString() is "The dog visited the firehouse " 6 | -------------------------------------------------------------------------------- /Vectors/Starter-4.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 4 2 | // From "Vectors" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | theString is "The dog visited the firehouse!" 6 | -------------------------------------------------------------------------------- /Vectors/Starter-5.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 5 2 | // From "Vectors" atom 3 | 4 | /* Output: 5 | ehT 6 | god 7 | detisiv 8 | eht 9 | esuoherif 10 | */ 11 | -------------------------------------------------------------------------------- /Vectors/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "Vectors" atom 3 | 4 | /* Output: 5 | firehouse 6 | the 7 | visited 8 | dog 9 | The 10 | */ 11 | -------------------------------------------------------------------------------- /Vectors/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /ZZZ_BuildTools/Autosave.py: -------------------------------------------------------------------------------- 1 | import sublime, sublime_plugin 2 | 3 | class AutoSaveCommand(sublime_plugin.EventListener): 4 | def on_modified(self, view): 5 | if view.file_name(): 6 | view.run_command('save') -------------------------------------------------------------------------------- /ZZZ_BuildTools/Default (Windows).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | //{ "keys": ["ctrl+a"], "command": "move_to", "args": {"to": "bol", "extend": false} }, 3 | //{ "keys": ["ctrl+e"], "command": "move_to", "args": {"to": "eol", "extend": false} } 4 | //,{ "keys": ["ctrl+d"], "command": "right_delete" } 5 | { "keys": ["f11"], "command": "run_macro_file", "args": {"file": "Packages/User/OUTPUT_SHOULD_BE.sublime-macro"} }, 6 | { "keys": ["f12"], "command": "run_macro_file", "args": {"file": "Packages/User/OUTPUT_SHOULD_CONTAIN.sublime-macro"} } 7 | ] 8 | -------------------------------------------------------------------------------- /ZZZ_BuildTools/FindingStraightQuotesInWord.txt: -------------------------------------------------------------------------------- 1 | Search for ^034 (double quotes) or ^039 (single quote) -------------------------------------------------------------------------------- /ZZZ_BuildTools/FormatCodeForExercise.py: -------------------------------------------------------------------------------- 1 | # Bind to key with: 2 | # { "keys": ["f11"], "command": "format_code_for_exercise" }, 3 | import sublime, sublime_plugin, pprint, os, re 4 | 5 | class FormatCodeForExerciseCommand(sublime_plugin.TextCommand): 6 | 7 | def run(self, edit): 8 | for region in self.view.sel(): 9 | code = self.view.substr(region).replace("\n", "\r") 10 | sublime.set_clipboard(code) 11 | sublime.message_dialog(code) 12 | 13 | -------------------------------------------------------------------------------- /ZZZ_BuildTools/atomic-scala-solutions.sublime-build: -------------------------------------------------------------------------------- 1 | { 2 | "cmd": ["C:\\Program Files (x86)\\scala\\bin\\scala.bat", "$file"], 3 | "variants": [ 4 | { 5 | "name": "Run", 6 | "cmd": ["run.bat", "-f", "$file"] 7 | } 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /ZZZ_BuildTools/colorama-test.py: -------------------------------------------------------------------------------- 1 | import colorama 2 | colorama.init() 3 | print(colorama.Back.RED + 'some red text' + colorama.Style.RESET_ALL) 4 | # print(Back.GREEN + 'and with a green background') 5 | # print(Style.DIM + 'and in dim text') 6 | # print(Fore.RESET + Back.RESET + Style.RESET_ALL) 7 | print('back to normal now') -------------------------------------------------------------------------------- /ZZZ_BuildTools/termcolor-test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from termcolor import colored, cprint 3 | 4 | text = colored('Hello, World!', 'red', attrs=['reverse', 'blink']) 5 | print(text) 6 | cprint('Hello, World!', 'green', 'on_red') 7 | 8 | print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan') 9 | print_red_on_cyan('Hello, World!') 10 | print_red_on_cyan('Hello, Universe!') 11 | 12 | for i in range(10): 13 | cprint(i, 'magenta', end=' ') 14 | 15 | cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr) -------------------------------------------------------------------------------- /_.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | start /I "atomic scala solutions" run.bat 3 | if ERRORLEVEL 1 ( 4 | echo. 5 | echo ================================================= 6 | echo This command is part of the tools used to create 7 | echo and test these solutions. You can ignore it. 8 | echo ================================================= 9 | ) 10 | -------------------------------------------------------------------------------- /libs/commons-math3-3.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AtomicScala/atomic-scala-solutions/9dbaf5a71bed56964b9119b6895da03b5a69b3d4/libs/commons-math3-3.3.jar -------------------------------------------------------------------------------- /mapAndreduce/Solution-1.scala: -------------------------------------------------------------------------------- 1 | // Solution-1.scala 2 | // Solution to Exercise 1 in "map & reduce" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v = Vector(1, 2, 3, 4) 6 | v.map(n => ((n * 11) + 10)) is Vector(21, 32, 43, 54) 7 | 8 | /* OUTPUT_SHOULD_BE 9 | Vector(21, 32, 43, 54) 10 | */ 11 | -------------------------------------------------------------------------------- /mapAndreduce/Solution-2.scala: -------------------------------------------------------------------------------- 1 | // Solution-2.scala 2 | // Solution to Exercise 2 in "map & reduce" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v = Vector(1, 2, 3, 4) 6 | // This doesn't produce a result: 7 | v.foreach(n=>((n*11) + 10)) is Vector(21, 32, 43, 54) 8 | 9 | /* OUTPUT_SHOULD_BE 10 | () 11 | [Error] expected: 12 | Vector(21, 32, 43, 54) 13 | */ 14 | -------------------------------------------------------------------------------- /mapAndreduce/Solution-3.scala: -------------------------------------------------------------------------------- 1 | // Solution-3.scala 2 | // Solution to Exercise 3 in "map & reduce" 3 | import com.atomicscala.AtomicTest._ 4 | import scala.collection.immutable.VectorBuilder 5 | 6 | val v = Vector(1, 2, 3, 4) 7 | 8 | var vb1 = new VectorBuilder[Int]() 9 | for(n <- v) 10 | vb1 += (n * 11) + 10 11 | 12 | vb1.result is Vector(21, 32, 43, 54) 13 | 14 | /* OUTPUT_SHOULD_BE 15 | Vector(21, 32, 43, 54) 16 | */ 17 | -------------------------------------------------------------------------------- /mapAndreduce/Solution-4.scala: -------------------------------------------------------------------------------- 1 | // Solution-4.scala 2 | // Solution to Exercise 4 in "map & reduce" 3 | import com.atomicscala.AtomicTest._ 4 | import scala.collection.immutable.VectorBuilder 5 | 6 | val v = Vector(1, 2, 3, 4) 7 | var vb2 = new VectorBuilder[Int]() 8 | for(n <- v) 9 | vb2 += (n+1) 10 | 11 | vb2.result is Vector(2, 3, 4, 5) 12 | 13 | /* OUTPUT_SHOULD_BE 14 | Vector(2, 3, 4, 5) 15 | */ 16 | -------------------------------------------------------------------------------- /mapAndreduce/Solution-5.scala: -------------------------------------------------------------------------------- 1 | // Solution-5.scala 2 | // Solution to Exercise 5 in "map & reduce" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val vReduce = Vector(1, 10, 100, 1000) 6 | var sum = 0 7 | for(n <- vReduce) 8 | sum += n 9 | sum is 1111 10 | 11 | /* OUTPUT_SHOULD_BE 12 | 1111 13 | */ 14 | -------------------------------------------------------------------------------- /mapAndreduce/Solution-6.scala: -------------------------------------------------------------------------------- 1 | // Solution-6.scala 2 | // Solution to Exercise 6 in "map & reduce" 3 | import com.atomicscala.AtomicTest._ 4 | 5 | def sumIt(someNumbers:Int*):Int = { 6 | someNumbers.reduce((total, n) => total + n) 7 | } 8 | 9 | sumIt(1, 2, 3) is 6 10 | sumIt(45, 45, 45, 60) is 195 11 | 12 | /* OUTPUT_SHOULD_BE 13 | 6 14 | 195 15 | */ 16 | -------------------------------------------------------------------------------- /mapAndreduce/Starter-1.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 1 2 | // From "map & reduce" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | val v = Vector(1, 2, 3, 4) 6 | v.map(/* Fill this in */) is Vector(21, 32, 43, 54) 7 | -------------------------------------------------------------------------------- /mapAndreduce/Starter-6.scala: -------------------------------------------------------------------------------- 1 | // Starter Code for Exercise 6 2 | // From "map & reduce" atom 3 | import com.atomicscala.AtomicTest._ 4 | 5 | sumIt(1, 2, 3) is 6 6 | sumIt(45, 45, 45, 60) is 195 7 | -------------------------------------------------------------------------------- /mapAndreduce/run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0..\;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ..\ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | -------------------------------------------------------------------------------- /run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | set CLASSPATH=%~dp0;.;..;..\libs\commons-math3-3.3.jar; 4 | :: echo CLASSPATH is [!CLASSPATH!] 5 | ZZZ_BuildTools\runner.bat %* 6 | if ERRORLEVEL 1 ( 7 | echo. 8 | echo ====================================================== 9 | echo This 'run' command is part of the tools used to create 10 | echo and test these solutions. You can ignore it. 11 | echo ====================================================== 12 | ) 13 | --------------------------------------------------------------------------------