Files
DataScienceAndBigDataAnalytics/Codes/Code-B4 (Apache Scala).md
T

1.3 KiB

B4 - Apache Scala

Tested and working as intended.


  1. Open your Terminal and run the following commands:
source ~/.bashrc # Not really needed but still
start-master.sh # Considering the shell script is defined in PATH
spark-shell # This will start scala CLI
  1. Enter paste mode:
:paste
  1. Paste the following code:
object EvenOddCheck {
  def main(args: Array[String]): Unit = {
    val number = 15
    if (number % 2 == 0) {
      println(s"$number is even")
    } else {
      println(s"$number is odd")
    }
  }
}

Note

IF USER INPUT REQUIRED, USE THIS CODE INSTEAD:

import scala.io.StdIn

object AddTwoNumbers {
  def main(args: Array[String]): Unit = {
    println("Enter the first number:")
    val num1 = StdIn.readInt()
    println("Enter the second number:")
    val num2 = StdIn.readInt()
    val sum = num1 + num2
    println(s"The sum of $num1 and $num2 is $sum")
  }
}
  1. Press Ctrl + D to exit paste mode

  2. Execute:

EvenOddCheck.main(Array.empty[String])

Note

TO EXECUTE USER INPUT WALA CODE, RUN:

AddTwoNumbers.main(Array.empty[String])

Tip

Additional Scala codes available at here.