This commit is contained in:
2020-01-21 21:58:39 +01:00
parent 7a0047320e
commit ce2065d828
24 changed files with 145 additions and 4 deletions

2
.idea/misc.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="true" project-jdk-name="13.0.2" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@@ -0,0 +1,83 @@
package exercises
abstract class MyList (val ints : Array[Int]) {
def head: Int = ints(0)
def tail = (ints.slice(1,ints.length))
def isEmpty: Boolean = ints.length == 0
def add(i: Int):Array[Int] = ints :+ i
override def toString: String = {
loop(ints, "(") + ")"
}
private def loop(rest: Array[Int], acc: String): String = {
if (rest.length != 1) return acc
else {
loop(rest.slice(1,rest.length) , acc + s"${rest.head}")
}
}
/*
implement collection
single linked list holds int's
head = first element of list
tail = remainder of the list
isEmpty = boolean
add(int) => new List with element added
override toString => a string representation of the list
*/
}
//i'm an idiot
abstract class TrueList {
def head: Int
def tail: TrueList
def isEmpty: Boolean
def add(element: Int): TrueList
//polymorphic
def printElements: String
override def toString: String = s"[$printElements]"
}
object Empty extends TrueList {
override def head: Int = throw new NoSuchElementException
override def tail: TrueList = throw new NoSuchElementException
override def isEmpty: Boolean = true
override def add(element: Int): TrueList = new Cons(element, Empty)
override def printElements: String = ""
}
class Cons(h: Int, t: TrueList) extends TrueList {
override def head: Int = h
override def tail: TrueList = t
override def isEmpty: Boolean = false
override def add(element: Int): TrueList = new Cons(element, this)
override def printElements: String = if (t.isEmpty) "" + h else s"$h ${t.printElements}"
}
object ListTest extends App {
val list = new Cons(1, new Cons(2, new Cons(3, Empty)))
println(list.tail.head)
println(list.add(4).head)
println(list.isEmpty)
println(list.toString)
}

View File

@@ -62,7 +62,7 @@ object Functions extends App {
def isPrime(n: Int) : Boolean = { def isPrime(n: Int) : Boolean = {
def checkNum(m: Int) : Int = { def checkNum(m: Int) : Int = {
if (m <= 1) throw IsPrimeException("Finished, it is prime") if (m <= 1) throw IsPrimeException()
if (n % m == 0 && m != 1) throw IsNotPrimeException(m + "") if (n % m == 0 && m != 1) throw IsNotPrimeException(m + "")
else checkNum(m-1) else checkNum(m-1)
} }
@@ -86,6 +86,6 @@ object Functions extends App {
} }
case class IsPrimeException(value: "Finished, it is prime") extends Throwable case class IsPrimeException() extends Throwable
case class IsNotPrimeException(str: String) extends Throwable case class IsNotPrimeException(str: String) extends Throwable

View File

@@ -52,7 +52,7 @@ object Recursion extends App {
def isPrime(n: Int) : Boolean = { def isPrime(n: Int) : Boolean = {
@tailrec @tailrec
def checkNum(m: Int) : Int = { def checkNum(m: Int) : Int = {
if (m <= 1) throw IsPrimeException("Finished, it is prime") if (m <= 1) throw IsPrimeException()
if (n % m == 0 && m != 1) throw IsNotPrimeException(m + "") if (n % m == 0 && m != 1) throw IsNotPrimeException(m + "")
else checkNum(m-1) else checkNum(m-1)
} }

View File

@@ -0,0 +1,49 @@
package lectures.part2oop
object AbstractDataTypes extends App {
// abstract
abstract class Animal {
val creatureType: String = "wild"
def eat: Unit
}
class Dog extends Animal {
override val creatureType: String = "Canine"
def eat: Unit = println("crunch crunch")
}
// traits
trait Carnivore {
def eat(animal: Animal): Unit
val preferedMeal: String = "fresh meat"
}
trait ColdBlooded
class Crocodile extends Animal with Carnivore with ColdBlooded {
override val creatureType: String = "croc"
override def eat: Unit = println("nomnomnom")
override def eat(animal: Animal): Unit = println(s"i'm a croc and i'm eating ${animal.creatureType}")
}
val dog = new Dog
val croc = new Crocodile
croc.eat(dog)
// traits vs abstract classes
// 1 traits do not have constructor parameters
// 2 can only extend 1 class, but can extend multiple traits (same class)
// 3 traits are meant to describe behavior, abstract class describes a thing
}

View File

@@ -0,0 +1,8 @@
package lectures
object Inheritance extends App {
}

View File

@@ -8,5 +8,6 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="scala-sdk-2.13.1" level="application" /> <orderEntry type="library" name="scala-sdk-2.13.1" level="application" />
<orderEntry type="library" name="scala-sdk-2.12.7" level="application" />
</component> </component>
</module> </module>