February Coding Challenge
In the month of February, we challenged our students to create a program that allows a user to calculate the square root of any number they enter. The one restriction was that they were not allowed to use any functions already provided by Java libraries. With only six weeks of Java instruction under their belts, our students were able to create highly functional and efficient programs, a testament to their hard work and dedication. You can take a look at the projects of our winners below!
What my program does is it calculates the approximate value of the root by using while() functions to continually subtract and add smaller and smaller values until it gets to a reasonably accurate value. First, type your value in the console, as it uses Scanner variables to receive a value. Next, the script uses while() loops, variables, comments, and math operators to continually “guess” values until it gets to an approximate value. It is similar to the Digit By Digit method to finding square roots. Finally, it will print the approximate value. Then it will multiply that value by -1, in order to receive the negative version of the number.
I learned a lot from doing this square root coding challenge. At first, I obviously just made a program that could find the square root when you entered any number in the program,. Then, I decided to take it a step further. Instead of having just any number be entered and square rooted, I would have a random number generator to decide the number. I decided that still wasn’t enough. In my final program, it asks you whether you want to enter your own number or have the computer generate one to be square rooted. This took a lot of time and research, but I finally got it to work.
The method I used is Heron’s Method. The equation is (x = the number that you want to find the square root for, y = the guessed square root): ((x / y) + y)/2. So if you input the values for an unknown square root. However, you don’t get the exact number, you get an approximate value. So, doing this multiple times will get you a very close value. In my code, I did this three times for a close value.