Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

The Heron Method for approximating the square root of a number states that if x is a guess for the square root of n then a better guess x' is:

x' = [x + (n/x)] / 2

Naturally this process can be repeated using x' in place of x the next time through the loop. The loop can stop when the difference between the previous and next guesses is small enough.

A programmer wrote the following code to implement the Heron Method to locate the square root of any number:

2374_ff.png

function heronSqrt(n)
{
var DELTA = 1.0E-10;
var nextGuess;
var prevGuess = n;
do
{
nextGuess = (prevGuess + (n/prevGuess))/2.0;
prevGuess = nextGuess;
} while (nextGuess-prevGuess > DELTA)
return nextGuess;
}

However, the code does not work properly. Fix the program so that it works. Also list a set of test cases that will thoroughly exercise the Heron Method List a set of test cases that will thoroughly exercise the Heron Method code above. Consider all possible kinds of square roots.

The following are some guidelines for choosing test cases:

• Have several test cases where the code is expected to work correctly on fairly standard input. These are positive tests and correspond to test cases 1 and 3 above.

• Have several test cases where the code is expected to work correctly on boundary input (input that is close to being "wrong"). These are positive boundary tests and correspond to test cases 9, 10, and 11.

  • Have as many "special case" positive test cases as necessary, where the input is correct but requires accounting for special cases. These could be either positive or negative test cases, but in this example corresponds to test cases 4, 5, 7, 8, and 10.

Selecting test cases

Determining which inputs to use to create good test cases requires careful thought. Consider the following code for a RootFinder object that finds the integer root of a number. The algorithm is a binary search using a reversible function:

function RootFinder(number, root)
{
    this.number = number;
    this.root = root;
}
 
RootFinder.prototype.approxEqual = function(x, y)
{
    var EPSILON = 1E-12;
    return Math.abs(x - y) <= EPSILON;
}
    
RootFinder.prototype.pow = function(number, power)
{
    var result = 1;
    for (var i=0; i
    {
        result *= number;
    }
    return result;
}
    
RootFinder.prototype.getRoot = function()
 {
    // binary search for resultvar left = 0;
    var right = this.number;
    var mid;
        
    do
    {
        mid = (left + right)/2.0;
        if (this.pow(mid, this.root) < this.number)
            left = mid;
        else
            right = mid;
    } while (!this.approxEqual(left, right));
    return mid;
}

(The source code is available for both RootFinder.js and RootFinder.html)

Consider test cases for this code; the only possible inputs are the number and the root desired. What inputs are valid? What inputs are invalid? What valid inputs will produce a good result in this version? What valid inputs will produce bad results? Clearly, some knowledge of the problem domain is required. The table below classifies some inputs:

Case

number

root

Valid?

Expected

Notes

1

25.0

2

Yes

5.00

Should work

2

-25.0

2

No

none

No even roots of negatives

3

27.0

3

Yes

3.00

Should work

4

-27.0

3

Yes

-3.00

Should work

5

0.25

2

Yes

0.500

Should work

6

-0.25

2

No

none

No even roots of negatives

7

0.125

3

Yes

0.500

Should work

8

-0.125

3

Yes

-0.500

Should work

9

1.00

2

Yes

1.00

Should work

10

-1.00

3

Yes

-1.00

Should work

11

0.0

2

Yes

0.00

Should work

Of these test cases, the presented code only passes cases test cases 1, 3, 9, and 11! The remaining test cases ended up in an infinite loop. The code presented above was written to work on all the "normal" cases; however, the assumptions were wrong for ranges of numbers in which the other test cases fell. For instance, the code assumes that all roots of a number will be found between 0 and the number itself. However, this assumption is false in test cases 5, 7, and 8. Cases 2 and 6 expose the lack of precondition checking. Finally, cases 4, 8, and 10 have left and right reversed.

Evaluating Test Cases

How does a programmer know when the output of a method is correct? Evaluating the correctness of test cases is another difficult issue. In the case of the RootFinder above, the algorithm's results can be predicted with known input, which is the way a majority of test cases are structured. For instance, if the expected answer is 10.0, then inputs of (1000.0, 3) or (100.0, 2) should yield 10.0 as the answer. Another powerful method is to use an oracle. An oracle is another method that is known to produce the correct result, and in the RootFinder example, that method is Math.pow(). The same inputs to Math.pow() can be provided to the constructor of RootFinder, and the results can be compared. One may ask why someone would wish to write a new method that does the same thing as an existing oracle. The answer is that the new method may be designed to work faster than the oracle or that an entire system is being re-written, yet the outputs of the systems should match.

• Have several test cases where the code is expected to check preconditions for bad input. These are negative test cases and correspond to test cases 2 and 6.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M91615103
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Computer Engineering

Question the chief technology officer cto has indicated

Question: The Chief Technology Officer (CTO) has indicated that your organization has been requested by the National Security Council (NSC) to comment on the upcoming National Cybersecurity Strategy. The NSC has asked fo ...

Suppose that we have a block cipher and want to use it as a

Suppose that we have a block cipher and want to use it as a hash function. Let X be a specified constant and let M be a message consisting of a single block, where the block size is the size of the key in the block ciphe ...

Systems analysis project personal trainer inc owns and

Systems analysis project Personal Trainer, Inc. owns and operates fitness centers in a dozen Midwestern cities. The centers have done well, and the company is planning an international expansion by opening a new "superce ...

Write review on this article with apa formatdigital

Write review on this article with APA format. Digital evidence has become a key component of the judicial process, due solely to the frequency of communications and transactions completed on computers and mobile devices. ...

Start from scratch on each of the parts write a main

Start from scratch on each of the parts. Write a main function that declares an array of 100 doubles. In a for loop, assign each of the doubles a random number between 0.50 and 50.00. Here's how. array[i] = (double) (ran ...

Patient patientid familyname givenname address suburb state

PATIENT (PatientID, FamilyName, GivenName, Address, Suburb, State, PostCode) DOCTOR (ProviderNo, Name) ITEM (ItemNo, Description, Fee) ACCOUNT (AccountNo, ProviderNo, PatientID, TreatmentDate) ACCOUNTLINE (AccountNo, Ite ...

Assignmentsuppose you are asked to write a program to count

Assignment Suppose you are asked to write a program to count the frequency of occurrence of each word in a document. Describe how you would implement your program using: 1. A hash table to store words and their frequenci ...

Question each student will research a paper on big data

Question: Each student will research a paper on Big Data word document 1000 to 1500 words with graphics describing the following: 1. what is Big Data? 2. Origins ? 3. Differences? 4. examples of Big data your point of vi ...

A random sample ofnbsp87nbspeighth gradenbspstudents scores

A random sample of 87 eighth grade? students' scores on a national mathematics assessment test has a mean score of 279. This test result prompts a state school administrator to declare that the mean score for the? state' ...

Question suppose you have a 50-bit string that contains

Question : Suppose you have a 50-bit string that contains exactly 37 ones at random positions. How many bits of entropy does this string contain?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As