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

PointRectangle

import math.abs
object HelloWorld {
   def main(args: Array[String]) {
       val a = convertStringToPoint("0,12")
       val b = convertStringToPoint("10,12")
       val c = convertStringToPoint("10,0")
       val d = convertStringToPoint("0,0")
       val point = convertStringToPoint("2,1")
       println(ST_Contains(point,a,b,c,d))
   }
   def ST_Contains(p: Point, a: Point, b: Point, c:Point, d: Point): Boolean = {
       val areaRectangle = 0.5 * abs(
        //              ( y_A - y_C ) * ( x_D - x_B )
                        ( a.y - c.y ) * ( d.x - b.x )
        //              ( y_B - y_D ) * ( x_A - x_C )
                      + ( b.y - d.y ) * ( a.x - c.x )
                    )
       val area_ABP = area_triangle(a,b,p)
       val area_BCP = area_triangle(b,c,p)
       val area_cdp = area_triangle(c,d,p)
       val area_dap = area_triangle(d,a,p)
       
       return areaRectangle == (area_ABP+area_BCP+area_cdp+area_dap)
  }
  
  def area_triangle(a: Point, b: Point, c: Point): Double = {
      val area = 0.5 * abs( 
          (a.x * ( b.y - c.y )) + (b.x * ( c.y - a.y )) + (c.x *  (a.y - b.y))
          )
      return area
  }
  
  def convertStringToPoint(pointString: String): Point ={

    val splitString = pointString.split(",")

    Point(splitString(0).toDouble, splitString(1).toDouble)

  }
  
  case class Point(x: Double, y: Double)
  case class Rectangle(a: Point, b: Point, c: Point, d: Point)
   
}

Compile and Execute Scala Online

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
        val keerthi =  person("keerthi",35,"architect")
       print("keerthi's age:",keerthi.age)
   }
}

case class person(name:String, age:Int, designation: String)

assign1

object HelloWorld {
    def averageLettersImperative(names: Array[String]): Double = {
         if(names.size == 0){
          println("The code goes through here 0")
          return 0}
         var sum = 0.0;
         for (x <- names) 
            sum += x.length
         return sum / names.size
      }
      
    def averageLettersFunctional(names: Array[String]): Double = {
        if(names.size == 0){
          println("The code goes through here 1")
          return 0}
        //   return names.foreach(sum+=names.length) / names.size
        //   return sum(names.length) / names.size
        //return names.iterate(0,names.size)() //error:value to iterate is not a memeber of an array
        //Try: toMap, toStream, sum, foreach
    }
  
      def main(args: Array[String]){
        var names0 = Array.empty[String] // test if statement
        var names1 = Array("Edna","Simone","Violet")
        // for (x <- names) 
        //   println(x.length)
          
        println(averageLettersImperative(names0))
        //println(averageLettersFunctional(names))
        println((1 to 100).toSream) // needs library scala.collection.immutable.Stream[Int]
      }
}

Scala Script Mode

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
   }
}

Compile and Execute Scala Online

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

Compile and Execute Scala Online

object Demo {
   def main(args: Array[String]) {
      var myVar :Int = 10;
      val myVal :String = "Hello Scala with datatype declaration.";
      var myVar1 = 20;
      val myVal1 = "Hello Scala new without datatype declaration.";
      
      println(myVar); 
      println(myVal); 
      println(myVar1); 
      println(myVal1);
   }
}

Compile and Execute Scala Online

object HelloWorld {
   def main(args: Array[String]) {
      
      val weights=Array[1,2,3]
      val values=Array[10,10,10]
      val w=3
      
      def knapsackProblem():Array[Int]={
          
      }
   }
}

Random

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

Compile and Execute Scala Online

object MainObject {  
   def main(args: Array[String]) {  
      val result = checkIt(-10)  
      println (result)  
   }  
   def checkIt (a:Int)  =  if (a >= 0) 1 else -1
}

Compile and Execute Scala Online

object MainObject {  
   def main(args: Array[String]) {  
      val result = checkIt(-10)  
      println (result)  
   }  
   def checkIt (a:Int)  =  if (a >= 0) 1 else -1
}

Advertisements
Loading...

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