Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Scala Script Mode Program

object HelloWorld {
   /* This is my first java program.  
   * This will print 'Hello World' as the output
   */
   def main(args: Array[String]) {
      println("Hello, world!")   // prints Hello World
   }
}

ScalaStudy

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
   }
}

Compile and Execute Scala Online

class MyClass ( val name : String,  val rollno : Int) /* Primary Constructor */
{   //When a class is instantiated everything inside the class get executed except the methods. Its unique features of scala unlike java and c++.
    println("Print everytime the class is instantiated")
    //Auxillary Constructor
    def this ( name : String){ //First Line has to be primary constructor
        this(name,100)
        println("Hello Auxillary Constructor")
       //this(name,100)
    }
    def test() : Unit = {
        println("Test Mesaage "+name  +rollno)
    }
}

object HelloWorld {
   def main(args: Array[String]) {
       val m = new MyClass("kedar",1)  //m is an instance
       m.test()
       println(m.name)
       println(m.rollno)
       
       val n = new MyClass("Sugyani")  //n is an instance
       n.test()
   }
}

scala code

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
   }
}

test

object Demo {
   def main(args: Array[String]) {
      println(matchTest(3))
      
      println("end....")
   }
   
   def matchTest(x: Int): String = x match {
      case 1 => "one"
      case 2 => "two"
      case _ => "many"
   }
}

ScalaPractice

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
   }
}

Compile and Execute Scala Online

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
   }
   
   /**
    * given a Seq[A] and a function f : A => B, return a Seq[B]
    */
  def unknown[A, B](as: Seq[A], fn: A => B): Seq[B] = as.map(fn(_))
   
   
}

foobar

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
   }
}

zadanie

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
   }
}

Compile and Execute Scala Online

object HelloWorld {
   def main(args: Array[String]) {
      prtH(BigInt(8))
   }
   
   def seq(n: BigInt, max: BigInt): String = {
       val l = if(max == BigInt(2)) " | " else ""
       if(max == BigInt(0)) ""
       else {
           if(n % BigInt(2) == BigInt(1)) "b"+ l + seq((BigInt(3)*n+BigInt(1))/BigInt(2), max-BigInt(1))
           else "a" + l + seq(n / BigInt(2), max - BigInt(1))
       }
   }
   
   def prt(n: BigInt, k: BigInt){
       for(a <- BigInt(0) until n){
               println(a+": "+seq(a, k))
               if(a == power(BigInt(2), k-BigInt(1))-BigInt(1)) println("-------------------")
       }
   }
   
   def prtH(n: BigInt){
       for(a <- BigInt(1) to n){
           prt(power(BigInt(2), a), a)
           println("============================")
       }
   }
   
   def follow(k: BigInt, n: BigInt){
       for(a <- BigInt(1) to n){
           println(a+": "+seq(k, a))
           println("============================")
       }
   }

   
   def power(a: BigInt, to: BigInt): BigInt = {
       if(to == BigInt(0)) BigInt(1) else a*power(a, to-BigInt(1))
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.