Question: PLEASE HELP ME TO FIX THE PROBLEM FOR MY CODE JAVA: Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number
PLEASE HELP ME TO FIX THE PROBLEM FOR MY CODE JAVA:
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
1. Make a guess at the answer (you can pick n/2 as your initial guess). 2. Computer = n / guess. 3. Set guess = (guess +r) / 2. 4. Go back to step 2 until the last two guess values are within 1% of each other.
Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess is within 1% of the previous guess, and outputs the answer as a double to two decimal places. Your answer should be accurate even for large values of n.
INPUT and PROMPTS. The program prints "This program estimates square roots." and then prompts for a positive integer as follows: ("Enter an integer to estimate the square root of: " and then reads in the integer.
OUTPUT. Each time a new guess is computed, the program prints ithe line "Current guess: g", where g is the current guess, printed out with no special formatting. The final output is "The estimated square root of x is y", where x is the integer that was read in and y is the value computed by the procedure described above, printed out to two decimal places and a toal of six characters.
CLASS NAMES. Your program class should be called Babylonia
**MY CODE**:
import java.util.Scanner; //Babylonian.java public class Babylonian
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("This program estimates square roots.");
System.out.println();
System.out.print("Enter an integer to estimate the square root of: ");
int n = input.nextInt();
double guess = n / 2.0;
double previous = guess;
System.out.println("Current guess: " + guess);
do
{
double r = n / guess;
previous = guess;
guess = (guess +r)/2;
System.out.println("Current guess: " + guess);
} while (Math.abs(guess-previous) > 0.01 * previous);
System.out.printf("The estimaged square root of %d is %6.2f ",
n, guess);
}
}
**RESULT**:
Babylonia.java:3: error: class Babylonian is public, should be declared in a file named Babylonian.java public class Babylonian ^ 1 error
1 import java.util.Scanner; 2 //Babylonian.java 3 public class Babylonian 4 5 { 6 7 public static void main(String[] args) 8 9 { 10 11 Scanner input = new Scanner(System.in); 12 13 System.out.println("This program estimates square roots."); 14 15 System.out.println(); 16 17 System.out.print("Enter an integer to estimate the square root of: "); 18 19 int n = input.nextInt(); 20 21 22 23 double guess = n / 2.0; 24 25 double previous = guess; 26 27 System.out.println("Current guess: " + guess); 28 29 do 30 31 { 32 33 double r = n / guess; 34 35 previous = guess; 36 37 guess = (guess +r)/2; 38 39 System.out.println("Current guess: " + guess); 40 41 } while (Math.abs(guess-previous) > 0.01 * previous); 42 43 44 45 System.out.printf("The estimaged square root of %d is %6.2f ", 46 47 n, guess); 48 49 50 51 } 52 53 54 55 } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
