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

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 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