Preview:
1. Create a 'Book' class with the following specifications:

 Properties:  title,Author,Published_year

Methods:
  displayInfo : to print Book details.

//Program
package com.example.objectoriented

class Book(val title:String, val author:String, val published_year:Int){
    fun display(){
        println("Title: $title\nAuthor: $author\nPublished Year: $published_year")
        println()
    }
}

fun main(){
    val b1 = Book("Computer Networking", "John", 2020)
    val b2 = Book("Algorithms Design And Analysis", "James", 2019)

    println("Book Details")
    println()
    b1.display()
    b2.display()
}



2. Simple class with a Primary Constructor

//Program

package com.example.objectoriented

class Emp(val ename:String, val id:Int, val age:Int){

    init {
        if(age<0){
            println("Age cannot be negative")
        }
        else{
            println("Object is created")
        }
    }
    fun showDetails(){
        println("Name: $ename \nID: $id")
    }
}

fun main(){
    val e1 = Emp("Rahul", 101, 20)
    e1.showDetails()
    println()
    val e2 = Emp("Aarav", 102, -3)
    e2.showDetails()
}


3. Implelemt a Employee Class with default constructor values

    Properties: name,position,department,experience(set it to 1 default)

  Methods: showdetails()

  a. Create a Instance with only Name specified
  b. Create a Instance with Name and Position Specified
  c. Create a Instance with All Properties Specified
  d. Instance with Name and Experience Specified
  
//Program

package com.example.objectoriented

class Employee(val name:String, val position:String = "Clerk", val department:String = "CS", val experience:Int = 1){
    fun display(){
        println("Name: $name")
        println("Poistion: $position")
        println("Department: $department")
        println("Experience: $experience")
        println()
    }
}

fun main(){
    val e1 = Employee("Rahul")
    val e2 = Employee("Aarav", "Data Analyst ", "CS")
    val e3 = Employee("James", "Manager", "IT", 20)
    val e4 = Employee("Adam", experience = 15)

    e1.display()
    e2.display()
    e3.display()
    e4.display()


}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter