I am learning Scala and am a beginner. My trainer has given me some work, see below:
Let's construct a bouquet of functions that would test the following conditions on a given no "n". A) check prime. B) check if square. C) check irrational. Construct the bouquet as a Map[Char, Int=>Boolean]. Now accept the integer and code in {A, B,C} from the command line to call the appropriate function and print the result.
I write all program A, B and C. But using Map I don't know what to do.
object firstProgram
{
def main(args: Array[String]):Unit =
{
square(9);
primeNumber(19);
}
def square(x: Int): Unit =
{
var no = math.sqrt(x);
if((no%1) == 0)
println(x +" IS A SQUARE NUMBER OF - "+no.toInt);
else
println(x +" IS NOT A SQUARE NUMBER");
}
def primeNumber(x: Int) : Unit =
{
var a =0;
var returnValue = false;
for(a <- 2 until x)
{
if( (x%a) ==0)
returnValue = true;
}
if(returnValue == false)
println(x + " IS A PRIME NUMBER");
else
println(x + " IS NOT A PRIME NUMBER");
}
}
How can I understand the question, and how to do it with Map?
Intis irrational. There's no way plain integers can be anything but rational