scala-3

Modifier         Outside package Package Class Subclass
No access modifier Yes              Yes  Yes Yes          
Protected          No               No  Yes Yes          
Private              No               No  Yes  No
Access modifire
========================
class AccessExample{ 
     private var a:Int = 10 
     def show(){ 
         println(a) 
     } 

object MainObject{ 
    def main(args:Array[String]){ 
        var p = new AccessExample() 
        p.a = 12 
        p.show() 
    } 
}
class AccessExample{ 
     protected var a:Int = 10 

class SubClass extends AccessExample{ 
    def display(){ 
        println("a = "+a) 
    } 

object MainObject{ 
    def main(args:Array[String]){ 
        var s = new SubClass() 
        s.display() 
    } 

====================================================
Final variable in scala============
Scala Final Variable Example
You can't override final variables in subclass. Let's see an example.

class Vehicle{ 
     final val speed:Int = 60 

class Bike extends Vehicle{ 
   override val speed:Int = 100 
    def show(){ 
        println(speed) 
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var b = new Bike() 
        b.show() 
    } 
Scala Final Method
Final method declare in the parent class can't be override. You can make any method to final if you don't want to get it overridden. Attempt to override final method will cause to a compile time error.
Scala Final Method Example
class Vehicle{ 
     final def show(){ 
         println("vehicle is running") 
     } 

class Bike extends Vehicle{ 
   //override val speed:Int = 100 
    override def show(){ 
        println("bike is running") 
    } 

object MainObject{ 
    def main(args:Array[String]){ 
        var b = new Bike() 
        b.show() 
    } 

Scala Final Class Example
You can also make final class. Final class can't be inherited. If you make a class final, it can't be extended further.
final class Vehicle{ 
     def show(){ 
         println("vehicle is running") 
     } 
 

 
class Bike extends Vehicle{ 
       override def show(){ 
        println("bike is running") 
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var b = new Bike() 
        b.show() 
    } 
}
=====================================
inheritance in scala
Scala Single Inheritance Example
class Employee{ 
    var salary:Float = 10000 

 
class Programmer extends Employee{ 
    var bonus:Int = 5000 
    println("Salary = "+salary) 
    println("Bonus = "+bonus) 

 
object MainObject{ 
    def main(args:Array[String]){ 
        new Programmer() 
    } 
}
Scala Multilevel Inheritance Example
class A{ 
    var salary1 = 10000 

 
class B extends A{ 
    var salary2 = 20000 

 
class C extends B{ 
    def show(){ 
        println("salary1 = "+salary1) 
        println("salary2 = "+salary2) 
    } 

 
object test{ 
    def main(args:Array[String]){{   
        var c = new C() 
        c.show() 
     
    } 

}


ARRAY
=====
class ArrayExample{ 
    var arr = Array(1,2,3,4,5)      // Creating single dimensional array 
    def show(){ 
        for(a<-arr)                       // Traversing array elements 
            println(a) 
        println("Third Element  = "+ arr(2))        // Accessing elements by using index 
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var a = new ArrayExample() 
        a.show() 
    } 


class ArrayExample{ 
    var arr = new Array[Int](5)         // Creating single dimensional array 
    def show(){ 
        for(a<-arr){                      // Traversing array elements 
            println(a) 
        } 
        println("Third Element before assignment = "+ arr(2))        // Accessing elements by using index 
        arr(2) = 10                                                          // Assigning new element at 2 index 
        println("Third Element after assignment = "+ arr(2)) 
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var a = new ArrayExample() 
        a.show() 
    } 

class ArrayExample{ 
    def show(arr:Array[Int]){ 
        for(a<-arr)                // Traversing array elements 
            println(a) 
        println("Third Element = "+ arr(2))        // Accessing elements by using index 
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var arr = Array(1,2,3,4,5,6)    // creating single dimensional array 
        var a = new ArrayExample() 
        a.show(arr)                     // passing array as an argument in the function 
    } 

class ArrayExample{ 
    var arr = Array(1,2,3,4,5)      // Creating single dimensional array 
    arr.foreach((element:Int)=>println(element))       // Iterating by using foreach loop 

 
object MainObject{ 
    def main(args:Array[String]){ 
        new ArrayExample() 
    } 

class ArrayExample{ 
    var arr = Array.ofDim[Int](2,2)          // Creating multidimensional array 
    arr(1)(0) = 15                          // Assigning value 
    def show(){ 
        for(i<- 0 to 1){                       // Traversing elements by using loop 
           for(j<- 0 to 1){ 
                print(" "+arr(i)(j)) 
            } 
            println() 
        } 
        println("Third Element = "+ arr(1)(1))        // Accessing elements by using index 
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var a = new ArrayExample() 
        a.show()                      
    } 
}

class ArrayExample{ 
    var arr = Array(Array(1,2,3,4,5), Array(6,7,8,9,10))   // Creating multidimensional array 
    def show(){ 
        for(i<- 0 to 1){               // Traversing elements using loop 
           for(j<- 0 to 4){ 
                print(" "+arr(i)(j)) 
            } 
            println() 
        }     
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var a = new ArrayExample() 
        a.show()                      
    } 

=================================================================
Exception-
========
class ExceptionExample{ 
    def divide(a:Int, b:Int) = { 
            a/b             // Exception occurred here 
        println("Rest of the code is executing...") 
    } 

object MainObject{ 
    def main(args:Array[String]){ 
        var e = new ExceptionExample() 
        e.divide(100,0) 
  
    } 

class ExceptionExample{ 
    def divide(a:Int, b:Int) = { 
        try{ 
            a/b 
        }catch{ 
            case e: ArithmeticException => println(e) 
        } 
        println("Rest of the code is executing...") 
    } 

object MainObject{ 
    def main(args:Array[String]){ 
        var e = new ExceptionExample() 
        e.divide(100,0) 
  
    } 

class ExceptionExample{ 
    def divide(a:Int, b:Int) = { 
        try{ 
            a/b 
            var arr = Array(1,2) 
            arr(10) 
        }catch{ 
            case e: ArithmeticException => println(e) 
            case ex: Throwable =>println("found a unknown exception"+ ex) 
        } 
        println("Rest of the code is executing...") 
    } 

object MainObject{ 
    def main(args:Array[String]){ 
        var e = new ExceptionExample() 
        e.divide(100,10) 
  
    } 

class ExceptionExample{ 
    def divide(a:Int, b:Int) = { 
        try{ 
            a/b 
            var arr = Array(1,2) 
            arr(10) 
        }catch{ 
            case e: ArithmeticException => println(e) 
            case ex: Exception =>println(ex) 
            case th: Throwable=>println("found a unknown exception"+th) 
        } 
        finally{ 
            println("Finaly block always executes") 
        } 
        println("Rest of the code is executing...") 
    } 

 
 
object MainObject{ 
    def main(args:Array[String]){ 
        var e = new ExceptionExample() 
        e.divide(100,10) 
  
    } 
}


class ExceptionExample2{ 
    def validate(age:Int)={ 
        if(age<18) 
            throw new ArithmeticException("You are not eligible") 
        else println("You are eligible") 
    } 

 
object MainObject{ 
    def main(args:Array[String]){ 
        var e = new ExceptionExample2() 
        e.validate(10) 
  
    } 
}


==========================================================
clooections
===============

Scala Set
It is used to store unique elements in the set. It does not maintain any order for storing elements. You can apply various operations on them. It is defined in the Scala.collection.immutable package.

import scala.collection.immutable._ 
object MainObject{ 
    def main(args:Array[String]){ 
        val set1 = Set()                            // An empty set 
        val games = Set("Cricket","Football","Hocky","Golf")    // Creating a set with elements 
        println(set1) 
        println(games) 
    } 

import scala.collection.immutable._ 
object MainObject{ 
        def main(args:Array[String]){ 
            val games = Set("Cricket","Football","Hocky","Golf") 
            println(games.head)             // Returns first element present in the set 
            println(games.tail)         // Returns all elements except first element. 
            println(games.isEmpty)          // Returns either true or false 
        } 
    } 
 
 
 
 merge two set
 
 import scala.collection.immutable._ 
object MainObject{ 
        def main(args:Array[String]){ 
            val games = Set("Cricket","Football","Hocky","Golf") 
            val alphabet = Set("A","B","C","D","E")  
            val mergeSet = games ++ alphabet            // Merging two sets 
            println("Elements in games set: "+games.size)   // Return size of collection 
            println("Elements in alphabet set: "+alphabet.size)  
            println("Elements in mergeSet: "+mergeSet.size) 
            println(mergeSet) 
        } 
    } 
 
 
 import scala.collection.immutable._ 
object MainObject{ 
        def main(args:Array[String]){ 
            val games = Set("Cricket","Football","Hocky","Golf") 
            println(games) 
            println("Elements in set: "+games.size) 
            println("Golf exists in the set : "+games.contains("Golf")) 
            println("Racing exists in the set : "+games.contains("Racing")) 
             
        } 
    } 
 
 
 
 adding removing element from set
 ========================================
 
 
 import scala.collection.immutable._ 
object MainObject{ 
        def main(args:Array[String]){ 
            var games = Set("Cricket","Football","Hocky","Golf") 
            println(games) 
            games += "Racing"               // Adding new element 
            println(games) 
            games += "Cricket"              // Adding new element, it does not allow duplicacy. 
            println(games) 
            games -= "Golf"             // Removing element 
            println(games) 
        } 
    } 
 
 
 import scala.collection.immutable._ 
object MainObject{ 
        def main(args:Array[String]){ 
            var games = Set("Cricket","Football","Hocky","Golf") 
            for(game <- games){ 
                println(game) 
            } 
        } 
    } 
 
 
 
 import scala.collection.immutable._ 
    object MainObject{ 
        def main(args:Array[String]){ 
            var games = Set("Cricket","Football","Hocky","Golf") 
            games.foreach((element:String)=> println(element)) 
        }    


import scala.collection.immutable._ 
object MainObject{ 
    def main(args:Array[String]){ 
        var games = Set("Cricket","Football","Hocky","Golf","C") 
        var alphabet = Set("A","B","C","D","E","Golf") 
        var setIntersection = games.intersect(alphabet) 
        println("Intersection by using intersect method: "+setIntersection) 
        println("Intersection by using & operator: "+(games & alphabet)) 
        var setUnion = games.union(alphabet) 
        println(setUnion) 
    } 

import scala.collection.immutable.SortedSet             
object MainObject{ 
    def main(args:Array[String]){ 
        var numbers: SortedSet[Int] = SortedSet(5,8,1,2,9,6,4,7,2) 
        numbers.foreach((element:Int)=> println(element)) 
    }    
////////////

4. How many maximum number of columns can be added to HBase table?
5. Why columns are not defined at the time of table creation in HBase?

//////////////////////

Scala Queue
Queue implements a data structure that allows inserting and retrieving elements in a first-in-first-out (FIFO) manner.
In scala, Queue is implemented as a pair of lists. One is used to insert the elements and second to contain deleted elements. Elements are added to the first list and removed from the second list.

import scala.collection.immutable._ 
object MainObject{ 
    def main(args:Array[String]){ 
        var queue = Queue(1,5,6,2,3,9,5,2,5,dfss) 
        var queue2:Queue[Int] = Queue(1,5,6,2,3,9,5,2,5,"sdfs") 
        println(queue)   
        println(queue2) 
    } 

import scala.collection.immutable._ 
object MainObject{ 
    def main(args:Array[String]){ 
        var queue = Queue(1,5,6,2,3,9,5,2,5) 
        print("Queue Elements: ") 
        queue.foreach((element:Int)=>print(element+" "))   
        var firstElement = queue.front 
        print("\nFirst element in the queue: "+ firstElement)        
        var enqueueQueue = queue.enqueue(100) 
        print("\nElement added in the queue: ") 
        enqueueQueue.foreach((element:Int)=>print(element+" ")) 
        var dequeueQueue = queue.dequeue 
        print("\nElement deleted from this queue: "+ dequeueQueue) 
    } 

map ======

object MainObject{ 
    def main(args:Array[String]){ 
        var map = Map(("A","Apple"),("B","Ball")) 
        var map2 = Map("A"->"Aple","B"->"Ball") 
        println(map) 
        println(map2) 
     
    } 
}


object MainObject{ 
    def main(args:Array[String]){ 
        var map = Map("A"->"Apple","B"->"Ball")             // Creating map 
        println(map("A"))                            // Accessing value by using key 
        var newMap = map+("C"->"Cat")                  // Adding a new element to map 
        println(newMap) 
        var removeElement = newMap - ("B")                // Removing an element from map 
        println(removeElement) 
    } 




Tuple
======================

object MainObject{ 
    def main(args:Array[String]){ 
        var tupleValues = tupleFunction() 
        println("Iterating values: ") 
        tupleValues.productIterator.foreach(println)    // Iterating tuple values using productIterator 
    } 
    def tupleFunction()={ 
        var tuple = (1,2.5,"India") 
        tuple 
    } 
File handling in scala
=====================
import java.io._ 
object MainObject{ 
  def main(args:Array[String]){ 
val fileObject = new File("C:/Users/sudkumar/Desktop/eclipsewrkspace/ScalaFile.txt" )     // Creating a file 
val printWriter = new PrintWriter(fileObject)       // Passing reference of file to the printwriter 
printWriter.write("Hello, This is scala file")  // Writing to the file 
printWriter.close()             // Closing printwriter 
  }
}

Scala Reading a File Example: Reading Each Line
import scala.io.Source 
object MainObject{ 
  def main(args:Array[String]){ 
    val filename = "C:/Users/sudkumar/Desktop/eclipsewrkspace/ScalaFile.txt" 
    val fileSource = Source.fromFile(filename) 
    for(line<-fileSource.getLines){ 
      println(line) 
    } 
    fileSource.close() 
  } 



Comments

Popular posts from this blog

scala-4