Methods

Image

Loved studying this chapter and the related material. Here are the code for the more trickier exercises at the end of the chapter.


 

Quadratic Equation:

ImageI’ve explained my strategy in the comments. I would have liked to return both the solutions back to the run method for further manipulation, but I don’t know how to return two variables from a method. (Or if its even possible). I’m sure there is some way to use arrays to achieve that but I haven’t yet touched the topic of arrays so I’ll leave it for later.

quad_soln

askYesNoQuestion

8_questThis problem wasn’t that tricky but it needed some thought as it was the first predicate method I wrote. I used the loop-and-a-half trick for this method so that the return statement breaks the while(true) loop.

8_ans
And the output is something like this:
8_out

And 1 more —-

isPrime

10_questI did the brute method myself but had to refer to a math website for the more efficient strategy. Again, everything is explained in the comments.

10_ans