Question

Implement a function with the following signature: int tryThis(int); The function must do this in this...

Implement a function with the following signature:

int tryThis(int);

The function must do this in this order:

if the parameter is less than zero, throw any negative int.

if p is less than 10, throw the string expression: "Bad value 1"

if p more than 100, throw the string expression: "Bad value 2"

if all the above tests are not true, check to see if the parameter is divisible by 10, throw 100.

If no expression needs to be thrown, return the parameter cubed.

The tests will verify these conditions. There's no need to add any code in main unless you wish to test.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assuming the language is C++, Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//required method

int tryThis(int value){

                //if value is negative, throwing a negative value

                if(value<0){

                                throw value;

                }

                //if value is less than 10, throwing "Bad value 1"

                if(value<10){

                                throw "Bad value 1";

                }

                //if value is greater than 100, throwing "Bad value 2"

                if(value>100){

                                throw "Bad value 2";

                }

                //if value is divisible by 10, throwing 100

                if(value%10==0){

                                throw 100;

                }

                //otherwise returning cube of value

                return value*value*value;

}

Add a comment
Know the answer?
Add Answer to:
Implement a function with the following signature: int tryThis(int); The function must do this in this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT