0

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?

2
  • 3
    The idea is that you store all the checks on a Map so the program will receive as input the number to check and the check to do (as a single letter), and you would extract the check from the map. - Anyways, I am pretty sure your trainee won't be happy that you solve this using mutability, and your checks are not checks, since they do not return a Boolean but instead print a value. Commented May 21, 2020 at 14:57
  • 1
    I don't see why there is a predicate to check if an Int is irrational. There's no way plain integers can be anything but rational Commented May 21, 2020 at 15:23

1 Answer 1

1

Since your map has keys that are characters and values that are predicates, I think your trainer wants you to have the letters 'A', 'B', and 'C' corresponding to isPerfectSquare, isPrime, isIrrational, etc. You can do that as such:

val checksMap = Map(
  'A' -> isPerfectSquare _,
  'B' -> isPrime _,
  'C' -> isIrrational _
)

Of course, for this to work, you'd have to change the signatures of your methods to look like

def isPerfectSquare(x: Int): Boolean
def isPrime(x: Int): Boolean
def isIrrational(x: Int): Boolean

Afterwards, you can read integers from the command line and iterate through the map, and if a check doesn't pass, you're probably supposed to print something like s"Check $key didn't pass"

for ((k, v) <- checksMap) if (!v(n)) println(s"Check $k not passed")

And may I suggest a cooler implementation of isPrime that I found online?

lazy val primes: LazyList[Int] = 3 #:: (LazyList.from(5, 2).filter(x => !primes.takeWhile(math.pow(_,2) < x).exists(x % _ == 0)))

def isPrime(x: Int): Boolean = x == 2 || primes.takeWhile(_ <= x).last == x

Link to Scastie (fill in the blanks): https://scastie.scala-lang.org/WRw8Z6FxSauBvxQ9PChbPA

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.