Posts

Showing posts from September, 2020

Python

By default, each print statement prints text in a new line. If we want multiple print statements to print in the same line, we can use the following code: print("Sasmit",end =" ") print("is working in capgemini") o/p: Sasmit is working in capgemini Comments # Comments are pieces of text used to describe what is happening in the code. They have no effect on the code whatsoever. A comment can be written using the # character: if u want to write multi-line comments: then enclosed with """(triple quotes) print(""" Hi This is Sasmit from jungle and you can have a cup of coffee with me at evening time""") o/p:  Hi This is Sasmit from jungle and you can have a cup of coffee with me at evening time DataTypes: The language provides three main data types: Numbers Strings Booleans Variables are mutable. Hence, the value of a variable can always be updated or replaced. To create complex number: complex(real, imaginary) eg: ...

scala-7

//A minimal case class requires the keywords case class, an identifier, and a parameter list (which may be empty): case class Book(isbn: String) val frankenstein = Book("978-0486282114") //Notice how the keyword new was not used to instantiate the Book case class. This is because case classes have an apply method by default which takes care of object construction. //When you create a case class with parameters, the parameters are public vals. case class Message(sender: String, recipient: String, body: String) val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") println(message1.sender)  // prints guillaume@quebec.ca message1.sender = "travis@washington.us"  // this line does not compile //When you declare a case class the Scala compiler does the following for you: //1. Creates a class and its companion object. //2. Implements the apply method that you can use as a factory. This lets you create instances of the ...

scala-6

object LearningScala3 {   // Functions     // Format is def <function name>(parameter name: type...) : return type = { expression }   // Don't forget the = before the expression!   def squareIt(x: Int) : Int = {    x * x   }                                               //> squareIt: (x: Int)Int     def cubeIt(x: Int): Int = {x * x * x}           //> cubeIt: (x: Int)Int     println(squareIt(2))                            //> 4     println(cubeIt(2))        ...

scala-5

object LearningScala2 {   // Flow control     // If / else syntax   if (1 > 3) println("Impossible!") else println("The world makes sense.")                                                   //> The world makes sense.     if (1 > 3) {    println("Impossible!")   } else {    println("The world makes sense.")   }                                               //> The world makes sense.     // Matching - like switch in other languages:   v...

Java-1

//class is a blueprint for objects //create objects from the class blueprint with the keyword new // import java.io._ class Point(val xc: Int, val yc: Int) {    var x: Int = xc    var y: Int = yc       def move(dx: Int, dy: Int) {       x = x + dx       y = y + dy       println ("Point x location : " + x);       println ("Point y location : " + y);    } } object Demo {    def main(args: Array[String]) {       val pt = new Point(10, 20);       // Move to a new location       pt.move(10, 10);    } } //Extending a Class //You can extend a base Scala class and you can design an inherited class in the same way you do it in Java (use extends key word), but there are two restrictions: method overriding requires the override keyword, and only the primary...

scala-4

Scala: ----------------------------- The s interpolator It allows the use of variable directly in processing a string, when you prepend ‘s’ to it. val name = "John" println(s"Hello $name") //output: Hello John String interpolation can also process arbitrary expressions. println(s"2 + 3 = ${2 + 3}") //output: 2 + 3 = 5 The f interpolator It allows creating a formatted String, similar to printf in C language. While using ‘f’ interpolator, all variable references should be followed by the printf style format specifiers such as %d, %i, %f, etc. val height = 1.7d val name = "John" println(f"$name%s is $height%2.2f meters tall") //John is 1.70 meters tall The raw interpolator It is similar to s interpolator except that it performs no escaping of literals within a string. Output with s option: println(s"Result = \n a \n b") Result =  a  b Outout with raw option: println(raw"Result = \n a \n b") Result = \n a \n b Example 1:...

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)       }  }  o...

scala-2

class Student{      var id:Int = 0;                         // All fields must be initialized      var name:String = null;  }  object MainObject{      def main(args:Array[String]){          var s = new Student()               // Creating an object          println(s.id+" "+s.name);      }  }  ================================== class Student(id:Int, name:String){     // Primary constructor      def show(){          println(id+" "+name)      }  }  object MainObject{      def main(args:Array[String]...

scala-1

eclilpse ide - http://scala-ide.org/ ======================================================================================================= val hello: String = "Hola!"                     //> hello  : String = Hola!   println(hello)                                  //> Hola!     // Notice how Scala defines things backwards from other languages - you declare the   // name, then the type.     // VARIABLES are mutable   var helloThere: String = hello                  //> helloThere  : String = Hola!   helloThere = hello + " There!"   println(helloThere)     ...