File tree 8 files changed +279
-0
lines changed
8 files changed +279
-0
lines changed Original file line number Diff line number Diff line change
1
+ package Basic
2
+
3
+ abstract class School (var name : String , std : Int ) {
4
+ var school = " ABC School"
5
+ fun name (): String {
6
+ return name;
7
+ }
8
+
9
+ abstract fun showprofile ()
10
+ }
11
+
12
+ class Student (name : String , var std : Int ) : School(name, std) {
13
+ override fun showprofile () {
14
+ println (" School : $school \n Name : $name , Class : $std " )
15
+ }
16
+ }
17
+
18
+ fun main (Args : Array <String >) {
19
+ var obj = Student (" Krishna" , 4 )
20
+ println (" Calling Abstract method" )
21
+ obj.showprofile()
22
+ println (" \n calling non abstract method" )
23
+ println (" Name : ${obj.name()} " )
24
+ }
Original file line number Diff line number Diff line change
1
+ package Basic
2
+
3
+ interface twoWheeler {
4
+ fun getDeatils () { // method with code
5
+ println (" This is an interface providing a method getPrice() which returns price as per quantity " )
6
+ }
7
+
8
+ var price: Int // property
9
+ fun getPrice (qt : Int ): Int // abstract method
10
+ }
11
+
12
+ class bajaj : twoWheeler {
13
+ override var price = 45000
14
+ override fun getPrice (qt : Int ): Int {
15
+ return qt * price
16
+ }
17
+ }
18
+
19
+ class hero : twoWheeler {
20
+ override var price = 50000
21
+ override fun getPrice (qt : Int ): Int {
22
+ return (qt * price + 4500 )
23
+ }
24
+ }
25
+
26
+ fun main (args : Array <String >) {
27
+ var platina = bajaj()
28
+ platina.getDeatils()
29
+ println (" Enter quantity for platina : " )
30
+ var qt = readLine()!! .toInt()
31
+ print (" Your Price : " )
32
+ println (platina.getPrice(qt))
33
+
34
+ var passion = hero()
35
+ println (" Enter quantity for passion : " )
36
+ qt = readLine()!! .toInt()
37
+ print (" Your Price : " )
38
+ println (passion.getPrice(qt))
39
+ }
Original file line number Diff line number Diff line change
1
+ package Basic
2
+
3
+ interface A {
4
+ fun display () {
5
+ println (" this is interface A" )
6
+ }
7
+ }
8
+
9
+ interface B {
10
+ fun display () {
11
+ println (" this is interface B" )
12
+ }
13
+ }
14
+
15
+ class C : A , B {
16
+ override fun display () {
17
+ super <A >.display()
18
+ super < B > .display()
19
+
20
+ }
21
+ }
22
+
23
+ fun main (args : Array <String >) {
24
+ var ob = C ()
25
+ ob.display()
26
+ }
Original file line number Diff line number Diff line change
1
+ package Data_Structure
2
+
3
+ import java.util.*
4
+
5
+ fun insertion_sort (A : ArrayList <Double >) {
6
+ var n = A .size
7
+ var i: Int
8
+ for (j in 1 until n) {
9
+ var key = A [j]
10
+ i = j - 1
11
+ while (i >= 0 && A [i] > key) {
12
+ A [i + 1 ] = A [i]
13
+ i--
14
+ }
15
+ A [i + 1 ] = key
16
+ }
17
+ }
18
+
19
+ fun bucket_sort (A : Array <Double >) {
20
+ var n = A .size
21
+ var bucket = Array <ArrayList <Double >>(n, { i -> ArrayList () }) // creating buckets with n size
22
+ for (i in 0 .. A .size - 1 ) {
23
+ bucket[Math .floor(n * A [i]).toInt()].add(A [i]) // adding element a[i] into position n*a[i]
24
+ }
25
+ for (i in 0 .. A .size - 1 ) {
26
+ insertion_sort(bucket[i]) // sorting a list using inserton sort
27
+ }
28
+ for (i in 1 .. A .size - 1 ) {
29
+ bucket[0 ].addAll(bucket[i]) // concatenate all the buckets
30
+ }
31
+ println (" After sorting :" )
32
+ for (i in bucket[0 ]) {
33
+ print (" $i " ) // print the sorted elements
34
+ }
35
+ }
36
+
37
+ fun main (arg : Array <String >) {
38
+ print (" Enter no. of elements :" )
39
+ var n = readLine()!! .toInt()
40
+
41
+ println (" Enter elements : " )
42
+ var A = Array (n, { 0.0 })
43
+ for (i in 0 until n)
44
+ A [i] = readLine()!! .toDouble()
45
+
46
+ bucket_sort(A )
47
+
48
+ }
49
+
Original file line number Diff line number Diff line change
1
+ package Data_Structure
2
+
3
+ fun counting_sort (A : Array <Int >, max : Int ) {
4
+ var B = Array <Int >(A .size, { 0 }) // Array in which result will store
5
+ var C = Array <Int >(max, { 0 }) // count array
6
+ for (i in 0 .. A .size - 1 ) {
7
+ C [A [i] - 1 ] = C [A [i] - 1 ] + 1 // count the no. of occurrence of a particular element store in count array
8
+ }
9
+ for (i in 1 .. C .size - 1 ) {
10
+ C [i] = C [i] + C [i - 1 ] // calculate commutative sum
11
+ }
12
+ for (i in A .size - 1 downTo 0 ) {
13
+ B [C [A [i] - 1 ] - 1 ] = A [i] // place the element at its position
14
+ C [A [i] - 1 ] = C [A [i] - 1 ] - 1 // decrease the occurrence of the element by 1
15
+ }
16
+ println (" After sorting :" )
17
+ for (i in B ) {
18
+ print (" $i " )
19
+ }
20
+
21
+ }
22
+
23
+ fun main (arg : Array <String >) {
24
+ print (" Enter no. of elements :" )
25
+ var n = readLine()!! .toInt()
26
+
27
+ println (" Enter elements : " )
28
+ var A = Array (n, { 0 })
29
+ for (i in 0 until n)
30
+ A [i] = readLine()!! .toInt()
31
+ // calculate maximum element from array
32
+ var max = A [0 ];
33
+ for (i in A ) {
34
+ if (i > max) {
35
+ max = i
36
+ }
37
+ }
38
+ counting_sort(A , max)
39
+ }
40
+
Original file line number Diff line number Diff line change
1
+ package Data_Structure
2
+
3
+ fun gcd_1 (a : Int , b : Int ): Int {
4
+ var a = a
5
+ var b = b
6
+ while (b != 0 ) {
7
+ var t = b;
8
+ b = a % b;
9
+ a = t;
10
+ }
11
+ return a
12
+ }
13
+
14
+ fun gcd_2 (a : Int , b : Int ): Int {
15
+ if (b == 0 )
16
+ return a
17
+ else
18
+ return gcd_2(b, a % b)
19
+ }
20
+
21
+ fun main (args : Array <String >) {
22
+ println (" Enter two number " )
23
+ var a = readLine()!! .toInt()
24
+ var b = readLine()!! .toInt()
25
+
26
+ println (" Using iterative function GCD = ${gcd_1(a, b)} " )
27
+ println (" using recursive function GCD = ${gcd_2(a, b)} " )
28
+ }
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ fun insertion_sort(A: Array<Int>) {
14
14
}
15
15
}
16
16
17
+
17
18
fun main (arg : Array <String >) {
18
19
print (" Enter no. of elements :" )
19
20
var n = readLine()!! .toInt()
Original file line number Diff line number Diff line change
1
+ package Data_Structure
2
+
3
+ import kotlin.system.exitProcess
4
+
5
+ data class SinglyLinkedList (var value : Int , var link : SinglyLinkedList ? = null )
6
+
7
+ class TestList {
8
+ var start: SinglyLinkedList ? = null
9
+
10
+ init {
11
+
12
+ }
13
+
14
+ fun insert (value : Int ) {
15
+ var ptr = SinglyLinkedList (value, null )
16
+ if (start == null ) {
17
+ start = ptr
18
+ } else {
19
+ ptr.link = start
20
+ start = ptr
21
+ }
22
+ }
23
+
24
+ fun delete () {
25
+ if (start == null ) {
26
+ print (" underFlow!!" )
27
+ exitProcess(0 )
28
+ } else {
29
+ start = start?.link
30
+ }
31
+ }
32
+
33
+ fun traverse () {
34
+ if (start?.link == null ) {
35
+ print (" underFlow!!" )
36
+ return
37
+ }
38
+
39
+ var ptr1 = start
40
+ while (ptr1?.link != null ) {
41
+ print (" ${ptr1.value} " )
42
+ ptr1 = ptr1.link
43
+ }
44
+ print (ptr1?.value)
45
+ }
46
+ }
47
+
48
+ fun main (str : Array <String >) {
49
+ while (true ) {
50
+ println (" Linked List Menu " )
51
+ println (" 1. Insert at begining" )
52
+ println (" 2. Delete" )
53
+ println (" 3. Traverse" )
54
+ println (" 4. Exit" )
55
+ var ob = TestList ()
56
+ var ch = readLine()!! .toInt()
57
+ when (ch) {
58
+ 1 -> {
59
+ print (" Enter element : " )
60
+ var element = readLine()!! .toInt()
61
+ ob.insert(element)
62
+ }
63
+ 2 -> {
64
+ ob.delete()
65
+ print (" Deleted" )
66
+ }
67
+ 3 -> {
68
+ ob.traverse()
69
+ }
70
+ }
71
+ }
72
+ }
You can’t perform that action at this time.
0 commit comments