I have to write a recursion method that finds the square root of a number by creating more accurate guesses each time. For some reason, my code is not returning any value and is not terminating at my base case. Can you help me see the issue I am having?
I have to write a recursion method that finds the square root of a number by creating more accurate guesses each time. For some reason, my code is not returning any value and is not terminating at my base case. Can you help me see the issue I am having?
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
I have to write a recursion method that finds the square root of a number by creating more accurate guesses each time. For some reason, my code is not returning any value and is not terminating at my base case. Can you help me see the issue I am having?
import java.util.Scanner;
/**
* Greek's method to approximate the square root of a given number.
*/
public class SquareRootComputer {
publicstaticvoidmain(String[] args) {
// this is your tester
// read a value from the user and print the results
// along with expected value
Scannerin=newScanner(System.in);
System.out.println("Please input a value whose square root you would like determined: ");
doublex=in.nextDouble(); //makes user input the value for x to determine it's square root. USE 91.
System.out.println("The square root of "+x+"is: "+squareRoot(x));
System.out.println("Expected square root value of 91 is: 9.539392014.");
in.close(); //closes the scanner to prevent any unintended errors.
}
publicstaticdoublesquareRoot(doublex) {
// from this method come up with a guess
// and then make the call to the recursive method
// squareRootGuesser()
returnsquareRootGuess(x, x-1);
}
privatestaticdoublesquareRootGuess(doublex, doubleg) {
// recursive method to compute the square root of x
// you will need to have a base case and a recursive case
// in this method
if(Math.abs(Math.pow(g, 2) -x) <=0.001){ //need Math.abs incase the difference returns a negative value.
returng; //base case. If the g^2 is approximately equal to x, then return g.
}else{
doublebetterGuess= (g+ (x/g) /2); //formula for finding the better guess.
returnsquareRootGuess(x, betterGuess); //otherwise we need to return a better guess and try the method again.
}
//return 0;
}
}
Expert Solution
Changes :
In your formula (g+ (x/g) /2); you have given brackets wrongs.
That's why the number is increasing not decreasing.
So change (g+ (x/g) /2); to (g+ x/g) /2;
Step by step
Solved in 2 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education