To develop a recursive solution to an open-ended iterative problem

DESCRIPTION
Objectives
To develop a recursive solution to an open-ended iterative problem
To develop a program that manages bad input
Description
Write a program (printSquares.scala) that read non-negative Int values from a user one at a time. For each positive Int, print out a message with the square of the number. Your program should quit when the use enters a negative Int. Note: You will also need to manage bad input! If the user types something in that isn’t an Int, your program should print an error message and ask again! Pay close attention to the specific messages that your program must print!
Remember: You don’t want any side effects with recursion, so you should not use any “global” variables in your program!
Sample Run 1
$ scala printSquares.scala
Enter an Int (negative to quit):
>4
4^2 is 16.
Enter an Int (negative to quit):
>2
2^2 is 4.
Enter an Int (negative to quit):
>-3
Goodbye!
Sample Run 2
$scala printSquares.scala
Enter an Int (negative to quit):
>5
5^2 is 25.
Enter an Int (negative to quit):
>3
3^2 is 9.
Enter an Int (negative to quit):
>six
That is not an Int!
Enter an Int (negative to quit):
>6.0
That is not an Int!
Enter an Int (negative to quit):
>6
6^2 is 36.
Enter an Int (negative to quit):
>-3
Goodbye!
Hints
Write 2 recursive functions. One to ask the user for an Int and recurse until a Int is entered, and one to call the first function to get an Int, and print the square (ending if the Int is negative).
Use try/catch blocks to trap run time exceptions like NumberFormatException

 

In: Computer Science

Don't use plagiarized sources. Get Your Custom Essay on
To develop a recursive solution to an open-ended iterative problem
Just from $8 /Page 0r 300 words
Order Now

Looking for a similar assignment? Get help from our qualified experts!

Order Now

Save your time - order a paper!

Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlines

Order Paper Now