Question

Java Program Create a Project called “SquareRoot” with a class called “SquareRoot”. Write a Java program...

Java Program Create a Project called “SquareRoot” with a class called “SquareRoot”.

Write a Java program to compute the square root of a double using the algorithm

The ancient Babylonians had an algorithm for determining the square root of a number a. Start with an initial guess of a / 2. Then find the average of your guess g and a / g. That’s your next guess. Repeat until two consecutive guesses are close enough.

For example,

Square Root by Tai Babylonia

Enter the target value: 1000

Enter the initial guess: 30

31.666666667 squared equals 1002.7777778, which is 100.27777778% of the target.

31.622807018 squared equals 1000.0019237, which is 100.00019237% of the target.

31.622776602 squared equals 1000.0000000, which is 100.00000000% of the target.

The calculated square root of 1000.0000000 is 31.622776602.

To format the numbers, use the format specifier %.11g – this will always show 11 significant figures and will allow very small numbers to be displayed correctly as well:

Square Root by Tai Babylonia

Enter the target value: 0.00000045678

Enter the initial guess: .005

0.0025456780000 squared equals 6.4804764797e-06, which is 1418.7303471% of the target.

0.0013625557670 squared equals 1.8565582182e-06, which is 406.44472573% of the target.

0.00084889671095 squared equals 7.2062562587e-07, which is 157.76207931% of the target.

0.00069349168790 squared equals 4.8093072119e-07, which is 105.28716695% of the target.

0.00067607927935 squared equals 4.5708319197e-07, which is 100.06637593% of the target.

0.00067585505123 squared equals 4.5678005028e-07, which is 100.00001101% of the target.

0.00067585501404 squared equals 4.5678000000e-07, which is 100.00000000% of the target.

The calculated square root of 4.5678000000e-07 is 0.00067585501404.

To create the loop, we will use the following construct:

while (true) {
// compute next guess

// termination condition

if ( /* guess is close enough */ ) break;

}

After you compute the percentage, consider the guess to be close enough if the absolute value of the percentage minus 100 is less than 1E-8.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class SqrtTest {

        public static void main(String[] args) {
                
                Scanner in = new Scanner(System.in);
                
                System.out.println("Square Root by Tai Babylonia");
                System.out.println("Enter the target value: ");
                
                double target = in.nextDouble();
                
                System.out.println("Enter the initial guess: ");
                double initialGuess = in.nextDouble();
                
                double currentGuess = initialGuess, prevGuess;
                
                do {
                        prevGuess = currentGuess;
                        currentGuess = (prevGuess + target/prevGuess)/2.0;
                        
                        double value = currentGuess * currentGuess;
                        double percChange = value * 100 / target;
                        System.out.printf("%.11g squared equals %.11g, "
                                        + "which is %.11g%% of the target\n",
                                        currentGuess, value, percChange);
                        
                } while(Math.abs(currentGuess - prevGuess) > Math.pow(10, -8));
                
                
                System.out.printf("The calculated square root of %.11g is %.11g.\n"
                                , target, currentGuess);
                in.close();
                
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Java Program Create a Project called “SquareRoot” with a class called “SquareRoot”. Write a Java program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • Please help! We use Java JGrasp. Write a Java program to take the input of 5...

    Please help! We use Java JGrasp. Write a Java program to take the input of 5 numbers and output the mean (average) and standard deviation of the numbers. If the numbers are x1, x2, x3, x4, and x5, the formula for mean is X = (x1 + x2 + x3 + x4 + x5)/5 And the formula for standard deviation is You can also break standard deviation down like this: 1. Work out the Mean (the simple average of the...

  • 5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create...

    5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create a method called generateSuperLottoNumbers() that returns an array of 6 random SuperLotto lottery numbers. The first 5 numbers must be from the range 1 to 47 The 6th number (the MEGA) must be from 1 to 27. Create a method called printTicket() that takes an integer array as an parameter it will loop through the integer array and print out the data Display the...

  • Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the...

    Create a New Java Project called YourLastNameUpperCase. Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • Write a program that takes as input a positive real number s and a positive real...

    Write a program that takes as input a positive real number s and a positive real number E and outputs the approximate value of Vs to within the tolerance E. In other words, the output of your program should be a number s' such that Is SIS E. (Thus the distance" between the output s' and the correct answer Vs should be at most E.) Your program should implement a very old algorithm for approximating the square root of numbers,...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • Java Program: In this project, you will write a program called GradeCalculator that will calculate the...

    Java Program: In this project, you will write a program called GradeCalculator that will calculate the grading statistics of a desired number of students. Your program should start out by prompting the user to enter the number of students in the classroom and the number of exam scores. Your program then prompts for each student’s name and the scores for each exam. The exam scores should be entered as a sequence of numbers separated by blank space. Your program will...

  • 4. One interesting property of a Fibonacci sequence is that the ratio of the values of...

    4. One interesting property of a Fibonacci sequence is that the ratio of the values of adjacent members of the sequence approaches a number called "the golden ratio". Create a program that accepts the first two numbers of a Fibonacci sequence as user input and then calculates additional values in the sequence until the ratio of adjacent values converges to within 0.001 You can do this in a while loop by comparing the ratio of element k to element k...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT