Menu

Please Help Fix Problem Code Java Babylonian Algorithm Babylonian Algorithm Compute Square Q43888394

PLEASE HELP ME TO FIX THE PROBLEM FOR MY CODE JAVA:

Babylonian Algorithm. The Babylonian algorithmto compute the square root of a positive number n is asfollows:

      1. Make a guess at theanswer (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 thelast two guess values are within 1% of each other.

Write a program that inputs an integer for n, iterates throughthe Babylonian algorithm until the guess is within 1% of theprevious guess, and outputs the answer as a double to two decimalplaces. Your answer should be accurate even for large values ofn.

INPUT and PROMPTS. The program prints “Thisprogram estimates square roots.” and then prompts for a positiveinteger as follows: (“Enter an integer to estimate the square rootof: ” and then reads in the integer.

OUTPUT. Each time a new guess is computed, theprogram prints ithe line “Current guess: g”, where g is the currentguess, printed out with no special formatting. The final output is”The estimated square root of x is y”, where x is the integer thatwas read in and y is the value computed by the procedure describedabove, printed out to two decimal places and a toal of sixcharacters.

CLASS NAMES. Your program class should becalled 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 squareroots.”);

System.out.println();

System.out.print(“Enter an integer to estimate thesquare 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.2fn”,

n, guess);

}

}

**RESULT**:

Babylonia.java:3: error: class Babylonian is public, should be declared in a file named Babylonian.javapublic class Babylonian ^1 error 1 import java.util.Scanner;2 //Babylonian.java3 public class Babylonian4 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 do30 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.2fn”,46 47 n, guess);48 49 50 51 }52 53 54 55 }

Expert Answer


Answer to PLEASE HELP ME TO FIX THE PROBLEM FOR MY CODE JAVA: Babylonian Algorithm. The Babylonian algorithm to compute the square…

OR