diff --git a/.idea/dictionaries/Beppe.xml b/.idea/dictionaries/Beppe.xml new file mode 100644 index 0000000..d7d9408 --- /dev/null +++ b/.idea/dictionaries/Beppe.xml @@ -0,0 +1,7 @@ + + + + beppe + + + \ No newline at end of file diff --git a/src/exercises/ooExcercises.scala b/src/exercises/ooExcercises.scala new file mode 100644 index 0000000..9204d28 --- /dev/null +++ b/src/exercises/ooExcercises.scala @@ -0,0 +1,65 @@ +package exercises + +object ooExcercises extends App { + + + + + // novel and a writer class + // writer: first name, surname, year of birth + // =method full name + // novel: name, year of release, author + // -method authorAge (age at year of release), isWrittenBy(author), copy(new year of release) = new instance of Novel with new year of release + + val au1 = new writer("beppe", "vanrolleghem", 1996) + val au2 = new writer("beppe2", "vanrolleghem2", 1997) + val nov1 = new novel("somebook",2004,au1) + println(nov1.isWrittenBy(au2)) + println(au1.fullName()) + println(nov1.authorAge()) + val nov2 = nov1.copy(2020) + println(nov2.authorAge()) + + + + // counter class + // receives an int value + // method returns current count + //method to increment/decrement => new Counter + //overload inc/dec to receive an amount = new counter + + + var c = new counter(5) + println(c.currentCount()) + c = c.increment() + c = c.increment() + println(c.currentCount()) + println(c.decrement(3).currentCount()) + + +} + + + + + +class writer(firstName: String, surName: String, val yearOfBirth: Int) { + def fullName(): String = s"$firstName $surName" + +} + +class novel(name: String, yearOfRelease: Int, author: writer) { + def authorAge(): Int = yearOfRelease - author.yearOfBirth + def isWrittenBy(author: writer): Boolean = this.author == author + def copy (newYearOfRelease: Int): novel = new novel(name, newYearOfRelease, author) +} + + + +class counter(i: Int) { + def currentCount(): Int = i + def increment(): counter = new counter(i+1) + def decrement(): counter = new counter(i-1) + def increment(in: Int): counter = new counter(i+in) + def decrement(de: Int): counter = new counter(i-de) +} diff --git a/src/lectures/part2oop/MethodNotations.scala b/src/lectures/part2oop/MethodNotations.scala new file mode 100644 index 0000000..eadd6b0 --- /dev/null +++ b/src/lectures/part2oop/MethodNotations.scala @@ -0,0 +1,47 @@ +package lectures.part2oop + +object MethodNotations extends App { + + + class Person(val name: String, favoriteMovie: String) { + def likes(movie: String): Boolean = movie == favoriteMovie + def +(person: Person): String = s"${this.name} is hanging out with ${person.name}" + def unary_! : String = name.reverse + def isAlive: Boolean = true + def apply(): String = s"Hi, my name is $name and I like $favoriteMovie" + + + } + + + val mary = new Person("Mary", "Inception") + println(mary.likes("Inception")) + println(mary likes "Inception") + val tom = new Person("Tom", "Fight Club") + println(mary + tom) + println(mary.+(tom)) + + //this functionality is nice, but i will never use it because using a . is way more clear to me + + println(1 + 2) + println(1.+(2)) + + + // all operators are methods. + val x = -1 + val y = 1.unary_- + println(x == y) + + //unary only works with - + ~ ! + //this on the other hand seems really usefull for certain things + println(!mary) + + //println(mary isAlive) //postfix notation + + + //apply + println(mary.apply()) + println(mary()) //so apply is kindof a default method + + +} diff --git a/src/lectures/part2oop/OOBasics.scala b/src/lectures/part2oop/OOBasics.scala new file mode 100644 index 0000000..3b9e17d --- /dev/null +++ b/src/lectures/part2oop/OOBasics.scala @@ -0,0 +1,38 @@ +package lectures.part2oop + +object OOBasics extends App { + + val person = new Person("Beppe",23) + println(person.age) + println(person.x) + person.greet() + person.greet("Fred") + +} + + +class NoFieldPerson(name: String, age: Int) + +// class parameters != fields + +class fieldPerson(name: String, val age: Int) //person.age not accessible, fieldPerson.age is accessible + +class Person(name: String, val age: Int) { + //body + val x = 2 + println(1 + 3) + + //this, fields etc... + def greet(name: String): Unit = println(s"${this.name} says: Hi, $name") + + + //overload + def greet(): Unit = println(s"Hi, I am $name") + + //multiple constructors + //new constructor calls original constructor here + //means only usable for easyer default constructors, which can be easily fixed by using default parameters + def this(name: String) = this(name, 0) + + +} \ No newline at end of file