Ch 6 Lab: Newton's Method for Calculating Square Root

Authors: Ralph Morelli and Bronzell Dinkins
Trinity College, Hartford, CT
For use with Chapter 6

Brief description

This is a lab that is suitable for Chapter 6. It requires a conditional loop. It uses a simple applet interface, including TextArea and TextField components. This lab requires students to take more of a role in designing their solution. It asks that they prepare a design document -- including UML diagrams and pseudocode descriptions of the key algorithms involved -- before beginning to code.

Objectives

The objectives of this lab are:

Problem Statement

Newton's method for calculating the square root of N starts by making a (non zero) guess at the square root. It then uses the original guess to calculate a new guess, according to the following formula:
    
       guess = (( N / guess) + guess) / 2 ;
No matter how wild the original guess is, if we repeat this calculation the algorithm will eventually find the square root of N.

Design a Java applet that let's the user input a number N, a maximum error that determines how close Newton's method should come to the square root calculated by Math.sqrt(), and the user's initial guess. Then calculate the square root using Newton's method and report both the square root and the number of guesses required to calculate it.

Before Lab

Read Chapter 6 of Java, Java, Java, 3E and carefully read this lab writeup.

Object Oriented Design

Before creating your Codewarrier project and before beginning to code your solution, you must hand in, for approval, a design document containing the elements described below. This should be done within the first hour of lab, and must be approved before you can start writing code

GUI Design

Follow the GUI design shown in the demo program. This involves three input TextFields and an output TextArea. The TextFields should all be associated with ActionListeners.

Object Decomposition and Class Design

What objects will your solution use? Follow the design shown on pages 314-318, which utilizes two objects, an applet that serves as a user interface and a computational object, which handles the computation of the square root. Let's call the computational object NewtonTester. For each object, draw its UML class diagram showing its name, instance variables, and its public and private methods.

One public method that your NewtonTester class should have is the sqrt() method. This is the method that the applet will call to calculate the square root of N.

Don't forget that your NewtonTester object must not only be able to calculate the square root of a number that it is given, it must also remember how many iterations it took to calculate the square root. It must also "know" what the tolerance is and what the initial guess is in order to perform its calculation. How will you handle these parts of the problem?? HINT: What additional instance variables and instance methods will you need for these tasks?

Method Design

For this project your applet methods, such as init() and actionPerformed() can be modeled after the applets you designed in previous labs. The init() method should instantiate the GUI components used by the applet and add them to the applet. The actionPerformed() method should input the values that the user typed into the TextFields and pass them to the NewtonTester object to perform the calculation.

For the NewtonTester you'll need methods to set and get the values of certain instance variables -- e.g., the number of iterations used -- as well as the method to calculate the square root.

Algorithm Design

There are two algorithms that you need to think about in detail before you begin coding. One is the algorithm for the applet's actionPerformed() method. The other is the algorithm for the NewtonTester object's sqrt() method. Write out pseudocode descriptions of both of these algorithms. Remember, in addition to calculating the square root, the square root method should also keep track of how many iterations were required. How are you going to handle this?

Implementation

After your design documents have been approved, you may create your CodeWarrier project and begin coding your solution. Your code should closely follow your design.

Use the stepwise refinement approach as you develop your code, compiling and testing short segments of your code at each stage. Here are some appropriate steps:

Optional

Hand in

Hand in your appropriately documented source code. For documentation guidelines, see Documentation Specifications.

You're done. Great work!