Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

babylonian method using recursion  RSS feed

 
christine ramos
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am supposed to write the babylonian method using recursion and so far this is what I have. I keep getting an error when compiling that says variable babyl may have not been initialized. Any suggestions??

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
variable babyl may have not been initialized. Any suggestions??

My suggestion is to give it an initial value.
 
christine ramos
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
variable babyl may have not been initialized. Any suggestions??

My suggestion is to give it an initial value.



If I assign it a value it won't perform the square root correctly
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The problem is line 34.  Why are you calling scanner.nextInt(radix)? I don't think that's what you need to be doing there. And where are you calling your babyl() method? I don't see you doing that anywhere.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, giving a variable the same name as a method makes your code less readable and potentially confusing.  Try to find a different name for the variable that you declare on line 27.  The name result might make more sense.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
christine ramos wrote:

Step away from the keyboard and pick up a pen and a paper. Write down the procedure for calculating Babylonian Square Roots using plain English.  Once you understand the procedure that way, it's easier to translate each step into Java instructions.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example:

In plain English:
1. Ask the user for a number whose square root we should calculate.

Equivalent in Java:

 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Christine,

please have a careful look at your base case. If guess=2, n=5 and err=0.001, what should be the outcome?
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Somebody else asked a related question recently; are they working at the same place as you? Is the Babylonian method the same as Newton's approximation?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!