Skip to content

Commit 4efbb5f

Browse files
committed
[Kotlin] added control flow, returns, jumps, classes and inheritance
1 parent c700c3b commit 4efbb5f

File tree

6 files changed

+519
-190
lines changed

6 files changed

+519
-190
lines changed

KotlinFromScratch/.idea/workspace.xml

+76-187
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

KotlinFromScratch/src/ControlFlow.kt

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
class ControlFlow {
2+
3+
fun test() {
4+
println("If Expression")
5+
val ten = 10
6+
val twenty = 20
7+
val max = getMax(ten, twenty)
8+
9+
10+
println("")
11+
println("When Expression")
12+
13+
14+
print("(when 1)fibonacci array = [")
15+
for (i in 1..10) {
16+
print(fibonacci1(i))
17+
if (i != 10)
18+
print(", ")
19+
}
20+
println("]")
21+
22+
23+
print("(when 2)fibonacci array = [")
24+
for (i in 1..10) {
25+
print(fibonacci2(i))
26+
if (i != 10)
27+
print(", ")
28+
}
29+
println("]")
30+
31+
print("(when 3)")
32+
val taxi = Sedan()
33+
recogniseCar(taxi)
34+
35+
36+
println("")
37+
println("For Expression")
38+
39+
40+
val primes = intArrayOf(2, 3, 5, 7, 11, 13, 17, 23, 29, 31)
41+
print("(If 1)primes array = [")
42+
for (num in primes) {
43+
print("$num ,")
44+
}
45+
println("]")
46+
47+
48+
print("(If 2)primes array = [")
49+
for (index in 0 until primes.size) {
50+
print("${primes[index]} ,")
51+
}
52+
println("]")
53+
54+
55+
print("(If 3)primes array = [")
56+
for (index in primes.indices) {
57+
print("${primes[index]} ,")
58+
}
59+
println("]")
60+
61+
62+
print("(If 4)primes array = [")
63+
primes.forEach {
64+
print("$it ,")
65+
}
66+
println("]")
67+
68+
69+
print("(If 5)primes array = [")
70+
primes.forEachIndexed { index, i ->
71+
print(i)
72+
if (index != primes.lastIndex)
73+
print(", ")
74+
}
75+
println("]")
76+
77+
78+
print("Iterate over 2 numbers from the bottom. Primes array = [")
79+
for (index in primes.size - 1 downTo 0 step 2) {
80+
print("${primes[index]} ,")
81+
}
82+
println("]")
83+
84+
85+
println("")
86+
println("While Expression")
87+
88+
print("Factors of 36 = [")
89+
var factors = findFactors1(36)
90+
factors.forEachIndexed { index, i ->
91+
print(i)
92+
if (index != factors.lastIndex)
93+
print(", ")
94+
}
95+
println("]")
96+
97+
98+
print("Factors of 42 = [")
99+
factors = findFactors2(42)
100+
factors.forEachIndexed { index, i ->
101+
print(i)
102+
if (index != factors.lastIndex)
103+
print(", ")
104+
}
105+
println("]")
106+
107+
}
108+
109+
private fun getMax(num1: Int, num2: Int): Int {
110+
return if (num1 > num2) {
111+
println("$num1 is larger")
112+
num1
113+
} else {
114+
println("$num2 is larger")
115+
num2
116+
}
117+
}
118+
119+
private fun fibonacci1(size: Int): Int {
120+
if (size < 1) {
121+
throw ArrayIndexOutOfBoundsException("size must be positive")
122+
}
123+
return when (size) {
124+
1, 2 -> 1
125+
else -> {
126+
fibonacci1(size - 1) + fibonacci1(size - 2)
127+
}
128+
}
129+
}
130+
131+
private fun fibonacci2(size: Int): Int {
132+
return when {
133+
size < 0 -> throw ArrayIndexOutOfBoundsException("size must be positive")
134+
size == 1 -> 1
135+
size == 2 -> 1
136+
else -> (fibonacci2(size - 1) + fibonacci2(size - 2))
137+
}
138+
}
139+
140+
private fun recogniseCar(car: Car) {
141+
when (car) {
142+
is Sedan -> println("This is a ${car.getName()}")
143+
is Suv -> println("This is a ${car.getName()}")
144+
else -> println("No idea")
145+
}
146+
}
147+
148+
private fun findFactors1(number: Int): IntArray {
149+
var factors = IntArray(0)
150+
var i = 1
151+
while (i <= (number / 2)) {
152+
if (number.rem(i) == 0) {
153+
factors += i
154+
}
155+
i++
156+
}
157+
factors += number
158+
return factors.sortedArray()
159+
}
160+
161+
private fun findFactors2(number: Int): IntArray {
162+
var factors = IntArray(0)
163+
var i = 1
164+
do {
165+
if (number.rem(i) == 0) {
166+
factors += i
167+
}
168+
i++
169+
} while (i <= (number / 2))
170+
factors += number
171+
return factors.sortedArray()
172+
}
173+
}
174+
175+
open class Car {
176+
open fun getName(): String {
177+
return "car"
178+
}
179+
}
180+
181+
class Sedan : Car() {
182+
override fun getName(): String {
183+
return "sedan"
184+
}
185+
}
186+
187+
class Suv : Car() {
188+
override fun getName(): String {
189+
return "suv"
190+
}
191+
}

KotlinFromScratch/src/Main.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
fun main(args: Array<String>) {
2-
val basicTypes = BasicTypes()
3-
basicTypes.test()
2+
// val basicTypes = BasicTypes()
3+
// basicTypes.test()
4+
5+
// val controlFlow = ControlFlow()
6+
// controlFlow.test()
7+
8+
// val returnsAndJumps = ReturnsAndJumps()
9+
// returnsAndJumps.test()
10+
11+
val myClass = MyClass()
12+
myClass.test()
413
}

KotlinFromScratch/src/MyClass.kt

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
class MyClass {
2+
fun test() {
3+
println("Class with multiple constructors")
4+
val frank = Person("Frank", "S", "Watson")
5+
println(frank.fullName())
6+
7+
println("")
8+
println("Inheritance")
9+
val wood = Student("Kent", "Wood")
10+
println(wood.fullName())
11+
12+
var bar1 = Bar1(0)
13+
var bar2 = Bar2()
14+
15+
println("")
16+
println("Inheritance (Child -> Parent -> Grandparent)")
17+
println("0. Initialise Child")
18+
val child = Child("memo1")
19+
println("1. Override function in Child")
20+
child.f()
21+
println("2. Inherited function from Parent")
22+
child.g()
23+
println("3. Call both super and override function in Child")
24+
child.h()
25+
println("4. Call both super and override function in inner Class of Parent")
26+
val parent = Parent("memo2")
27+
val colleague = parent.Colleague()
28+
colleague.g()
29+
}
30+
}
31+
32+
open class Person {
33+
34+
var firstName: String = ""
35+
var middleName: String = ""
36+
var lastName: String = ""
37+
38+
constructor(firstName: String, lastName: String) {
39+
this.firstName = firstName
40+
this.lastName = lastName
41+
}
42+
43+
constructor(firstName: String, middleName: String, lastName: String) : this(firstName, lastName) {
44+
this.middleName = middleName
45+
}
46+
47+
var fullName: () -> String = {
48+
"$firstName $middleName $lastName"
49+
}
50+
}
51+
52+
class Student : Person {
53+
54+
constructor(firstName: String, lastName: String) : super(firstName, lastName) {
55+
this.firstName = firstName
56+
this.lastName = lastName
57+
}
58+
59+
constructor(firstName: String, middleName: String, lastName: String) : this(firstName, lastName) {
60+
this.middleName = middleName
61+
}
62+
}
63+
64+
65+
interface Foo {
66+
val count: Int
67+
}
68+
69+
class Bar1(override val count: Int) : Foo
70+
class Bar2 : Foo {
71+
override val count = 0
72+
}
73+
74+
open class Grandparent(memo: String) {
75+
init {
76+
println("Initialising Grandparent")
77+
}
78+
79+
open val id: Int = (0..10000).random().also { println("Initialising id in Grandparent: $it") }
80+
open fun f() {
81+
println("Grandparent.f()")
82+
}
83+
}
84+
85+
open class Parent(memo: String) : Grandparent(memo) {
86+
init {
87+
println("Initialising Parent")
88+
}
89+
90+
override val id: Int = (0..10000).random().also { println("Initialising id in Parent: $it") }
91+
override fun f() {
92+
println("Parent.f()")
93+
}
94+
95+
fun g() {
96+
print("From Grandparent: ")
97+
super.f()
98+
99+
print("From Child: ")
100+
f()
101+
}
102+
103+
inner class Colleague {
104+
fun g() {
105+
print("From Grandparent: ")
106+
super@Parent.f()
107+
108+
print("From Parent: ")
109+
f()
110+
}
111+
}
112+
}
113+
114+
115+
class Child(memo: String) : Parent(memo) {
116+
init {
117+
println("Initialising Child")
118+
}
119+
120+
override val id: Int = (0..10000).random().also { println("Initialising id in Child: $it") }
121+
override fun f() {
122+
println("Child.f()")
123+
}
124+
125+
126+
fun h() {
127+
print("From Parent: ")
128+
super.f()
129+
130+
print("From Child: ")
131+
f()
132+
}
133+
}
134+
135+
abstract class Base {
136+
abstract fun f()
137+
}
138+
139+
class Derived : Base() {
140+
override fun f() {
141+
142+
}
143+
}

0 commit comments

Comments
 (0)