Purchase Solution

Newtom's method using Java

Not what you're looking for?

Ask Custom Question

The NewtonsSquareRoot Class
Objectives
? Writing a class using conditionals that test double values
? Writing a class using a while loop
The project directory should include the following:
? NewtonsSquareRoot.java
? NewtonsSquareRootTest.java
Details
To approximate the square root of a positive number n using Newton's method, you need to make an initial guess at the root and then refine this guess until it is "close enough." Your first initial approximation should be root = 1; A sequence of approximations is generated by computing the average of root and n/root.
Use the constant:
private static final double EPSILON = .00001;
Your loop for findSquareRoot should behave like this:
make the initial guess for root
while ( EPSILON < absolute value of the difference between root squared and n )
calculate a new root
return root
Your class should have a constructor that takes the number you want to find the square root of. Implement the usual accessor method(s) and a findSquareRoot method that uses Newton's method described above to find and return the square root of the number. Add a method setNumber that takes a new number that replaces the number in the instance field. Supply a toString method that returns a string containing the number and the square root of the number.
Your test class should:
? Instantiate a NewtonsSquareRoot object using the number 1.
? Print the square root of 1 using the toString method.
? Use a while loop to generate the numbers 2 through 100.
? In the loop use the setNumber and toString methods to find and print the square roots of all the numbers from 2 to 100 inclusive.

Attachments
Purchase this Solution

Solution Summary

This solution implements Newton's method to estimate a square root.

Solution Preview

This solution shows how to use Newton's Method to estimate the square root of a number. The class NewtonsSquareRoot encapsulates this functionality.

Newton's method for finding a square root ...

Purchase this Solution


Free BrainMass Quizzes
Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

C# variables and classes

This quiz contains questions about C# classes and variables.

Basic Networking Questions

This quiz consists of some basic networking questions.