In: Computer Science
We will follow the instructions as specified in the problem statement.
Let's see the flow of the program we have written:
1. We will create an object with the name printSquares and enclose all of our code into it.
2. We will define the main function from which we will call our function getOutput (). This will be a recursive function that will call our takeInput() function and will end if the user entered a negative integer, otherwise, it will print the square of the number entered. (According to the problem statement, this will be the second recursive function, "one to call the first function to get an Int, and print the square") This function will make a recursive call to itself at the end, in order to repeat the process.
3. The takeInput() function will prompt the user for an integer by displaying a message, store that message in a val and return it to the getOutput() function, which will print its square, if it is found to be non-negative, otherwise, it will print "Goodbye!".
4. Please note that we are taking input from the user using scala.io.StdIn.readInt ().
5. Please note that in takeInput() a NumberFormatException will be raised if the user did not enter an integer when prompted for it. Therefore, we are using try block around the statement where the user is asked for the input and in the corresponding catch block, we will display the message that the value entered wasn't an integer and we will recursively call the takeInput() function.
Please find all the code explanations within the code itself as comments.
Here is the full code:
object printSquares {
// main function
def main (args: Array[String]) {
// call the getOutput() function to initiate the process
getOutput();
}
// this function will take the user input and
// find and print its square if the input
// was a non-negative integer, otherwise,
// will print Goodbye and end
def getOutput () {
// call the takeInput () function to
// get the input from the user
val num = takeInput ();
// check if input is negative
if (num < 0) {
// if yes, print GoodBye
println ("Goodbye!");
// and return
return;
}
// otherwise, if it's non-negative
// find its square
val square = num * num;
// print the square
println (num + "^2 is " + square + ".");
// recursively call getOutput () to repeat the process
getOutput ();
}
// this function will take the input and
// return it(the integer) to the getOutput () function
def takeInput () : Int = {
// prompt the user for the integer
println ("Enter an Int(negative to quit):");
try {
// use the readInt() function to take the input
val num = scala.io.StdIn.readInt();
// return the integer to getOutput ()
return num
} catch {
// if user did not enter an integer, an Exception
// will be thrown, and we will display the message to the user
case e: NumberFormatException => println("That is not an Int!");
// as the input couldn't be found, we will call the takeInput() again
val input = takeInput ();
// return the found input
return input;
}
}
}
Please refer to the screenshots of the code for understanding the indentation.
the main() function and the getOutput() function
the takeInput() function
The full code screenshot:
Let's look at the input-output as given in the problem statement:
Input-Output 1:
Input-Output 2: